WM_COPYDATA 进程间传递参数的问题

来源:互联网 发布:社交网络电影讲的什么 编辑:程序博客网 时间:2024/04/27 22:34
我们首先定义下面的结构,
  1. #define BUFFER_SIZE MAX_PATH*4
  2. // struct for loading report
  3. typedef struct _REPORT_INFO
  4. {
  5.     TCHAR reportFile[BUFFER_SIZE];
  6.     TCHAR schemaFile[BUFFER_SIZE];
  7.     TCHAR tablesFile[BUFFER_SIZE];
  8.     TCHAR dataXmlFile[BUFFER_SIZE];
  9.     TCHAR outputFile[BUFFER_SIZE];
  10. }REPORT_INFO,*PREPORT_INFO;
注意REPORT_INFO结构中的字符为字符数组,这样可以保证数据可以跨进程正确的传递,如果我们把REPORT_INFO修改为下面的样子:
  1. // struct for loading report
  • typedef struct _REPORT_INFO
  • {
  •     TCHAR* reportFile;
  •     TCHAR* schemaFile;
  •     TCHAR* tablesFile;
  •     TCHAR* dataXmlFile;
  •     TCHAR* outputFile;
  • }REPORT_INFO,*PREPORT_INFO;
  • 这样的数据结构在进程内是可以的,可以正确传递,但是如果是跨进程SendMessage,里面的reportFile等成员的内容不能正确传递。

    1. void SendMessage(TCHAR* reportFileName)
    2. {
    3.     REPORT_INFO info;
    4.     ZeroMemory( &info, sizeof(info) );
    5.     ::wcscpy_s(info.reportFile,BUFFER_SIZE,(TCHAR*)reportFileName);
    6.     COPYDATASTRUCT copy_struct;
    7.     ZeroMemory( ©_struct, sizeof(copy_struct) );
    8.     copy_struct.lpData = &info;
    9.     copy_struct.dwData = 0;
    10.     copy_struct.cbData = sizeof(info);
    11.     ::SendMessage((HWND)hwnd,WM_COPYDATA,(WPARAM)NULL,(LPARAM)©_struct);
    12. }

     

    原创粉丝点击