• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 704
  • Last Modified:

extra WM_COPY_DATA...... data....

Hi,
I'm trying to use WM_COPY_DATA to send a string between two of my applications. My sending application sends a string like:

        CString strMsg = "sdfsdfsdfsdd";
        COPYDATASTRUCT cpd;
        cpd.dwData = 0;
        cpd.cbData = strMsg.GetLength();
        cpd.lpData = (void*)strMsg.GetBuffer(cpd.cbData);
        LRESULT result = ::SendMessage(hwnd, WM_COPYDATA, (WPARAM)GetModuleHandle(NULL), (LPARAM)&cpd);

I dumped the string to a text file and it looks fine. On the receiving end, I usually get some garbage characters at the end which is problematic since I have to parse the string. I receive it like:

    BOOL CMainFrame::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
    {
        CString strReceived = (LPCSTR)(pCopyDataStruct->lpData);
    }

I mmediately here when debugging I can see some extra stuff at the back:

     éA~¨A~PÁ

I don't know where that came from. Any ideas how it came into existence?

Thanks
0
DJ_AM_Juicebox
Asked:
DJ_AM_Juicebox
  • 3
  • 3
  • 2
2 Solutions
 
DJ_AM_JuiceboxAuthor Commented:
Although there must be a way to send the message cleanly, as a back up, can I use the cpd.dwData  member to record the exact number of intended characters and truncate on the receiving side to cleanup the garbage at the end?
0
 
AndyAinscowCommented:
You will need to use the length of the string (you pass that also) to determine how much to put into the CString.

eg something like

CString s;
_tcsncpy(s, (LPCSTR)(pCopyDataStruct->lpData), cpd.cbData);
0
 
DJ_AM_JuiceboxAuthor Commented:
Andy, what do I replace?

Thanks
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jkrCommented:
... or simply make that

CString strReceived;

strReceived.Format ("%*.*s", pCopyDataStruct->cbData, pCopyDataStruct->cbData, (LPCSTR)(pCopyDataStruct->lpData);
0
 
AndyAinscowCommented:
BOOL CMainFrame::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
    {
        CString strReceived = (LPCSTR)(pCopyDataStruct->lpData);
    }

with something like
BOOL CMainFrame::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
    {
CString s;
_tcsncpy(s, (LPCSTR)(pCopyDataStruct->lpData), cpd.cbData);
    }
0
 
DJ_AM_JuiceboxAuthor Commented:
@jkr
Is that a typo, you have two "pCopyDataStruct->cbData" in the format statement, or is that the way it's supposed to be?

@Andy
Is that the exact way it should be in your example (or does it still need modification, just confused because you said 'something like')

Thanks
0
 
jkrCommented:
>>Is that a typo

No, there are to '*' format specs in the string (min and max), thus twice the length parameter.
0
 
AndyAinscowCommented:
This should work

      CString s(_T(' '), cpd.cbData);  //make a string of blanks of the correct length to receive the data
      _tcsncpy(s.GetBuffer(cpd.cbData), (LPCSTR)(pCopyDataStruct->lpData), cpd.cbData);  //copy the data
      s.ReleaseBuffer();  //release the buffer for further usage
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now