Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 屏幕截图代码在调用GetDIBits时出现意外错误_C_Windows_Winapi - Fatal编程技术网

C 屏幕截图代码在调用GetDIBits时出现意外错误

C 屏幕截图代码在调用GetDIBits时出现意外错误,c,windows,winapi,C,Windows,Winapi,我正在使用一些C代码拍摄一个窗口的屏幕截图,直接从一个运行完美的PowerBuilder示例翻译过来。以下是出现问题的部分: extern "C" __declspec(dllexport) BOOL __stdcall WindowScreenShot(const wchar_t* fileName, unsigned long x, unsigned long y, unsigned long width, unsigned long height) {

我正在使用一些C代码拍摄一个窗口的屏幕截图,直接从一个运行完美的PowerBuilder示例翻译过来。以下是出现问题的部分:

extern "C" __declspec(dllexport) BOOL __stdcall WindowScreenShot(const wchar_t* fileName, unsigned long x, unsigned long y,
    unsigned long width, unsigned long height)
{
    HWND ll_hWnd;
    HDC ll_hdc, ll_hdcMem;
    HBITMAP ll_hBitmap;
    HANDLE hDib = NULL, hFile = NULL;
    char* lpBitmap = NULL;
    BOOL lb_result, lb_ok = FALSE;
    BITMAPINFO lstr_Info;
    BITMAPFILEHEADER lstr_Header;
    int li_pixels;
    DWORD dwBmpSize, dwBytesWritten;

    // get handle to windows background
    ll_hWnd = GetDesktopWindow();

    // Get the device context of window and allocate memory
    ll_hdc = GetDC(ll_hWnd);
    ll_hdcMem = CreateCompatibleDC(ll_hdc);
    ll_hBitmap = CreateCompatibleBitmap(ll_hdc, width, height);

    if (ll_hBitmap != 0)
    {
        // Select an object into the specified device context
        SelectObject(ll_hdcMem, ll_hBitmap);
        // Copy the bitmap from the source to the destination
        lb_result = BitBlt(ll_hdcMem, 0, 0, width, height, ll_hdc, x, y, SRCCOPY);
        lstr_Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        // Get the bitmapinfo (THIS LINE IS FAILING CURRENTLY)
        if (GetDIBits(ll_hdcMem, ll_hBitmap, 0, height, NULL, &lstr_Info, DIB_RGB_COLORS) > 0)
        { ...
最后一次调用
GetDIBits
总是失败,它只能给出一个记录在案的错误,
error\u INVALID\u PARAMETER
。由于此代码基本上与PowerBuilder代码完全相同,因此就使用的结构和调用的Windows API而言,我不知道如何解决此问题。我也仔细阅读了API文档,看起来应该可以正常工作


有什么好主意吗?谢谢。

保罗·奥格尔维的回答解决了这个问题——我使用了未初始化的结构,因为它们是自动变量。一旦我确定代码正常工作。

FWIW,at上的示例在将位图头传递给GetDIBits之前填充了位图头的更多字段。
lstr\u Info
是一个自动变量,包含垃圾。你必须正确初始化它。谢谢你,保罗!显示了当你只是偶尔用C语言编程时会发生什么,我忘记了一些事情,比如必须初始化自动变量,这在我一直这么做的时候是我的第二天性!这就解决了问题。来自AKX的Microsoft示例对我不起作用,在截图之前,它以某种方式弄乱了它正在截图的原始窗口。“我使用未初始化的结构,因为它们是自动变量”-“自动”与对象的生命周期有关。这不是原因,也与未初始化的对象无关。您可以有未初始化的变量,自动或其他。您可以(也应该)初始化变量,自动或其他。你的编译器警告过你这一点。不要忽视这些警告。