限制应用程序只运行一次

作者在 2006-09-06 18:57:00 发布以下内容

32位操作系统中,可以用两种方法实现限制应用程序只运行一次

第一种:

首先在global external functions声明外部函数如下:
FUNCTION long FindWindowA( ulong Winhandle, string wintitle ) Library ″user32″

然后在application的 Open 事件中加入如下代码:
ulong l_handle, lu_class
string ls_name
ls_name = ″我的系统″                     // 此处ls_name为系统主窗口的标题Title
l_handle = FindWindowA(lu_class, ls_name)
if l_handle > 0 then
   MessageBox(″提示信息″, ″应用程序″ + This.AppName + ″已经运行,不能多次启动!″)
   Halt Close
else
   open(w_main)                           //此处为系统主窗口
end if

这种方法是PowerBuilder联机帮助中的一个例子,是以系统主窗口的标题Title作为判别依据,若有其它与此Title同名应用程序在运行,再想启动此程序也会报应用程序已经运行。你可以将Title设为“计算器”,然后启动Windows附件中计算器程序,再运行你的PB应用程序试试。


第二种:

声明外部函数:
function ulong CreateMutexA (ulong lpMutexAttributes, int bInitialOwner, ref string lpName) library ″kernel32.dll″
function ulong GetLastError () library ″kernel32.dll″

然后在application的 Open 事件中加入如下代码:
ulong ll_mutex, ll_err
string ls_mutex_name
if handle (GetApplication (), false) <> 0 then
   ls_mutex_name = this.AppName + char (0)
   ll_mutex = CreateMutexA (0, 0, ls_mutex_name)
   ll_err = GetLastError ()
   if ll_err = 183 then                               //程序已经运行
      MessageBox (″提示信息″, ″程序已经运行了!″)
      Halt close
   else                                               //程序未运行
      open(w_main)
   end if
else                                                 //开发模式
   open(w_main)
end if

这种方法必须在应用程序编译成可执行文件.exe后才有效.

默认分类 | 阅读 1561 次
文章评论,共0条
游客请输入验证码
浏览48015次