一个简单的问题!(50分)

  • 主题发起人 主题发起人 lcj49997
  • 开始时间 开始时间
L

lcj49997

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;
 
create事件里面
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 // 若找到则激活已运行的程序并结束自身
; ; ShowWindow(Found, SW_RESTORE);
; ; Application.Terminate;
; end;
 
implementation
var
; hnd:THandle;

initialization
; hnd := CreateMutex(nil, True, 'irgendwaseinmaliges');
; if GetLastError=ERROR_ALREADY_EXISTS then
; ; Halt;

finalization
; if hnd<>0 then
; ; CloseHandle(hnd);

end.
 
program Prj_RunOnce;

uses
; Forms,
; Windows,
; SysUtils,
; two in 'two.pas' {Form1};

{$R *.RES}
Var
; hMutex:HWND;
; Ret:Integer;

begin
; Application.Initialize;
; Application.Title:='Program Run Once Test';
; hMutex:=CreateMutex(nil,False,'Program Run Once Test');
; Ret:=GetLastError;
; If Ret<>ERROR_ALREADY_EXISTS Then
; ; begin
; ; ; Application.CreateForm(TForm1, Form1);
; ; ; Application.Run;
; ; end
; else
; ; Application.MessageBox('Already Run!','Error!',MB_OK);
; ; ReleaseMutex(hMutex);
end.
 
我是用BCB的,谁能给我翻译成C,我看Pascal有点吃力!麻烦了!
谢谢以上几位了!
 
來慢了!
我想的,大家都說了。稍微將Crealion_zy的改一下
implementation
var
; hnd:THandle;

initialization
; hnd := CreateMutex(nil, True, 'irgendwaseinmaliges');
; if GetLastError<>ERROR_ALREADY_EXISTS then
; ; begin
; ; ;Application.CreateForm(TForm1, Form1);
; ; ;Application.Run;
; ; end;
finalization
; if hnd<>0 then
; ; CloseHandle(hnd);

end.
 
我是用BCB的,谁能给我翻译成C,我看Pascal有点吃力!麻烦了!
谢谢以上几位了!
 
后退
顶部