请问高手:
   怎样才能使delphi编写的程序生成的可执行程序只能打开一个呢??
   即,比如当我打开了一个proxy.exe之后,如果我再想点击打开一个的时候,就让它提示说程序已经在运行,不可以在打开。怎样才能做到呢???
   拜托各位帮忙啦,谢谢啦!

解决方案 »

  1.   

    program Project1;uses
      Forms,windows,
      Unit1 in 'Unit1.pas' {Form1};var hw:hwnd;{$R *.RES}
    begin
      Application.Initialize;
      application.title:='test';//名字自己定义
      CreateMutex(nil, false, 'ADManager');
      if GetLastError <> ERROR_ALREADY_EXISTS then
      begin
        Application.CreateForm(TForm1, Form1);
        Application.Run;
      end;
    end.
      

  2.   

    可以用HprevinstHprevinst:=findwindow('主窗体名',nil);
    if Hprevinst <> 0 then
    begin
      ShowMessage('程序已经运行!');
      Application.Terminate;
    end;
      

  3.   

    program Project1;uses
      Forms,windows,
      Unit1 in 'Unit1.pas' {Form1};var hw:hwnd;{$R *.RES}
      Application.Initialize;
      mHandle := Windows.CreateMutex(nil, true, 'SendMessage');
      if mHandle <> 0 then
      begin
        if GetLastError = ERROR_ALREADY_EXISTS then
        begin
          fHandle := FindWindow(PChar('Tfrom1'), nil);
          ShowWindow(fHandle, SW_RESTORE);
          SetForeGroundWindow(fHandle);
          Windows.ReleaseMutex(mHandle);
          Halt;
        end;
      end;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
    end.
      

  4.   

    Tensionli:
       Hprevinst是什么啊??在formcreate中加入这一段不可以实现好像啊?
    cdsgajxlp(新手):
       请问,mHandle和fHandle是什么型的?string么??
       CreateMutex是个函数么??在help中怎样才能找到它的介绍呢??
      

  5.   

    mHandle和fHandle是hwmd型的对不??加到formcreate过程中还是不行啊?
      

  6.   

    hMutex:=CreateMutex(nil,False,Pchar('SchoolHome.exe'));
      Ret:=GetLastError;
      if Ret=ERROR_ALREADY_EXISTS Then
      begin
        ReleaseMutex(hMutex);
        ShowMessage('程序已经打开');
      end
      else
      begin
        ....
       Application.Run;
      end;
      

  7.   

    谢谢各位,我明白了,在view unit->选择工程文件查看源代码,添加,改写为
    program Project1;uses
      Forms,
      Windows,
      SysUtils,
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}Var
        hMutex:HWND;
        Ret:Integer;
    begin
      Application.Initialize;
      Application.Title := 'aaaaaa';
      hMutex:=CreateMutex(nil,False,'aaaaaa');
      Ret:=GetLastError;
      If Ret<>ERROR_ALREADY_EXISTS Then
      Begin
         Application.CreateForm(TForm1, Form1);
      Application.Run;
      End
      Else
        Application.MessageBox('程序已经运行','Notes!',MB_OK);
        ReleaseMutex(hMutex);
    end.