We help IT Professionals succeed at work.
Get Started

MFC: How to pass ASCII data from DLL thread to main code

278 Views
Last Modified: 2015-02-25
Hi, experts.

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 callee, 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

}
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE