如何限制在一台机器上只开一个本程序(100分)

  • 主题发起人 主题发起人 树熊131
  • 开始时间 开始时间

树熊131

Unregistered / Unconfirmed
GUEST, unregistred user!
不知道那未大侠能帮我一下。
 
自己去看或者去搜索一下,多的是~~~
var
hMutex : Thandle;
WaitResult : word;
BroadcastList : DWORD;
begin
MessageID := RegisterWindowMessage('Check For Choice Previous Inst');
// register a message to use later on
hMutex := createMutex(nil,false,pchar('App_Choice'));
// grab a mutex
handle
WaitResult := WaitForSingleObject(hMutex,10);
// wait to see
if we can have exclusive use of the mutex
if ( waitResult = WAIT_TIMEOUT ) then
// if we can't then
broadcast
the message to make the owner of the mutex respond
{ request that the running application takes focus }
begin
BroadcastList := BSM_APPLICATIONS;
BroadcastSystemMessage(
BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0);
//32 bit - broadcast the
message to all apps - only a prev inst will hear it.
end
else
begin
{do
the normal stuff}
Application.Title := 'Choice Organics Purchase &
Sales System';
Application.CreateForm(TMainForm, MainForm);
Application.Run;
ReleaseMutex(hMutex);
// release the mutex as a politeness
end;
CloseHandle(hMutex);
// close the mutex handle
end.

This goes in the MainForm
procedure Tmainform.OnAppMessage(var Msg : TMsg ;
Var Handled : Boolean);
begin
{ If it's the special message then
focus on this window}
if Msg.Message = MessageID then
// if we get the broadcast message from an
another instance of this app that is trying to start up
begin
show;
WindowState := wsMaximized;
BringToFront;
SetFocus;
Handled := true;
end;
end;

//And this goes in the TMainForm.FormCreate ;-
Application.OnMessage:= OnAppMessage;
 
program DEL3test;
uses
Forms,Windows,SysUtils,
DEL3unit in 'DEL3unit.pas' {Form1};
{$R *.RES}
Var
hMutex:HWND;
Ret:Integer;
begin
Application.Initialize;
Application.Title := 'aaaaaa';
hMutex:=CreateMutex(nil,False,'aaaaaa');
Ret:=GetLastError;
If Ret<>ERROR_ALREADY_EXISTS then
begin
Application.CreateForm(TForm1, Form1);
Application.Run;
End
else
Application.MessageBox('Run Twice!','Notes!',MB_OK);
ReleaseMutex(hMutex);
end.
 

用Windows的全局表的方法最好﹐雖不盡然... ﹐但簡單的很﹐在各種方法中最好﹐
你可到 http://go1.163.com/delphigirl/
不知有沒有? 只有几行

 
program MB;
uses
Forms,
Windows,
SysUtils,
Main in 'Main.pas' {Form_Main},
var
AHand : Integer;
{$R *.RES}
begin
AHand := FindWindowEx(0,0,nil,'分检程序');
if AHand<>0 then
SetForegroundWindow(AHand);
Application.Initialize;
Application.Title := '分检程序';
if AHand=0 then
//让程序只运行一份
begin
Application.CreateForm(TForm_Main, Form_Main);
Application.Run;
end;
end.
 
但最好告诉我hwnd,hMutex:=CreateMutex(nil,False,'aaaaaa');
Ret:=GetLastError;ERROR_ALREADY_EXISTS是什么意思!
 
同意LiWD,我就是如此做的。另对LiWD的代码稍加修改和说明:
{项目文件}
program MB;
uses
Forms,
Windows,
SysUtils,
Main in 'Main.pas' {Form_Main},
var
AHand : Integer;
{$R *.RES}
begin
AHand := FindWindowEx(0,0,nil,'主窗体名');
//查找以名称为'主窗体名'的窗体个数
if AHand<>0 then
//如果AHand>0,则已有本程序正在Running...
SetForegroundWindow(AHand);
Application.Initialize;
if AHand=0 then
//如果AHand=0,则无本程序正在Running...
begin
Application.CreateForm(TForm_Main, Form_Main);
Application.Run;
end;
end.

 
Mutex叫着互斥物件(Mutual exclusion object),如果mutex没有拥有者,则呼叫这个函数的
应用程序就变成拥有者,若mutex已有拥有者,则应用程序会等到逾时后传回错误代码.
所以,假如已经存在,就返回ERROR_ALREADY_EXISTS,否则返回0.
你看看帮助吧.
The CreateMutex function creates a named or unnamed mutex object.
HANDLE CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAttributes, // pointer to security attributes
BOOL bInitialOwner, // flag for initial ownership
LPCTSTR lpName // pointer to mutex-object name
);

