DWORD g_processID;   //进程句柄
DWORD g_ThreadID;    //线程句柄
CRITICAL_SECTION g_hCritical;  //windows 锁句柄(确切的应该叫关键代码段)
bool IsExsit(CString strExeFile)
{
   //加锁
 ::EnterCriticalSection(&g_hCritical);
    CString strFileName;
    bool bFound = false;
 
    PROCESSENTRY32 pe32 = {sizeof(pe32)};
    HANDLE hSnapShot = NULL;
   //系统内进行进程快照
 hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS|TH32CS_SNAPNOHEAPS, 0);
    if (hSnapShot == INVALID_HANDLE_VALUE)
 {
  //如果快照失败,释放锁
  ::LeaveCriticalSection(&g_hCritical);
        return bFound;
 }
    //遍历这些快照后的结果,直到找到我们需要的进程,如果没有找到就返回失败
    BOOL bFlag = ::Process32First(hSnapShot, &pe32);
    while (bFlag)
    {
        strFileName = CString(pe32.szExeFile);
  if (strFileName==strExeFile)
        {
            bFound = true;
   g_processID = pe32.th32ProcessID;   
            break;
        }
        bFlag = ::Process32Next(hSnapShot, &pe32);
    }
    //关闭系统快照
    ::CloseToolhelp32Snapshot(hSnapShot);
//释放锁 
::LeaveCriticalSection(&g_hCritical);
 return bFound;
}
     //首先查看该进程是否存在,同时找到该进程的进程id
    if(IsExsit(_T("myapp")))
{
     HWND hwnd=::FindWindow(NULL,_T("myapp"));
    if (hwnd)
    {
         //根据句柄查找到该窗口对应的线程
         g_ThreadID = GetWindowThreadProcessId(hwnd,&g_processID); 
         //发送消息
         ::PostThreadMessage(g_ThreadID,WM_KEYUP,13,13);
    }
}这段代码是我网络上找的 我的目标是 
1、首先采用DELPHI 编写,能否实现怎么实现.
2、其次实现的目标是 进程A给 进程B的一个C.DLL 发送自己定义消息 实现指定的消息发送和接收.
3、这个自定义消息  和通过 进程A给 进程B的一个C.DLL 进行TCP/IP通讯 来实现  哪个更好?
4、最后我想问这样对 CPU的占用影响大么?
分数我不会吝啬,但是希望大家认真思考 回答,谢谢了.通信自定义消息

解决方案 »

  1.   

    注入带窗体的dll,按键呼出这个窗口 之类的东东,论坛的资源里有很多嘛,搜一下就有了
    俺之前也做过例子,不过找不到在哪了,俺随便搜了一个
    http://download.csdn.net/detail/kkksi13996362600/1423618
      

  2.   

    这样通迅感觉有难度,我觉得可以简化处理,两个dll都读写一个ini配置文件就可以了。虽然不那么高技术,但达到目的就好。
      

  3.   

    如果读写的话不考虑同步么?如果是多个DLL和一个EXE呢?这样的话这种方式应该很容易混乱吧。
    如果是消息的话,有消息队列的。
      

  4.   

    已经搞定顺便把代码发下,有需要的自己去看吧。dll的
    library DllGame;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }uses
      SysUtils,
      windows,
      Classes,
      Messages,
    Dllform in 'Dllform.pas' {Form1};{$R *.res}
    varkeyhhk:HWND;
    Function GetMsgProc(icode: Integer;wp: WPARAM;lp: LPARAM):LRESULT;stdcall;
    begin
        if (icode=HC_ACTION)  then
        begin
          if PMSG(lp)^.message=WM_USER+600 then  //MessageBox(0,'显示','显示',0);
         begin
         if form1=nil then  Form1:=Tform1.Create(nil);
         form1.Visible:=not form1.Visible;
         end;
        end;
        Result:=CallNextHookEx(keyhhk,icode,wp,lp);
    end;
    Function SetHook(h:HWND):boolean;stdcall;
    var
     GameTid:THandle;
    begin
    Result:=false;
     if (windows.IsWindow(h)=false) then
       begin
       Messagebox(0,'无效窗口句柄','error',0);
       exit;
       end;//如果未打开则退出
       GameTid:=GetWindowThreadProcessId(h);
       keyhhk:=SetWindowsHookEx(WH_GETMESSAGE,@GetMsgProc,HInstance,GameTid);
       if keyhhk>0 then
       Result:=true;
    end;procedure DllEnterProc(reason:integer);
    begin
       case reason of
       windows.DLL_PROCESS_ATTACH: begin end;
       windows.DLL_PROCESS_DETACH: begin Form1.Free;form1:=nil; end;
       end;
    end;
    exports   //导出函数   SetHook;
    begindllProc:=@DllEnterProc;
    end.exe部分是这样的
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        btn1: TButton;
        btn2: TButton;
        procedure btn2Click(Sender: TObject);
        procedure btn1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      fhwid:Integer;
      SetHook:function (hwid:Integer):boolean;stdcall;
    implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
    begin
      fhwid:=FindWindow(nil,'Form2');
      PostMessage(fhwid,WM_USER+600,0,0);
    end;procedure TForm1.btn2Click(Sender: TObject);
    var
    hModule:THandle;
    Pcall:pointer;
    begin
    fhwid:=FindWindow(nil,'Form2');
    hModule:=LoadLibrary('dll.dll');
    Pcall:=GetProcAddress( hModule,'SetHook');
    SetHook:=Pcall;
    SetHook(fhwid);
    FreeLibrary(hModule);
    end;end.