现写了一个程序,只想让程序运行一次(已实现),但第二次运行所携带的参数如何传递给第一个实例??请说的详细些,最好是能有实例。谢谢。

解决方案 »

  1.   

    implementationuses
      DateUtils;var
      PData: PChar;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
      FHandle: LongWORD;
      PrvHWND: HWND;
      S: string;
      IsFirstRun: Boolean;
    begin
      Application.ShowMainForm := False;
      IsFirstRun := False;
      FHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'Test');
      if FHandle = 0 then // 共享内存区尚未建立,表明程序是第一次运行
      begin
        // 创建一个1024字节长度的共享内存区,大小可根据需要设定
        FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, 1024, 'Test');
        if FHandle = 0 then
          if GetLastError <> ERROR_ALREADY_EXISTS then Exit;
        IsFirstRun := True;
      end;
      PData := MapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); // 共享内存区的地址
      if PData = nil then
      begin
        CloseHandle(FHandle);
        Exit;
      end;
      if IsFirstRun then
        PInteger(PData)^ := Handle  // 若是第一次运行,填入自己的窗体句柄以供后续实例调用
      else
      begin                         // 反之则传递参数给第一个实例,然后中止运行
        PrvHWND := PInteger(PData)^;
        S := FormatDateTime('hh:mm:ss', Now);
        CopyMemory(@PData[4], @S[1], Length(S));
        PData[Length(S)+5] := #0;
        SendMessage(PrvHWND, WM_MY_ACTION, 0, 0);  // 提醒第一个实例,有参数到达
        SetForegroundWindow(PrvHWND);
        PostQuitMessage(0);         // 中止运行
        Exit;
      end;
      Application.ShowMainForm := True;
    end;procedure TForm1.MessageProc(var Msg: TMessage);
    begin
      Caption := PChar(@PData[5]);  // 从共享内存区中获取另一实例传来的参数
                                    // 这里是以一个当前时间字符串为例
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      UnmapViewOfFile(PData);
    end;
      

  2.   

    对不起,没贴全,在interface里还有一个const定义和窗体消息函数完整的程序:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;const
      WM_MY_ACTION = WM_USER + 1;type
      TForm1 = class(TForm)
        procedure FormDestroy(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure MessageProc(var Msg: TMessage); message WM_MY_ACTION;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses
      DateUtils;var
      PData: PChar;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
      FHandle: LongWORD;
      PrvHWND: HWND;
      S: string;
      IsFirstRun: Boolean;
    begin
      Application.ShowMainForm := False;
      IsFirstRun := False;
      FHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'Test');
      if FHandle = 0 then // 共享内存区尚未建立,表明程序是第一次运行
      begin
        // 创建一个1024字节长度的共享内存区,大小可根据需要设定
        FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, 1024, 'Test');
        if FHandle = 0 then Exit;
        IsFirstRun := True;
      end;
      PData := MapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); // 共享内存区的地址
      if PData = nil then
      begin
        CloseHandle(FHandle);
        Exit;
      end;
      if IsFirstRun then
        PInteger(PData)^ := Handle  // 若是第一次运行,填入自己的窗体句柄以供后续实例调用
      else
      begin                         // 反之则传递参数给第一个实例,然后中止运行
        PrvHWND := PInteger(PData)^;
        S := FormatDateTime('hh:mm:ss', Now);
        CopyMemory(@PData[4], @S[1], Length(S));
        PData[Length(S)+5] := #0;
        SendMessage(PrvHWND, WM_MY_ACTION, 0, 0);  // 提醒第一个实例,有参数到达
        SetForegroundWindow(PrvHWND);
        PostQuitMessage(0);         // 中止运行
        Exit;
      end;
      Application.ShowMainForm := True;
    end;procedure TForm1.MessageProc(var Msg: TMessage);
    begin
      Caption := PChar(@PData[5]);  // 从共享内存区中获取另一实例传来的参数
                                    // 这里是以一个当前时间字符串为例
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      UnmapViewOfFile(PData);
    end;end.
      

  3.   

    使用内存映象文件:CreateFileMapping、 OpenFileMapping、MapViewOfFile
      

  4.   

    就使用上面的Memory Map File技术就OK_____________________
    http://lysoft.7u7.net