Parameters
lpMutexAttributes
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpMutexAttributes is NULL, the handle cannot be inherited.
Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new mutex. If lpMutexAttributes is NULL, the mutex gets a default security descriptor.
Windows 95: The lpSecurityDescriptor member of the structure is ignored.
bInitialOwner
Specifies the initial owner of the mutex object. If TRUE, the calling thread requests immediate ownership of the mutex object. Otherwise, the mutex is not owned.
lpName
Points to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (/). Name comparison is case sensitive.
If lpName matches the name of an existing named mutex object, this function requests MUTEX_ALL_ACCESS access to the existing object. In this case, the bInitialOwner parameter is ignored because it has already been set by the creating process. If the lpMutexAttributes parameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.
If lpName is NULL, the mutex object is created without a name.
If lpName matches the name of an existing event, semaphore, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because event, mutex, semaphore, and file-mapping objects share the same name space.

Return Values
If the function succeeds, the return value is a handle to the mutex object. If the named mutex object existed before the function call, the GetLastError function returns ERROR_ALREADY_EXISTS. Otherwise, GetLastError returns zero.
If the function fails, the return value is NULL. To get extended error information, call GetLastError
.
Remarks
The handle returned by CreateMutex has MUTEX_ALL_ACCESS access to the new mutex object and can be used in any function that requires a handle to a mutex object.
Any thread of the calling process can specify the mutex-object handle in a call to one of the wait functions. The single-object wait functions return when the state of the specified object is signaled. The multiple-object wait functions can be instructed to return either when any one or when all of the specified objects are signaled. When a wait function returns, the waiting thread is released to continue its execution.
The state of a mutex object is signaled when it is not owned by any thread. The creating thread can use the bInitialOwner flag to request immediate ownership of the mutex. Otherwise, a thread must use one of the wait functions to request ownership. When the mutex's state is signaled, one waiting thread is granted ownership, the mutex's state changes to nonsignaled, and the wait function returns. Only one thread can own a mutex at any given time. The owning thread uses the ReleaseMutex function to release its ownership.
The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution. Typically, you would not wait repeatedly for the same mutex, but this mechanism prevents a thread from deadlocking itself while waiting for a mutex that it already owns. However, to release its ownership, the thread must call ReleaseMutex once for each time that the mutex satisfied a wait.
Two or more processes can call CreateMutex to create the same named mutex. The first process actually creates the mutex, and subsequent processes open a handle to the existing mutex. This enables multiple processes to get handles of the same mutex, while relieving the user of the responsibility of ensuring that the creating process is started first. When using this technique, you should set the bInitialOwner flag to FALSE;
otherwise, it can be difficult to be certain which process has initial ownership.
Multiple processes can have handles of the same mutex object, enabling use of the object for interprocess synchronization. The following object-sharing mechanisms are available:
?A child process created by the CreateProcess function can inherit a handle to a mutex object if the lpMutexAttributes parameter of CreateMutex enabled inheritance.
?A process can specify the mutex-object handle in a call to the DuplicateHandle function to create a duplicate handle that can be used by another process.
?A process can specify the name of a mutex object in a call to the OpenMutex or CreateMutex function.

Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
 
最easy的方法,放一个LMDOneInstance控件就可以了,你用LMD吗?
 
看了DELPHI的帮助说明
对于FINDWINDOWSEX的说明好象是这样的
它的返回值好象是如果函数所要检查的值存在久返回窗口的句柄
如果不存在久返回空值
我不知道说的对还是不对
可是我看帮助好象是这样说的
这样的话好象就有点问题了
希望能给我说说
好吗?
 
zhangkan,没必要这么多吧
 
procedure tform1.stop_application();
var
ZAppName: array[0..127] of char;
Hold: String;
Found: HWND;
begin
// 防止本系统被重复执行
Hold := Application.Title;
Application.Title := 'OnlyOne' + IntToStr(HInstance);
// 暂时修改窗口标题
StrPCopy(ZAppName, Hold);
// 原窗口标题
Found := FindWindow(nil, ZAppName);
// 查找窗口
application.Title :=Hold;
// 恢复窗口标题
if Found<>0 then
begin
// 若找到则激活已运行的程序并结束自身
showmessage('本程序已经运行!');
Application.Terminate;
end;
end;
 
Var
hMutex:HWND;
Ret:Integer;
begin
Application.Initialize;
Application.Title := 'title';
hMutex:=CreateMutex(nil,False,'title');
Ret:=GetLastError;
If Ret<>ERROR_ALREADY_EXISTS then
begin
Application.CreateForm(Tfm_password, fm_password);
Application.CreateForm(Tfm_wait, fm_wait);
fm_wait.Show;
fm_wait.Refresh;
fm_wait.Hide;
fm_wait.Free;
Application.Run;
End
else
ReleaseMutex(hMutex);
end.
 
到深度历险找找,有很多这样的组件
http:/vcl.vclxx.org
 

Similar threads

后退
顶部