其实2最安全的方法还是互斥,具体代码在d5开发人员指南中3章有 ,直接在你的程序中new一个unit,拷贝下面代码上去就ok了unit MultInst;interfaceconst MI_QUERYWINDOWHANDLE = 1; MI_RESPONDWINDOWHANDLE = 2; MI_ERROR_NONE = 0; MI_ERROR_FAILSUBCLASS = 1; MI_ERROR_CREATINGMUTEX = 2;// Call this function to determine if error occurred in startup. // Value will be one or more of the MI_ERROR_* error flags. function GetMIError: Integer;implementationuses Forms, Windows, SysUtils;const UniqueAppStr = 'DDG.I_am_the_Eggman!';var MessageId: Integer; WProc: TFNWndProc; MutHandle: THandle; MIError: Integer;function GetMIError: Integer; begin Result := MIError; end;function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint): Longint; stdcall; begin Result := 0; // If this is the registered message... if Msg = MessageID then begin case wParam of MI_QUERYWINDOWHANDLE: // A new instance is asking for main window handle in order // to focus the main window, so normalize app and send back // message with main window handle. begin if IsIconic(Application.Handle) then begin Application.MainForm.WindowState := wsNormal; Application.Restore; end; PostMessage(HWND(lParam), MessageID, MI_RESPONDWINDOWHANDLE, Application.MainForm.Handle); end; MI_RESPONDWINDOWHANDLE: // The running instance has returned its main window handle, // so we need to focus it and go away. begin SetForegroundWindow(HWND(lParam)); Application.Terminate; end; end; end // Otherwise, pass message on to old window proc else Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam); end;procedure SubClassApplication; begin // We subclass Application window procedure so that // Application.OnMessage remains available for user. WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(@NewWndProc))); // Set appropriate error flag if error condition occurred if WProc = nil then MIError := MIError or MI_ERROR_FAILSUBCLASS; end;procedure DoFirstInstance; // This is called only for the first instance of the application begin // Create the mutex with the (hopefully) unique string MutHandle := CreateMutex(nil, False, UniqueAppStr); if MutHandle = 0 then MIError := MIError or MI_ERROR_CREATINGMUTEX; end;procedure BroadcastFocusMessage; // This is called when there is already an instance running. var BSMRecipients: DWORD; begin // Prevent main form from flashing Application.ShowMainForm := False; // Post message to try to establish a dialogue with previous instance BSMRecipients := BSM_APPLICATIONS; BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @BSMRecipients, MessageID, MI_QUERYWINDOWHANDLE, Application.Handle); end;procedure InitInstance; begin SubClassApplication; // hook application message loop MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr); if MutHandle = 0 then // Mutex object has not yet been created, meaning that no previous // instance has been created. DoFirstInstance else BroadcastFocusMessage; end;initialization MessageID := RegisterWindowMessage(UniqueAppStr); InitInstance; finalization // Restore old application window procedure if WProc <> Nil then SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc)); if MutHandle <> 0 then CloseHandle(MutHandle); // Free mutex end.
CreateMutex(nil, true, '你的程序名不是文件名哦'); if GetLastError = ERROR_ALREADY_EXISTS then begin Exit; end;避免重复运行,放在dpr 中,
互斥如楼上,我也用过;但没有那个方法好用,因为.dpr其实也是一个窗口: begin Application.Title := 'PerRecord'; Application.Initialize; mHandle := Windows.CreateMutex(nil, true, 'PerRecord'); if mHandle <> 0 then begin if GetLastError = Windows.ERROR_ALREADY_EXISTS then begin fHandle := FindWindow('TfrmLogin', nil); if fHandle = 0 then fHandle := FindWindow('TfrmPer', nil); if fHandle <> 0 then begin ShowWindow(fHandle, SW_SHOW); SetForeGroundWindow(fHandle); end; Windows.ReleaseMutex(mHandle); Halt; end; end; Application.CreateForm(TdmPer, dmPer); Application.CreateForm(TfrmPer, frmPer); Application.Run; end.
2,搜一搜一大堆
procedure TForm1.Button1Click(Sender: TObject);
var
hw, hc: THandle;
begin
hw := FindWindow('TfrmFir','frmFir');
hc := FindWindowEx(hw, 0 ,'TEdit', 'Edit1');
SendMessage(hc, WM_GETTEXT, 0, Integer(PChar('跨程序控制')));
end;2.在Project1.dpr,程序截下来的,思路就是这样:
begin
Application.Initialize;
aHandle := FindWindow(nil, '秋风网页广告拦截器');
if aHandle <> 0 then
begin
if IsIconic(aHandle) then
SendMessage(aHandle, WM_SYSCOMMAND, SC_RESTORE, 0);
SetForeGroundWindow(aHandle);
Application.Terminate;
Exit;
end; Application.Title := '秋风网页广告拦截器';
Application.CreateForm(TfrmAdKiller, frmAdKiller);
Application.Run;
end.
这个是我写错了,改成楼上的PChar(s)就取到了s中,我改发送的没彻底,呵呵
MI_QUERYWINDOWHANDLE = 1;
MI_RESPONDWINDOWHANDLE = 2; MI_ERROR_NONE = 0;
MI_ERROR_FAILSUBCLASS = 1;
MI_ERROR_CREATINGMUTEX = 2;// Call this function to determine if error occurred in startup.
// Value will be one or more of the MI_ERROR_* error flags.
function GetMIError: Integer;implementationuses Forms, Windows, SysUtils;const
UniqueAppStr = 'DDG.I_am_the_Eggman!';var
MessageId: Integer;
WProc: TFNWndProc;
MutHandle: THandle;
MIError: Integer;function GetMIError: Integer;
begin
Result := MIError;
end;function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint):
Longint; stdcall;
begin
Result := 0;
// If this is the registered message...
if Msg = MessageID then
begin
case wParam of
MI_QUERYWINDOWHANDLE:
// A new instance is asking for main window handle in order
// to focus the main window, so normalize app and send back
// message with main window handle.
begin
if IsIconic(Application.Handle) then
begin
Application.MainForm.WindowState := wsNormal;
Application.Restore;
end;
PostMessage(HWND(lParam), MessageID, MI_RESPONDWINDOWHANDLE,
Application.MainForm.Handle);
end;
MI_RESPONDWINDOWHANDLE:
// The running instance has returned its main window handle,
// so we need to focus it and go away.
begin
SetForegroundWindow(HWND(lParam));
Application.Terminate;
end;
end;
end
// Otherwise, pass message on to old window proc
else
Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam);
end;procedure SubClassApplication;
begin
// We subclass Application window procedure so that
// Application.OnMessage remains available for user.
WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
Longint(@NewWndProc)));
// Set appropriate error flag if error condition occurred
if WProc = nil then
MIError := MIError or MI_ERROR_FAILSUBCLASS;
end;procedure DoFirstInstance;
// This is called only for the first instance of the application
begin
// Create the mutex with the (hopefully) unique string
MutHandle := CreateMutex(nil, False, UniqueAppStr);
if MutHandle = 0 then
MIError := MIError or MI_ERROR_CREATINGMUTEX;
end;procedure BroadcastFocusMessage;
// This is called when there is already an instance running.
var
BSMRecipients: DWORD;
begin
// Prevent main form from flashing
Application.ShowMainForm := False;
// Post message to try to establish a dialogue with previous instance
BSMRecipients := BSM_APPLICATIONS;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
@BSMRecipients, MessageID, MI_QUERYWINDOWHANDLE,
Application.Handle);
end;procedure InitInstance;
begin
SubClassApplication; // hook application message loop
MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
if MutHandle = 0 then
// Mutex object has not yet been created, meaning that no previous
// instance has been created.
DoFirstInstance
else
BroadcastFocusMessage;
end;initialization
MessageID := RegisterWindowMessage(UniqueAppStr);
InitInstance;
finalization
// Restore old application window procedure
if WProc <> Nil then
SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
if MutHandle <> 0 then CloseHandle(MutHandle); // Free mutex
end.
if GetLastError = ERROR_ALREADY_EXISTS then
begin
Exit;
end;避免重复运行,放在dpr 中,
begin
Application.Title := 'PerRecord';
Application.Initialize;
mHandle := Windows.CreateMutex(nil, true, 'PerRecord');
if mHandle <> 0 then
begin
if GetLastError = Windows.ERROR_ALREADY_EXISTS then
begin
fHandle := FindWindow('TfrmLogin', nil);
if fHandle = 0 then
fHandle := FindWindow('TfrmPer', nil);
if fHandle <> 0 then
begin
ShowWindow(fHandle, SW_SHOW);
SetForeGroundWindow(fHandle);
end;
Windows.ReleaseMutex(mHandle);
Halt;
end;
end; Application.CreateForm(TdmPer, dmPer);
Application.CreateForm(TfrmPer, frmPer);
Application.Run;
end.