Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 软件时空 > 软件相关 > Delphi防止程序被重复执行
【标  题】:Delphi防止程序被重复执行
【关键字】:Delphi
【来  源】:http://blog.csdn.net/yeagen/archive/2007/01/10/1479114.aspx

Delphi防止程序被重复执行

Your Ad Here

复制到新的unit中,引用到自己的程序中即可

unit MultInst;

interface

const
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;

implementation

uses 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.
 

中文域名投资渐成我国第四次投资热潮:【上一篇】
关闭135和445端口。:【下一篇】
【相关文章】
  • delphi常用数据库连接
  • Mastering Delphi7 阅读笔记(摘要) 续
  • 用Delphi Client和TUXEDO进行文件传送
  • 妙用Delphi编译器开关为程序增加便捷的控制台调试/日志窗口
  • [Delphi]发布一个自己写的家庭光碟管理系统-开放源代码(Delphi)
  • Mastering Delphi7 阅读笔记(摘要) 前八章
  • 自录DELPHI.NET 中ECO视频教程下载新地址
  • Delphi中通过CreateObject创建的 COM 对象如何获取事件
  • CodeSite 4.0.1 在 Delphi 7.0中的简单使用
  • Delphi中使用资源文件的一点记录
  • 【随机文章】
  • 线程堆:给你的线程命名,察看你的系统
  • Announcement: 新书《Visual Studio Team System学习指南》
  • 在 Cell BE 处理器上编写高性能的应用程序,第 1 部分: 简介 PLAYSTATION 3 上的 Linux
  • 鼠标放在一个连接上,会显示图片(类似tooltip)
  • 十天学会php
  • 删除WINDOWS漏洞文件名 com1. 还原加密数据
  • 空中打击2 官方秘籍
  • Smartcard 讀寫器函數庫的使用說明
  • 打包一个使用SOAP应用的客户端需要什么?
  • 联盟战略的形成与发展机制
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.