简体   繁体   English

MFC - 使用PostMessage从DLL传递char缓冲区

[英]MFC - Passing char buffer with PostMessage From DLL

I have inherited an old-school MFC Windows CE program, and am having to make some modifications to it. 我继承了一个老式的MFC Windows CE程序,我不得不对它进行一些修改。 As part of this I have to pass additional data between disparate parts. 作为其中的一部分,我必须在不同的部分之间传递额外的数据。 What we have is a Main program that calls into a DLL. 我们有一个调用DLL的主程序。 The DLL starts a background process that acts as a TCP server. DLL启动后台进程,充当TCP服务器。 When that TCP server received data I need to push it all the way back up to the Main program. 当该TCP服务器收到数据时,我需要将其一直推回到主程序。

After some research it seems the WINAPI PostMessage function (see FIGURE 1) is the way to go. 经过一些研究后,似乎WINAPI PostMessage功能(见图1)是可行的方法。 When Main launches the DLL it passes in its own HWND. 当Main启动DLL时,它会在自己的HWND中传递。 After the spawned TCP server process receives the data it calls PostMessage with (1) this saved HWND (2) a message ID, (3) wParam is the length of the received data, and (4) the lParam is a pointer to the received data itself. 在生成的TCP服务器进程接收到它调用PostMessage的数据后,用(1)这个保存的HWND(2)消息ID,(3)wParam是接收数据的长度,(4)lParam是指向接收的指针数据本身。 Pseudocode for both the DLL thread caller, and the main code called, are shown below in FIGURES 2 and 3. DLL线程调用者和被调用的主代码的伪代码如下面的图2和3所示。

What I'm seeing is that the function IS called in my RemoteControlTCPMsg function, and the length is correct, BUT my string data is not correct -- weird, corrupted data. 我所看到的是在我的RemoteControlTCPMsg函数中调用函数IS,并且长度是正确的,但我的字符串数据不正确 - 奇怪,损坏的数据。 The way I'm packaging the string data is mixed and matched from a few examples, but apparently something was lost in translation. 我打包字符串数据的方式是混合和匹配的几个例子,但显然在翻译中丢失了一些东西。

Could someone please provide a line or two of code that would properly preserve a char buffer when passed via PostMessage from a DLL thread to a main app such as mine? 有人可以提供一行或两行代码,当通过PostMessage从DLL线程传递到我的主应用程序时,它会正确保留char缓冲区吗?

Thanks VERY much, I'm sort of at my wits end here. 非常感谢,我在这里结束了我的智慧。 Any thoughts/insight are appreciated. 任何想法/见解都表示赞赏。


*** FIGURE 1: PostMessage doc from MSDN ***图1:来自MSDN的PostMessage文档

BOOL WINAPI PostMessage(
  _In_opt_  HWND hWnd,
  _In_      UINT Msg,
  _In_      WPARAM wParam,
  _In_      LPARAM lParam
);

( https://msdn.microsoft.com/en-us/library/windows/desktop/ms644944%28v=vs.85%29.aspx ) https://msdn.microsoft.com/en-us/library/windows/desktop/ms644944%28v=vs.85%29.aspx


*** FIGURE 2: Pseudocode for the calling code in the DLL background thread: ***图2:DLL后台线程中调用代码的伪代码:

#define RC_COMMAND_BUFFER_SIZE = 256;
char m_Cmd[RC_COMMAND_BUFFER_SIZE];
int m_CmdLen = 0;

(when event occurs, m_Cmd and and m_CmdLen are populated and this is called:) (当事件发生时,填充m_Cmd和m_CmdLen并调用它:)

PostMessage(m_hWnd, MSG_ID, m_CmdLen,(LPARAM)(new CString(m_Cmd, m_CmdLen)));

*** FIGURE 3: Code for the receiving code in the main code: ***图3:主代码中接收代码的代码:

int CWAMPropertySheet::RemoteControlTCPMsg(WPARAM wParam, LPARAM lParam)
{

int length = = (int)wParam;
CString * rx_string = (CString*) lParam;

// handler code for the received text data and length

}

What makes me nervous about your post is that you have the tag "multithreading". 让我对你的帖子感到紧张的是你有“多线程”标签。 It makes me nervous about CStrings being allocated and used from different threads. 让我感到紧张的是CStrings被分配并从不同的线程中使用。 For that reason, if it was me, I would pass around BSTRs using SysAllocString to allocate the string from the code posting the message, and SysFreeString to free the string from the code receiving the message. 出于这个原因,如果是我,我将使用SysAllocString传递BSTR来从发布消息的代码中分配字符串,并使用SysFreeString从接收消息的代码中释放字符串。 My caveat is that I don't know if those APIs are supported on WinCE. 我的警告是,我不知道WinCE是否支持这些API。

Thanks for all the responses! 感谢所有的回复!

I was able to get the initial issue solved by casting a char * to (LPARAM) before sending it through PostMessage(). 在通过PostMessage()发送char *之前,我能够通过将char *转换为(LPARAM)来解决初始问题。 I was able to access the data using the following code: 我能够使用以下代码访问数据:

char * rx_string = (char *) lParam;

I then hit an issue trying to send code a message back from the main program. 然后我尝试从主程序发回代码消息。 I ended up using shared memory and critical sections to end up sending messages back, and this would have worked instead of trying to use PostMessage() in the first place. 我最终使用共享内存和关键部分来结束发送消息,这本来是有效的,而不是首先尝试使用PostMessage()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM