命名管道不同进程直接通信,接收到为空或乱码。
首先说一下我的项目,包括a.exe、b.ll。其中a.exe做一件事情,就是把b.dll注入到记事本程序之中。
现在的问题是我要在b.dll中得到a.exe的目录,如“C:\Users\Administrator\Desktop\xxx”(注:a.exe和b.ll同在该目录下)。 因为b.dll已经被注入到了记事本程序之中,用GetModuleFileName得到的只是“c:\windows\notepad.exe”,而我要的确是“C:\Users\Administrator\Desktop\xxx”。
因为是不同进程间通信,所以我用的命名管道。在a.exe中把目录“C:\Users\Administrator\Desktop\xxx”通过管道给b.dll.但是不知道什么原因,在b,dll中得到的要么为空,要么为乱码,请各位大神帮忙
a.exe之中
void ServerPipe(char catalogue[]) { DWORD dwTO = NMPWAIT_USE_DEFAULT_WAIT;//设置连接等待时间 HANDLE hSvr = CreateNamedPipe("\\\\.\\pipe\\test_pipe\\",PIPE_ACCESS_DUPLEX,PIPE_TYPE_BYTE,3,256,256,dwTO,NULL); if( INVALID_HANDLE_VALUE == hSvr) { AfxMessageBox("Error create/open pipe"); } else { if (ConnectNamedPipe(hSvr,NULL)) { char bRead[128]; DWORD dwRead,dwWritten; AfxMessageBox("服务器端准备写入数据"); WriteFile(hSvr,catalogue,129,&dwWritten,NULL); } else { AfxMessageBox("等待连接时出现错误"); } CloseHandle(hSvr); }}char cat[128]="C:\Users\Administrator\Desktop\xxx";ServerPipe(cat);
string ClientPipe() { HANDLE hClient = CreateFile("\\\\.\\pipe\\test_pipe\\",GENERIC_WRITE |GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if(hClient == INVALID_HANDLE_VALUE) { AfxMessageBox("打开命名管道失败"); } else { char bRead[128]; DWORD dwRead; if (ReadFile(hClient,bRead,128,&dwRead,NULL)) { AfxMessageBox("读取管道信息成功"); string s=bRead; return s; } else { AfxMessageBox("读取管道信息失败"); } CloseHandle(hClient);//close pipe }}string ss=ClientPipe();char MyHookPath[128];sprintf(MyHookPath,"%s",ss);MessageBox(NULL,MyHookPath,"当前路径",MB_OK);