问一个很简单的问题,惭愧啊!55555(50分)

  • 主题发起人 主题发起人 sw
  • 开始时间 开始时间
S

sw

Unregistered / Unconfirmed
GUEST, unregistred user!
在C++Builder中,怎样用PostMessage()发送一个字符串?又怎样接收呢?<br><br>我是这样做的:<br><br>&nbsp; 发送:<br>&nbsp; &nbsp; char* aa = "aksdjfkdjkfjdkf";<br>&nbsp; &nbsp; PostMessage(MyAqua,WM_USER, 4, (LPARAM)(aa));<br>&nbsp; 接收:<br>&nbsp; &nbsp; Caption = (char*)(Message.LParam);<br><br>&nbsp; 上面这样做没问题,可是下面这样就不行,为什么?<br><br>&nbsp; 发送:<br>&nbsp; &nbsp; AnsiString aa = "dfdfdfdsfsd";<br>&nbsp; &nbsp; PostMessage(MyAqua,WM_USER, 4, (LPARAM)(aa.c_str()));<br>&nbsp; 接收:<br>&nbsp; &nbsp; Caption = (char*)(Message.LParam);<br><br>到底应该怎么做呀??!!
 
可以使用WM_COPYDATA,以下是MSDN的帮助<br>An application sends the WM_COPYDATA message to pass data to another application. <br><br>To send this message, call the SendMessage function with the following parameters (do not call the PostMessage function). <br><br>SendMessage( <br>&nbsp; (HWND) hWnd, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// handle to destination window <br>&nbsp; WM_COPYDATA, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// message to send<br>&nbsp; (WPARAM) wParam, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// handle to window (HWND)<br>&nbsp; (LPARAM) lParam &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // data (PCOPYDATASTRUCT)<br>);<br>Parameters<br>wParam <br>Handle to the window passing the data. <br>lParam <br>Pointer to a COPYDATASTRUCT structure that contains the data to be passed. <br>Return Values<br>If the receiving application processes this message, it should return TRUE; otherwise, it should return FALSE. <br><br>Remarks<br>The data being passed must not contain pointers or other references to objects not accessible to the application receiving the data. <br><br>While this message is being sent, the referenced data must not be changed by another thread of the sending process. <br><br>The receiving application should consider the data read-only. The lParam parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by lParam. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer. <br><br>When you send a WM_COPYDATA message, SendMessage allocates a block of memory cbData bytes in size and copies the data from the caller's address space to this block. It then sends the message to the destination window. When the receiving window procedure processes this message, the lParam parameter is a pointer to a COPYDATASTRUCT structure that exists in the address space of the receiving process. The lpData member is a pointer to the copied block of memory, and the address reflects the memory location in the receiving process's address space. <br><br>
 
或者用FileMapping
 
首先,感谢各位的热心帮助!<br>用WM_COPYDATA的方法我早就知道,也会做,代码如下:<br>发送消息:<br>&nbsp; &nbsp; &nbsp; if(ParamCount() &gt; 0)<br>&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; int i;<br>&nbsp; &nbsp; &nbsp; &nbsp; AnsiString fPath = "";<br>&nbsp; &nbsp; &nbsp; &nbsp; for(i=1; i &lt; ParamCount(); i++)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fPath += ParamStr(i) + " ";<br>&nbsp; &nbsp; &nbsp; &nbsp; fPath += ParamStr(i);<br>&nbsp; &nbsp; &nbsp; &nbsp; char* TempStr = new char[fPath.Length()+1];<br>&nbsp; &nbsp; &nbsp; &nbsp; StrPCopy(TempStr, fPath);<br>&nbsp; &nbsp; &nbsp; &nbsp; TCopyDataStruct SendData;<br>&nbsp; &nbsp; &nbsp; &nbsp; SendData.cbData = fPath.Length()+1;<br>&nbsp; &nbsp; &nbsp; &nbsp; SendData.lpData = TempStr;<br>&nbsp; &nbsp; &nbsp; &nbsp; SendMessage(MyAqua, WM_COPYDATA, 0, (LPARAM)(&amp;SendData));<br>&nbsp; &nbsp; &nbsp; &nbsp; delete []TempStr;<br>&nbsp; &nbsp; &nbsp; }<br>接收消息:<br>&nbsp; &nbsp; &nbsp; void __fastcall TfrmMainForm::RelateOpenFile(TMessage &amp;Meg)<br>&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; Caption = (char*)(((TCopyDataStruct*)(Meg.LParam))-&gt;lpData);<br>&nbsp; &nbsp; &nbsp; }<br><br>但正象我题目中说到的那样,用PostMessage()也是完全可行的,或许是我没用好,<br>所以,还请知道怎么用PostMessage()的大侠快快出手,帮小弟解决这个问题,谢谢!
 
shi兄别来无恙?现何处高就?<br>这个问题我也遇到过,SendMessage发过去的字符串接受正常,PostMessage的就不行,一分析,显然<br>是PostMessage立即返回,字符串自动释放,自然收到的是乱七八糟;解决办法:<br>&nbsp; &nbsp;1.发送方用全局变量,PostMessage后也不会释放<br>&nbsp; &nbsp;2.发送方GetMem,接受方FreeMem<br>shi兄不妨一试:)
 
接受答案了.
 
shi兄好久不来,来了也不留一句话[8D]
 
后退
顶部