阅读:1854回复:4
DllMain中创建的窗体得不到SOCKET消息?!
我的一个程序,已经最小化了。
两个测试,代码99.99%相同,一个是Dll用LoadLibrary载入以后没有 发声,另一个Exe就正常地发出两声,跟踪了一下,Dll的那个窗体压根就没有得到任何WM_SOCKET_NOTIFY消息,我是按照介绍上把WM_SOCKET_NOTIFY设为WM_USER + 1的,有没有解决的办法。 // TestDLl.cpp : Defines the entry point for the DLL application. // #include \"stdafx.h\" #define WM_SOCKET_NOTIFY (WM_USER + 1) #include <TCHAR.H> #include <winsock2.h> #pragma comment(lib, \"ws2_32\") LRESULT CALLBACK MainWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { MSG msg; HWND hWnd; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = MainWndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = (struct HINSTANCE__ *)hModule; wndclass.hIcon = NULL; wndclass.hCursor = NULL; wndclass.hbrBackground = NULL; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = _T(\" \"); if(!RegisterClass(&wndclass)) return FALSE; hWnd = CreateWindow(_T(\" \"), _T(\"\"), WS_DISABLED, CW_USEDEFAULT, CW_USEDEFAULT, \\ CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, (struct HINSTANCE__ *)hModule, NULL); if(!hWnd) return FALSE; ShowWindow(hWnd,SW_HIDE); UpdateWindow(hWnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK MainWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { switch(message) { case WM_CREATE: { WSADATA wsaData; WSAStartup(MAKEWORD(2,1),&wsaData); sockaddr_in sa; SOCKET sNet = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(sNet != INVALID_SOCKET) { if(WSAAsyncSelect(sNet, hWnd, WM_SOCKET_NOTIFY, \\ FD_CONNECT | FD_CLOSE | FD_READ | FD_WRITE | FD_OOB) != SOCKET_ERROR) { sa.sin_family = PF_INET; sa.sin_port = htons(80); sa.sin_addr.S_un.S_addr = inet_addr(\"127.0.0.1\"); connect(sNet, (SOCKADDR *)&sa, sizeof(sa)); } } } case WM_SOCKET_NOTIFY: { switch(lParam) { case FD_CONNECT: Beep(500, 500); Beep(600, 500); break;//连上了 default: break; } } default: break; } return DefWindowProc(hWnd,message,wParam,lParam); } 但是用Exe就可以,就会响两声。 // testExe.cpp : Defines the entry point for the application. // #include \"stdafx.h\" #define WM_SOCKET_NOTIFY (WM_USER + 1) #include <TCHAR.H> #include <winsock2.h> #pragma comment(lib, \"ws2_32\") LRESULT CALLBACK MainWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // TODO: Place code here. MSG msg; HWND hWnd; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = MainWndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; // wndclass.hInstance = (struct HINSTANCE__ *)hModule; wndclass.hInstance = hInstance; wndclass.hIcon = NULL; wndclass.hCursor = NULL; wndclass.hbrBackground = NULL; wndclass.lpszMenuName = NULL; wndclass.lpszClassName = _T(\" \"); if(!RegisterClass(&wndclass)) return FALSE; hWnd = CreateWindow(_T(\" \"), _T(\"\"), WS_DISABLED, CW_USEDEFAULT, CW_USEDEFAULT, \\ CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if(!hWnd) return FALSE; ShowWindow(hWnd,SW_HIDE); UpdateWindow(hWnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK MainWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { switch(message) { case WM_CREATE: { WSADATA wsaData; WSAStartup(MAKEWORD(2,1),&wsaData); sockaddr_in sa; SOCKET sNet = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if(sNet != INVALID_SOCKET) { if(WSAAsyncSelect(sNet, hWnd, WM_SOCKET_NOTIFY, \\ FD_CONNECT | FD_CLOSE | FD_READ | FD_WRITE | FD_OOB) != SOCKET_ERROR) { sa.sin_family = PF_INET; sa.sin_port = htons(80); sa.sin_addr.S_un.S_addr = inet_addr(\"127.0.0.1\"); connect(sNet, (SOCKADDR *)&sa, sizeof(sa)); } } } case WM_SOCKET_NOTIFY: { switch(lParam) { case FD_CONNECT: Beep(500, 500); Beep(600, 500); break;//连上了 default: break; } } default: break; } return DefWindowProc(hWnd,message,wParam,lParam); } 跟踪了一下,Dll的那个窗体根本就没有任何WM_SOCKET_NOTIFY消息传入,又没有人能解决,xx。 [编辑 - 3/14/05 by pwpwpw123] [编辑 - 3/14/05 by pwpwpw123] [编辑 - 3/14/05 by pwpwpw123] |
|
沙发#
发布于:2005-03-15 08:31
有没有个监视系统消息的软件啊
|
|
板凳#
发布于:2005-03-15 08:48
每人能回答这个问题吗?
|
|
地板#
发布于:2005-03-15 11:13
有没有个监视系统消息的软件啊 SPY++,VC自带,Delphi带的是Winsight32 |
|
|
地下室#
发布于:2005-03-15 14:30
你已经陷在DllMain的消息循环中,无法返回true,你的DLL就无法被Windows加载成功
|
|
|