CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    Communication by WM_COPY (first chance exception)

    Hi,
    I have two applications , one is written in MFC and other is written in managed and unmanaged mix code of C++/CLI . MFC application send the data to CLI application. But after one or two times communication it crashed with first chance exception. Maybe i am missing something which you guys can point me for its correction...

    MFC Application's Code which communicate
    Code:
    #define _WM_HEADER_SIZE	(2*sizeof(DWORD)+sizeof(HWND)+sizeof(ULONGLONG))
    
    typedef struct _WM_DATASTRUCTURE{
    	DWORD cbSize;
    	DWORD iMessage;
    	HWND hClient;
    	ULONGLONG address;
    	_TCHAR Data[_WM_MAXMESSAGE_SIZE - _WM_HEADER_SIZE];	
    }WM_DATASTRUCTURE,*LPWM_DATASTRUCTURE;
    
    
    
    
    void SendDataToCLIApp()
    {
    HWND hd = ::FindWindow(L"CLIAPP",L"CLIAPPS");
    
    		if(!hd)
    		{
    			
    			ShellExecute(NULL,L"open",L"CLIAPP.exe",L"-tRun", NULL, SW_SHOWNORMAL);
    			Sleep(4000);
    		}
    		
    		
    		if(hd)
    		{
    			strBuff+="\0";
    			TransferData(hd,MSG_SENDFILE,strBuff.GetBuffer(),(STRLEN(strBuff.GetBuffer())*2));
    		}
    }
    
    BOOL TransferData(HWND hd,DWORD dwMsg, const _TCHAR *Buffer, DWORD dwBytes) 
    {
      BOOL bSend;
      COPYDATASTRUCT cpStructData;
      
      dwBytes+=4;
      cpStructData.cbData = dwBytes + _WM_HEADER_SIZE;
      HLOCAL hcl = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT,cpStructData.cbData);
      lpMsg = (LPWM_DATASTRUCTURE)hcl;
      lpMsg->iMessage = dwMsg;
      lpMsg->hClient = NULL;
      lpMsg->cbSize = dwBytes;
      lpMsg->address = m_devices[m_iSelectedDeviceIndex].Address.ullLong;
      cpStructData.lpData = lpMsg; 
      if(Buffer!=NULL){
    	  MEMCPY(lpMsg->Data,Buffer,dwBytes);
    	  lpMsg->Data[dwBytes-1]='\0';
      }
    
    
      bSend = SendMessage(hd,WM_COPYDATA, (WPARAM)hd,(LPARAM)&cpStructData);
    
      
      return(bSend);
    }

    And here is CLI application which response

    Code:
    LPWM_DATASTRUCTURE lpMsg;
    
    	switch (message)
    	{
    	
    	case WM_PAINT:
    		hdc = BeginPaint(hWnd, &ps);
    		// TODO: Add any drawing code here...
    		EndPaint(hWnd, &ps);
    		break;
    	case WM_DESTROY:
    		{
    			g_Log->WRITE(L"WM_DESTROY",L"Close Message Came");
    			PostQuitMessage(0);
    		}
    		break;
    
    		
    	case WM_COPYDATA:
    		{
    	
    		 g_Log->WRITE(L"WndProc",L"CopyData Called");
    		 lpMsg = (LPWM_DATASTRUCTURE)((COPYDATASTRUCT*)lParam)->lpData;
    		 g_Log->WRITE(L"GOT DATA",lpMsg->Data);
    
                    wstring strData = lpMsg->Data;
    
    		g_CurrentAddress = lpMsg->address;
    
        ..............process data .......................
    
    if((lpMsg->Data==NULL)||(lpMsg->Data==INVALID_HANDLE_VALUE))
    							LocalFree((HLOCAL)lpMsg);
                     }
    break;

    Thanks in advance

  2. #2
    Join Date
    Feb 2007
    Location
    Craiova, Romania
    Posts
    326

    Re: Communication by WM_COPY (first chance exception)

    You should not free the message buffer in the C++/CLI application. From MSDN:
    The receiving application should consider the data read-only. The lParam parameter is valid only during the processing of the message. The receiving application should not free the memory referenced by lParam. If the receiving application must access the data after SendMessage returns, it must copy the data into a local buffer.

  3. #3
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    Re: Communication by WM_COPY (first chance exception)

    Thanks for pointing me that , but still it gives me error...do you think i should free this memory in the MFC app. But when i use LocalFree in MFC right after SendMessage to CLI , it get crashed with exception. what you think???
    Last edited by Wolvorine; January 7th, 2009 at 06:24 PM.

  4. #4
    Join Date
    Aug 2004
    Location
    Pakistan
    Posts
    260

    Re: Communication by WM_COPY (first chance exception)

    I am using LocalFree right after SendMessage


    bSend = SendMessage(hd,WM_COPYDATA, (WPARAM)hd,(LPARAM)&cpStructData);

    LocalFree((HLOCAL)lpMsg);

    but its getting crash , any idea how can solve this???

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured