Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

WM_COPYDATA need  to correct errors.any one help me

Posted on 2003-11-10
13
Medium Priority
?
543 Views
Last Modified: 2013-11-20
I have used a structure and the following code . i am getting error access violation when message is sent to destination window. i am not able find were the error lies. plz any one help  me .
typedef struct tagSII{
      long lsessionID;
      long lqueue;
                CString  sCommand;
}SII ;
//Sending window
CWnd *pPdmm;

LRESULT lresult;
COPYDATASTRUCT copy;
HANDLE hWnd(this);

SII sii;       
      
  pPdmm = CWnd::FindWindow(NULL,"di");
 if(pPdmm == NULL){
  MessageBox("Would not find",NULL,MB_OK);
 }
else{
      sii.lsessionID = 4;
      sii.lqueue     = 5;
      sii.sCommand ="ab";
            
      copy.dwData =500;
      copy.cbData = sizeof(sii);
      copy.lpData = &sii;
lresult =pPdmm->SendMessage(WM_COPYDATA,(WPARAM)(HWND)m_hWnd,(LPARAM)(LPVOID)   &copy);
            
      }

      if ( lresult== TRUE)
            MessageBox("posted",NULL,MB_OK);
      else
            MessageBox("Failed",NULL,MB_OK);

//Receiving window

BOOL CDiDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
      if( pCopyDataStruct->cbData == sizeof(SII ) )
            {
                  SII sii1;
                  SII sii2;
            memcpy( &sii1, pCopyDataStruct->lpData, sizeof( SII ) );
           sii2.lsessionID=sii1.lsessionID;
             sii2.lqueue    =sii1.lqueue;
              sii2.sCommand      =sii2.sCommand;
         
             MessageBox(sii1.sCommand);
             return TRUE;
            }
 return FALSE;

}

0
Comment
Question by:sarath83
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9720735
I'm not sure about the validity of doing the line

memcpy( &sii1, pCopyDataStruct->lpData, sizeof( SII ) );

This is all very well when copying basic data, but I'm fairly certain that this will not set up the CString correctly. You would be better off to do a cast instead, like this :

SII * pSII = (SII *)pCopyDataStruct->lpData
sii1.sCommand = pSII->sCommand; etc

Hope this helps
Martyn
0
 

Author Comment

by:sarath83
ID: 9720889
i am getting the error"memory cannot b read" when i send the message
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9720969
I've had a look at the documentation for WM_COPYDATA, and came across the line :

"The data being passed must not contain pointers or other references to objects not accessible to the application receiving the data. "

What this means is that your SII struct can't contain pointers to local memory, and I would imagine that it is the CString that is causing the problem. Instead, you could have a character buffer in SII instead of a CString, or you'd have to dynamically allocate a chunk of memory to hold your SII members and the string, and then copy the data into it for sending.

So, something like this :

long lBufferLength = sizeof(long) + sizeof(long) + sCommand.GetLength() + 1
BYTE * pBuffer = new BYTE(lBufferLength);

memcpy(pBuffer, &sii.lsessionID, sizeof(long));
memcpy(pBuffer, &sii.lqueue, sizeof(long));
strcpy((char *)&pBuffer[sizeof(long) + sizeof(long)], sCommand);

Then send pBuffer as the data.

All the best
Martyn


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:sarath83
ID: 9720996
is there any onther technique  available to do this
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9721022
When you set up the COPYDATASTRUCT structure, you pass it a pointer to some data. It doesn't matter what the data is, but it cannot include pointers to other data. So, in other words, all the data you want to be copied has to be within that structure - with no pointers to any other data.

So, one approach is the one that I have suggested above, where you allocate a buffer, place your data into it and send it. Another approach could be to replace your CString with a fixed length character array in SII. However, this is less flexible as the number of charcters you send is fixed, and of course there will always be an upper limit. If you are always sending the same number of characters, this is fine, but what if you could send anywhere between 1 and 64,000 characters - you wouldn't want to have an array of 64,000 chars as that would mean that you are copying 64k EVERY time - even if you are really only sending one character.

Martyn
0
 

Author Comment

by:sarath83
ID: 9721056
Then hw to  retrieve the data in destination window. I wish to send using CString .
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9721075
Assuming when sending the data, you have built up a buffer containing the longs, and the characters of the string, you can do the following to put your data back together :

BOOL CDiDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
    SII sii;
    memcpy( &sii1.lsessionID, pCopyDataStruct->lpData, sizeof(long) );
    memcpy( &sii1.lsessionID, pCopyDataStruct->lpData, sizeof(long) );
    sii1.sCommand = CString((const char *)&pCopyDataStruct->lpData[sizeof(long) + sizeof(long)]);

    return TRUE;
}


Martyn
0
 

Author Comment

by:sarath83
ID: 9721097
Can I use this lines to send data

                                copy.dwData =500;
            copy.cbData = sizeof(pBuffer);
            copy.lpData = &pBuffer;
            lresult =pPdmm->SendMessage(WM_COPYDATA,(WPARAM)(HWND)m_hWnd,(LPARAM) &pBuffer);
0
 

Author Comment

by:sarath83
ID: 9721166
please help me out hw to do this .i am trying this for a very long time.
0
 

Author Comment

by:sarath83
ID: 9721267
sii1.sCommand = CString((const char *)&pCopyDataStruct->lpData[sizeof(long) + sizeof(long)]);
for the above line i am getting error :

 C2036: 'void *' : unknown size
0
 
LVL 8

Accepted Solution

by:
martynjpearson earned 220 total points
ID: 9721439
Firstly, on the receive side, my code needs a couple of changes :

   BYTE * pData = (BYTE *)pCopyDataStruct->lpData;
 SII sii;
    memcpy( &sii1.lsessionID, pData, sizeof(long) );
    memcpy( &sii1.lqueue, &pData[sizeof(long)], sizeof(long) );
    sii1.sCommand = CString((const char *)&pData[sizeof(long) + sizeof(long)]);

    return TRUE;

That should solve your C2036 error.

For sending the data, your code needs to be like this :

copy.dwData =500;
copy.cbData = lBufferLength;
copy.lpData = pBuffer;
lresult =pPdmm->SendMessage(WM_COPYDATA,(WPARAM)(HWND)m_hWnd,(LPARAM)(LPVOID)   &copy);


Martyn
0
 

Author Comment

by:sarath83
ID: 9721523
Mr.Martyn Thanku very much . its working . Can u plz suggets me which books to follow to learn VC++ . I am a beginner.
0
 
LVL 8

Expert Comment

by:martynjpearson
ID: 9721557
No problems - glad it's working now!

With regards to books, that's a tricky one, as the ones that I learnt from are probably no longer in print!! I learnt C and C++ at university, and then when I started programming for Windows (using Visual C++ 4.0 on Windows 95!!!) I had a book published by Sams, called Essential Visual C++ 4.0. This was quite good in that it covered all the basics, like dialogs, documents etc, and then went on to cover the basics of graphics using brushes, pens etc.

In the past, I have found the Wrox books to be of consistently high quality - check out www.wrox.com - and they generally do books that are aimed at a particular level, such as beginner, intermediate, expert etc.

All the best!
Martyn
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question