求救!在DLL中建立共享内存后,创建一子进程,再创建一个子窗口来向该子进程发送消息,结果是子进程收不到消息,都调了几天了,请大伙帮忙看看!
问题一.发送自定义消息语句到底要写在哪里? 是不是WNDPROC函数里?
************DLL********
#include <stdio.h>
#include <string.h>
#include <iostream>
#define WM_COMM (WM_USER+10)
#define WM_MAP_OPEN (WM_USER+11)
#include <tchar.h>//#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <Windows.h>HWND parent_hWnd;
HWND child_hWnd0;
HINSTANCE parent_hInst;LRESULT WINAPI MsgProcWindow0( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );struct gxnc{
DWORD handle;
char devname1[32];
int port1;
char devname2[32];
int port2;
char devname3[32];
int port3;
char openpioneer[512];
char closepioneer[256];
char savefilename[512];
DWORD handle_pionner;
};
//对外开放的接口
int xxxCreateWindow()
{ HANDLE hSockSrvRecMap;
LPBYTE lpData;
//char DataBuf[128];
struct gxnc c;
struct gxnc *p=&c;c.handle=NULL;strcpy(c.devname1,"first");
c.port1=1;strcpy(c.devname2,"second");
c.port2=2;strcpy(c.devname3,"third");
c.port3=3;strcpy(c.openpioneer,"eeeee");strcpy(c.closepioneer,"fffff");strcpy(c.savefilename,"ggggg");
c.handle_pionner=NULL;
hSockSrvRecMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, 2048, "SockSrvDataMap");
if (hSockSrvRecMap != NULL)
{
lpData = (LPBYTE)MapViewOfFile(hSockSrvRecMap, FILE_MAP_WRITE, 0, 0, 0);
if (lpData == NULL)
{
CloseHandle(hSockSrvRecMap);
hSockSrvRecMap = NULL;
}
}
/*HWND hDeCode =::FindWindow(NULL, "xiaoxi");
if (hDeCode != NULL)
::PostMessage(hDeCode, WM_MAP_OPEN, (WPARAM)0, (LPARAM)0);
*/
memcpy(lpData,p,sizeof(c));
STARTUPINFO si;
PROCESS_INFORMATION pi;ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
//C:\Program Files\Microsoft Visual Studio\MyProjects\xiaoxi\Debug
if( !CreateProcess( NULL, // No module name (use command line).
"C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\xiaoxi\\Debug\\xiaoxi.exe", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
//ErrorExit( "CreateProcess failed." );
}// Wait until child process exits.
WaitForSingleObject( pi.hProcess, 10);// Close process and thread handles.
//CloseHandle( pi.hProcess );
//CloseHandle( pi.hThread ); parent_hWnd =GetForegroundWindow();
parent_hInst =GetModuleHandle(NULL);
WNDCLASSEX wcex0 ={sizeof(WNDCLASSEX),CS_HREDRAW | CS_VREDRAW,MsgProcWindow0,0,0,parent_hInst,LoadIcon(NULL,IDI_INFORMATION),LoadCursor(NULL, IDC_ARROW),(HBRUSH)GetStockObject(BLACK_BRUSH),NULL,_T("Window0"),NULL};
RegisterClassEx( &wcex0 );
child_hWnd0 =NULL;
child_hWnd0 = CreateWindow( _T("Window0"), _T("Window0"), WS_OVERLAPPEDWINDOW|WS_CHILD, 50, 50, 200, 200,parent_hWnd, NULL,wcex0.hInstance, NULL );
if (!child_hWnd0 )
{
MessageBox(NULL,_T("子窗口创建失败"),_T("Error"),MB_OK);
}
MSG msg;
ShowWindow( child_hWnd0, SW_SHOWDEFAULT );
UpdateWindow( child_hWnd0 );
ZeroMemory( &msg, sizeof(msg) );
/*
HWND hDeCode =::FindWindow(NULL, "xiaoxi");
if (hDeCode != NULL)
::PostMessage(hDeCode, WM_MAP_OPEN, (WPARAM)0, (LPARAM)0);
*/
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
UnregisterClass( _T("Window0"), wcex0.hInstance );
return 1;
}
LRESULT WINAPI MsgProcWindow0( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
HWND hDeCode =::FindWindow(NULL, "xiaoxi");
if (hDeCode != NULL)
::PostMessage(hDeCode, WM_MAP_OPEN, (WPARAM)0, (LPARAM)0);
// HWND hWndApp;
// hWndApp= FindWindow(NULL, "xiaoxi");
// SendMessage(hWndApp,WM_COMM,(WPARAM)2,0);
SendMessage(hDeCode,WM_COMM,(WPARAM)2,0);
switch( msg )
{ case WM_CLOSE: DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
问题一.发送自定义消息语句到底要写在哪里? 是不是WNDPROC函数里?
************DLL********
#include <stdio.h>
#include <string.h>
#include <iostream>
#define WM_COMM (WM_USER+10)
#define WM_MAP_OPEN (WM_USER+11)
#include <tchar.h>//#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <Windows.h>HWND parent_hWnd;
HWND child_hWnd0;
HINSTANCE parent_hInst;LRESULT WINAPI MsgProcWindow0( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );struct gxnc{
DWORD handle;
char devname1[32];
int port1;
char devname2[32];
int port2;
char devname3[32];
int port3;
char openpioneer[512];
char closepioneer[256];
char savefilename[512];
DWORD handle_pionner;
};
//对外开放的接口
int xxxCreateWindow()
{ HANDLE hSockSrvRecMap;
LPBYTE lpData;
//char DataBuf[128];
struct gxnc c;
struct gxnc *p=&c;c.handle=NULL;strcpy(c.devname1,"first");
c.port1=1;strcpy(c.devname2,"second");
c.port2=2;strcpy(c.devname3,"third");
c.port3=3;strcpy(c.openpioneer,"eeeee");strcpy(c.closepioneer,"fffff");strcpy(c.savefilename,"ggggg");
c.handle_pionner=NULL;
hSockSrvRecMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, 2048, "SockSrvDataMap");
if (hSockSrvRecMap != NULL)
{
lpData = (LPBYTE)MapViewOfFile(hSockSrvRecMap, FILE_MAP_WRITE, 0, 0, 0);
if (lpData == NULL)
{
CloseHandle(hSockSrvRecMap);
hSockSrvRecMap = NULL;
}
}
/*HWND hDeCode =::FindWindow(NULL, "xiaoxi");
if (hDeCode != NULL)
::PostMessage(hDeCode, WM_MAP_OPEN, (WPARAM)0, (LPARAM)0);
*/
memcpy(lpData,p,sizeof(c));
STARTUPINFO si;
PROCESS_INFORMATION pi;ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
//C:\Program Files\Microsoft Visual Studio\MyProjects\xiaoxi\Debug
if( !CreateProcess( NULL, // No module name (use command line).
"C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\xiaoxi\\Debug\\xiaoxi.exe", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
//ErrorExit( "CreateProcess failed." );
}// Wait until child process exits.
WaitForSingleObject( pi.hProcess, 10);// Close process and thread handles.
//CloseHandle( pi.hProcess );
//CloseHandle( pi.hThread ); parent_hWnd =GetForegroundWindow();
parent_hInst =GetModuleHandle(NULL);
WNDCLASSEX wcex0 ={sizeof(WNDCLASSEX),CS_HREDRAW | CS_VREDRAW,MsgProcWindow0,0,0,parent_hInst,LoadIcon(NULL,IDI_INFORMATION),LoadCursor(NULL, IDC_ARROW),(HBRUSH)GetStockObject(BLACK_BRUSH),NULL,_T("Window0"),NULL};
RegisterClassEx( &wcex0 );
child_hWnd0 =NULL;
child_hWnd0 = CreateWindow( _T("Window0"), _T("Window0"), WS_OVERLAPPEDWINDOW|WS_CHILD, 50, 50, 200, 200,parent_hWnd, NULL,wcex0.hInstance, NULL );
if (!child_hWnd0 )
{
MessageBox(NULL,_T("子窗口创建失败"),_T("Error"),MB_OK);
}
MSG msg;
ShowWindow( child_hWnd0, SW_SHOWDEFAULT );
UpdateWindow( child_hWnd0 );
ZeroMemory( &msg, sizeof(msg) );
/*
HWND hDeCode =::FindWindow(NULL, "xiaoxi");
if (hDeCode != NULL)
::PostMessage(hDeCode, WM_MAP_OPEN, (WPARAM)0, (LPARAM)0);
*/
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
UnregisterClass( _T("Window0"), wcex0.hInstance );
return 1;
}
LRESULT WINAPI MsgProcWindow0( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
HWND hDeCode =::FindWindow(NULL, "xiaoxi");
if (hDeCode != NULL)
::PostMessage(hDeCode, WM_MAP_OPEN, (WPARAM)0, (LPARAM)0);
// HWND hWndApp;
// hWndApp= FindWindow(NULL, "xiaoxi");
// SendMessage(hWndApp,WM_COMM,(WPARAM)2,0);
SendMessage(hDeCode,WM_COMM,(WPARAM)2,0);
switch( msg )
{ case WM_CLOSE: DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
// xiaoxi.cpp : Defines the entry point for the application.
//#include "stdafx.h"
#include "resource.h"
#define WM_COMM (WM_USER+10)
#define WM_MAP_OPEN (WM_USER+11)
#define MAX_LOADSTRING 100
#include <stdio.h>#include <windows.h>#include <iostream>
using namespace std;
// Global Variables:
HINSTANCE hInst; // current instance
HWND h_pioneer;
struct gxnc *pa;
TCHAR d[100];
HWND xiaoxi=NULL;
HANDLE m_hReceiveMap;
unsigned char * m_lpbReceiveBuf=NULL;
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);struct gxnc{
DWORD handle;
char devname1[32];
int port1;
char devname2[32];
int port2;
char devname3[32];
int port3;
char openpioneer[512];
char closepioneer[256];
char savefilename[512];
DWORD handle_pionner;
};int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTable; // Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_XIAOXI, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
} hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_XIAOXI); // Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return msg.wParam;
}//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_XIAOXI);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_XIAOXI;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex);
}//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd)
{
return FALSE;
} ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
//strcpy(d,(*pa).devname1);}//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING]; LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
m_hReceiveMap = OpenFileMapping(FILE_MAP_READ, FALSE, "SockSrvDataMap");
if (m_hReceiveMap == NULL)
return 0;
m_lpbReceiveBuf = (LPBYTE)MapViewOfFile(m_hReceiveMap,FILE_MAP_WRITE,0,0,0);
if (m_lpbReceiveBuf == NULL)
{
CloseHandle(m_hReceiveMap);
m_hReceiveMap=NULL; switch (message)
{
// case WM_CREATE:
// break;
case WM_MAP_OPEN:
m_hReceiveMap = OpenFileMapping(FILE_MAP_READ, FALSE, "SockSrvDataMap");
if (m_hReceiveMap == NULL)
return 0;
m_lpbReceiveBuf = (LPBYTE)MapViewOfFile(m_hReceiveMap,FILE_MAP_WRITE,0,0,0);
if (m_lpbReceiveBuf == NULL)
{
CloseHandle(m_hReceiveMap);
m_hReceiveMap=NULL;
//printf("m_lpbReceiveBuf is null");
}
pa=(struct gxnc *) m_lpbReceiveBuf; case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
//strcpy(d,(*pa).devname1);
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
// DrawText(hdc, (*pa).devname1, strlen((*pa).devname1), &rt, DT_CENTER);
// EndPaint(hWnd, &ps);
break;
case WM_COMM:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
//strcpy(d,(*pa).devname1);
// RECT rt;
GetClientRect(hWnd, &rt); // strcpy(d,(*pa).devname1);
DrawText(hdc, (*pa).devname1, strlen((*pa).devname1), &rt, DT_CENTER);
if(wParam==2)
MessageBox(NULL,"ddd","ddd",MB_OK);
break;
case WM_DESTROY:
PostQuitMessage(0);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE; case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
if (hDeCode != NULL) //设置断点 查看窗口句柄是否有效 (可以在创建时把hWnd句柄传递进来)
查看消息响应/映射是否正确