CreateDibSection 抛出访问冲突异常

CreateDibSection throws Access Violation Exception

我正在尝试创建一个 DIBSection 以便能够更快地渲染像素,因为 SetPixel 太慢了。现在,我正在尝试像这样创建它:

void** imagePointer = NULL;
DWORD mask888[] = { 0xFF0000, 0x00FF00, 0x0000FF };

BITMAPINFO bitmapInfo = BITMAPINFO();
memset(&bitmapInfo, 0, sizeof(bitmapInfo));

memcpy(bitmapInfo.bmiColors, mask888, sizeof(mask888));

bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = LCD_WIDTH; // 160
bitmapInfo.bmiHeader.biHeight = LCD_HEIGHT; // 144
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 32;
bitmapInfo.bmiHeader.biCompression = BI_RGB;

bitmap = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, imagePointer, NULL, 0);

但是发生的事情是,在执行 CreateDIBSection 调用时我只是抛出了一个访问冲突异常。

抛出的异常如下:

Exception thrown at 0x76E69623 (gdi32.dll) in Emulation.exe: 0xC0000005: Access violation writing location 0x0000FF00.

你能告诉我如何调试或解决这个问题吗?

非常感谢您!

哎呀!您忘记阅读 the documentation.

你传递了一个没有指向任何东西的 void**,但是那个参数是:

ppvBits [out] A pointer to a variable that receives a pointer to the location of the DIB bit values.

自然地,这个空指针将被取消引用,导致未定义的行为(在您的情况下是访问冲突)。

相反,将 imagePointer 地址 传递给 ppvBits:

BYTE* imagePointer = NULL; 
...
bitmap = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, (void**)&imagePointer, NULL, 0);