如何在VC ++中将COPYDATASTRUCT的数据转换为可理解的字符串?

时间:2011-05-20 23:40:40

标签: c++ winapi sendmessage

我正在尝试将poCopyDataPCOPYDATASTRUCT)更改为字符串,也是将字符串转换回字符串的方法。所以我可以发送它,poCopyData是程序将来自另一个程序的消息复制到的地方。

PCOPYDATASTRUCT poCopyData = (PCOPYDATASTRUCT)ulParam;

// Note this is just outputs the message i don't how it converts it.
printf("Message (%u): %.*s\n", poCopyData->dwData, poCopyData->cbData, poCopyData->lpData);

1 个答案:

答案 0 :(得分:1)

inline LRESULT CopyData( HWND hwnd, DWORD message, const char* str )
{
  COPYDATASTRUCT copy_data = { message, strlen( str ) + 1, str };
  return SendMessage( hwnd, WM_COPYDATA, 0, reinterpret_cast< LPARAM >( &copy_data ) );
}

LRESULT WINAPI WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
  if( message == WM_COPYDATA )
  {
    COPYDATASTRUCT* copy_data = reinterpret_cast< COPYDATASTRUCT* >( lparam );

    const char* str = reinterpret_cast< const char* >( copy_data.lpData );

    printf( "Message (%u): %.*s\n", copy_data->dwData, str );
}