findwindow和创建互斥对象的方法;但是这两个办法的缺点就是对同一操作系统;
不同的登陆用户就可以重复启动这个程序;怎么样能保证操作系统只存在一个这样的进程;应该是启动程序的时候先到系统进程列表中去找是否存在这个进程;如果存在则不启动?如何做?
不同的登陆用户就可以重复启动这个程序;怎么样能保证操作系统只存在一个这样的进程;应该是启动程序的时候先到系统进程列表中去找是否存在这个进程;如果存在则不启动?如何做?
解决方案 »
- 怎么得到窗体按钮的坐标(x, y) ?
- 如何改变 delphi7 的默认字符集
- 怎么检测xp的外观为经典windows样式?
- 在QReport里,我用一张大小50K的JPG或BMP图片做背景,图片放在QRImage里,为什么只有开始两页有背景图而后面的就没有呢?
- 请问DELPHI中如何嵌入C语言程序??
- DBGrid控件排序问题,如有相关源代码,全加分!
- 请教c++格式的函数接口如何翻译到delphi
- [求助][求助]一个关于object pascal类析构和赋值的问题[求助][求助]
- 怎样动态的改变菜单的下拉项??
- 自编控件是否可以公布数组类型的属性?如何公布?
- 为什么我的全局鼠标钩子(内附源程序),在鼠标经过滚动条的时候什么消息都收不到? 请问怎样解决?
- 截获修改了的字段和内容,求助
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 MainFormprocedure 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;
CreateMutex创建互斥对象的方法和findwindow的API这两个方法对
远程登陆操作系统的两个不同用户根本不起作用
我登陆多少次就启动多少个
怎么后面的还说这两个愚笨的方法
我们也可以利用向系统添加全局原子的方法,来防止多个程序实例的运行。全局原子由Windows 系统负责维持,它能保证其中的每个原子都是唯一的,管理其引用计数,并且当该全局原子的引用计数为0时,从内存中清除。我们用GlobalAddAtom 函数向全局原子添加一个255个字节以内的字符串,用GlobalFindAtom来检查是否已经存在该全局原子,最后在程序结束时用GlobalDeleteAtom函数删除添加的全局原子。示例如下:
Uses Windows
const iAtom=‘SingleApp’;
begin
if GlobalFindAtom(iAtom)=0 then
begin
GlobalAddAtom(iAtom);
Application.Initialize;
Application.CreateForm(TForm1,Form1);
Application.Run;
GlobalDeleteAtom(GlobalFindAtom(iAtom));
end
else
MessageBox(0,‘You can not run a second copy of this App’,‘’,mb_OK);
end.
利用全局原子的引用计数规则,我们还可以判断当前共运行了该程序的多少个实例:
var i:Integer;
begin
I:=0;
while GlobalFindAtom(iAtom)<>0 do
begin
GlobalDeleteAtom(GlobalFindAtom(iAtom));
i:=i+1;
end;
ShowMessage(IntToStr(I));
end;
=====================================================================
设置一个变量记录(INI文件啊,注册表啊),在程序运行前进行判断,条件符合就运行,不符合就不运行。