Windows编程个人笔记
win32界面编程
Winmain() 是界面的入口函数
CreateWindow()是界面的展示函数。
通过CreateWindows()展示界面
调用ShowWindow()来展示。
DLL调用
HMODULE hDll =LoadLibraryA(DEF_DLL_NAME);//返回NULL的句柄表示调用失败。
//通过以下语句来获得dll中的函数指针。调用的函数需要是导出函数。
typedef void(*PFN_HOOKSTART)(DWORD a);
typedef void(*PFN_HOOKSTOP)();
HookStart = (PFN_HOOKSTART)GetProcAddress(hDll, DEF_HOOKSTART);
HookStop = (PFN_HOOKSTOP)GetProcAddress(hDll, DEF_HOOKSTOP);
消息处理机制
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
//可通过这个函数强制当前线程建立消息队列。
//下面是窗口消息处理的机制。
//消息的处理可以把需要的信号给自己建立逻辑,其他的扔给回调函数
while (GetMessage(&msg, NULL, 0, 0))
{
switch (msg.message)
{
case WM_USER:
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
这个是回调函数的格式,wParam一般是值,lParam一般是参数。
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
函数最后应返回以下值,意思是不处理的信号系统自动处理。
return DefWindowProc(hwnd, message, wParam, lParam);
常见的特殊的信号值
WM_CREATE//窗口实例
WM_PAINT//窗口绘制
WM_DESTROY//窗口销毁
WM_COMMAND//命令
常用函数
GetWindowThreadProcessId(hWinmine, &PID);
MessageBox(hwnd, L"获取PID失败", L"提示", MB_OK | MB_ICONINFORMATION);
Process = OpenProcess(PROCESS_ALL_ACCESS, 0, PID);
WriteProcessMemory(Process, (LPVOID)(food_address), &a, sizeof(DWORD),NULL);
下面语句调试时可以用。
wchar_t buffer[256];
wsprintfW(buffer, L"%d", pid);
MessageBox(NULL, buffer, L"提示", MB_OK | MB_ICONINFORMATION);
wsprintfW(buffer, L"%d", g_hInstance);
MessageBox(NULL,buffer, L"提示", MB_OK | MB_ICONINFORMATION);
HOOK 功能
常见的hook格式,PID为0则为全局钩子。KeyboardProc代表的是hook的回调函数。
extern "C" {
#endif
__declspec(dllexport) void HookStart(DWORD PID)
{
g_hHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstance, PID);
}
__declspec(dllexport) void HookStop()
{
if( g_hHook )
{
UnhookWindowsHookEx(g_hHook);
g_hHook = NULL;
}
}
#ifdef __cplusplus
}
#endif
LRESULT CALLBACK KeyboardProc(
int nCode,
WPARAM wParam,
LPARAM lParam
)
{
//KBDLLHOOKSTRUCT* pKHook = (KBDLLHOOKSTRUCT*)lParam;
//FILE* fp = fopen("input.txt", "a+");
if (lParam >> 31 == 0)
{
//MessageBox(NULL, L"cdca", L"提示", MB_OK | MB_ICONINFORMATION);
if (wParam == VK_F4)//F4,食品
PostThreadMessage(ppp, WM_USER, FOOD, NULL);
else if (wParam == VK_F2)//F2,汽油
PostThreadMessage(ppp, WM_USER, GAS, NULL);
else if (wParam == VK_F3)//F3,医药
PostThreadMessage(ppp, WM_USER, MEIDCAL, NULL);
else if (wParam == VK_F5)//F4,锁血
PostThreadMessage(ppp, WM_USER, blood, NULL);
else
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
//fprintf(fp, "%d\n",ppp);
}
//fclose(fp);
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
return 1;
}
socket udp编程
#pragma comment(lib,"ws2_32.lib")
WSADATA wsaData;//初始化
SOCKET SendSocket;
SOCKADDR_IN RecvAddr;//服务器地址
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
WSAStartup(MAKEWORD(2, 2), &wsaData);
SendSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
GetModuleFileNameA(NULL, SendBuf, MAX_PATH);
sendto(SendSocket, SendBuf, strlen(SendBuf), 0, (SOCKADDR *)&RecvAddr, sizeof(RecvAddr));
recvfrom(SendSocket, recvbuf, sizeof(recvbuf), 0, NULL, NULL);
WSACleanup();