【发布时间】:2022-01-24 01:48:47
【问题描述】:
我正在尝试创建一个简单的机器人来测试我的技能。它可以在短时间内正常工作,然后崩溃。它有一个奇怪的内存泄漏,我无法理解。 任何帮助将不胜感激。
关于代码的额外信息。它对屏幕上的某个区域进行截图并比较图像上4个像素的颜色,当它看到一个暗像素时,它会自行按下相应的按钮。 是的,我正在尝试用 C++ 做一个钢琴瓷砖机器人 :)
[#include <windows.h>
int R\[4\];
int G\[4\];
int B\[4\];
COLORREF cRef\[4\];
void ScreenShot() {
HWND DesktopHwnd = GetDesktopWindow();
RECT DesktopParams;
HDC DevC = GetDC(DesktopHwnd);
GetWindowRect(DesktopHwnd, &DesktopParams);
DWORD Width = DesktopParams.right - DesktopParams.left;
DWORD Height = DesktopParams.bottom - DesktopParams.top;
DWORD FileSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (sizeof(RGBTRIPLE) + 1 * (Width * Height * 4));
char* BmpFileData = (char*)GlobalAlloc(0x0040, FileSize);
//std::cout << (BmpFileData);
PBITMAPFILEHEADER BFileHeader = (PBITMAPFILEHEADER)BmpFileData;
PBITMAPINFOHEADER BInfoHeader = (PBITMAPINFOHEADER)&BmpFileData\[sizeof(BITMAPFILEHEADER)\];
BFileHeader->bfType = 'BM'; // BM
BFileHeader->bfSize = sizeof(BITMAPFILEHEADER);
BFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
BInfoHeader->biSize = sizeof(BITMAPINFOHEADER);
BInfoHeader->biPlanes = 1;
BInfoHeader->biBitCount = 24;
BInfoHeader->biCompression = BI_RGB;
BInfoHeader->biHeight = Height - 1079;
BInfoHeader->biWidth = Width - 1522;
RGBTRIPLE *Image = (RGBTRIPLE*)&BmpFileData\[sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)\];
//RGBTRIPLE color;
HDC CaptureDC = CreateCompatibleDC(DevC);
HBITMAP CaptureBitmap = CreateCompatibleBitmap(DevC, Width, Height);
SelectObject(CaptureDC, CaptureBitmap);
BitBlt(CaptureDC, -753, -566, Width, Height, DevC, 0, 0, SRCCOPY | CAPTUREBLT);
GetDIBits(CaptureDC, CaptureBitmap, 0, Height, Image, (LPBITMAPINFO)BInfoHeader, DIB_RGB_COLORS);
for (size_t i = 0; i < 4; i++)
{
cRef\[i\] = GetPixel(CaptureDC, (100 * i) + 2, 0);
}
for (size_t i = 0; i < 4; i++)
{
R\[i\] = GetRValue(cRef\[i\]);
G\[i\] = GetGValue(cRef\[i\]);
B\[i\] = GetBValue(cRef\[i\]);
}
}
int main()
{
// This structure will be used to create the keyboard
// input event.
INPUT ip;
// Pause for 5 seconds.
Sleep(3000);
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
while (true)
{
ScreenShot();
for (size_t i = 0; i < 4; i++)
{
if (R\[i\] == 17 && B\[i\] == 17 && G\[i\] == 17)
{
if (i == 0)
{
// Press the "A" key
ip.ki.wVk = 0x41; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "A" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
if (i == 1)
{
// Press the "S" key
ip.ki.wVk = 0x53; // virtual-key code for the "S" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "S" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
if (i == 2)
{
// Press the "D" key
ip.ki.wVk = 0x44; // virtual-key code for the "D" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "D" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
if (i == 3)
{
// Press the "D" key
ip.ki.wVk = 0x46; // virtual-key code for the "D" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "D" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
}
Sleep(2);
}
}
}]
【问题讨论】:
-
您使用
GlobalAlloc而不使用GlobalFree并且想知道吗? - 你对内存管理了解多少?
标签: c++ windows memory-management memory-leaks