我采用的方法是程序运行时先查找有没有相同的运行了,如果有,就立刻退出程序.修改dpr项目文件,修改begin和end之间的代码如下:
begin
  Application.Initialize;
  if (FindWindow(nil,'Project1')=0) then
  begin
    //application.ShowMainForm:=false;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end;
end.但使用FindWindow这个方法时,第二个参数是选择'Project1'还是'Form1'?
 在使用'Project1'时,程序都没有启动,也就是说没有执行IF里面的代码,可任务管理器中并没有'Project1'这个进程
  使用'Form1'时,可以运行多个相同程序.
  哪位高手能否解决下!

解决方案 »

  1.   

    Form1
    别用这个方法吧,用互斥体吧
    ...........
    var
      hMutex: THandle;function MakeMutex: Boolean;
    begin
      hMutex := CreateMutex(nil, False, 'Form1Form1Form1Form1');
      Result := (GetLastError = 0) and (hMutex <> 0);
      if not Result then
        FreeMutex;
    end;procedure FreeMutex;
    begin
      if hMutex <> 0 then
      begin
        ReleaseMutex(hMutex);
        hMutex := 0;
      end;
    end;begin
      if MakeMutex then
      begin
        Application.Initialize;
        Application.CreateForm(TForm1, Form1);
        Application.Run;
        FreeMutex;
      end
      

  2.   

      谢谢啦```  马上有效果了!
       能否还详细介绍下CreateMutex方法!
      

  3.   

    楼主可以修改工程代码如下:var
       hMutex:HWND;
       ret:integer;
    begin
       application.initialize;
       application.title:='project name';
       hMutex:=createMutex(nil,False,'project name'); //创建互斥对象
       ret:=getlastError;
       if ret<>ERROR_ALREADY_EXISTS Then //没有发现互斥对象
       begin
          application.createform(Tform1,form1);
          application.run;
       end
       else
          application.messagebox('程序已在运行!','project name',mb_ok);
          releaseMutex(hMutex); //释放互斥对象
    end;
    CreateMutex函数原型:
    function createmutex(lpMutexAttributes:PSecurity Attributes;bInitialOwner:BOOL;lpName:PChar):THandle;
    参数说明:
    lpMutexAttributes  是一个PSecurity Attributes结构类型指针,可以设置为null
    bInitialOwner      是否初始化互斥对象
    lpName             互斥对象名称函数返回一个互斥对象的句柄。