简体   繁体   中英

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. 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. The DLL starts a background process that acts as a TCP server. When that TCP server received data I need to push it all the way back up to the Main program.

After some research it seems the WINAPI PostMessage function (see FIGURE 1) is the way to go. When Main launches the DLL it passes in its own 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. Pseudocode for both the DLL thread caller, and the main code called, are shown below in FIGURES 2 and 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. 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?

Thanks VERY much, I'm sort of at my wits end here. Any thoughts/insight are appreciated.


*** FIGURE 1: PostMessage doc from MSDN

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 )


*** FIGURE 2: Pseudocode for the calling code in the DLL background thread:

#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:)

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:

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. 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. My caveat is that I don't know if those APIs are supported on WinCE.

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(). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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