How to capture image of a specific application window using windows API?

I am following the api from here: https://learn.microsoft.com/en-us/windows/win32/gdi/capturing-an-image

I am using msys ucrt64 gcc compiler for this program.
Here is the code I have:

#include <Windows.h>
#include <tlhelp32.h>
#include <wincodec.h>
typedef HWND WindowHandle;

#define ASSERT_EXIT(expr, str, ...)                                                                                                      
  ((void) sizeof ((expr) ? 1 : 0), __extension__ ({                                                                                      
    if (!(expr)) {                                                                                                                       
        fprintf(stderr,"At function [%s] in file [%s:%d], assert failed: [(%s)].n" str "n", __func__, __FILE__, __LINE__, (#expr));  
        __VA_ARGS__;                                                                                                                     
        exit(EXIT_FAILURE);                                                                                                              
    }                                                                                                                                    
  }))

#define TEST
#define DEBUG_UTIL

#ifdef DEBUG_UTIL
void save_bmp(BITMAP bmp, HDC winDC, HBITMAP hbmWin) {
    BITMAPFILEHEADER bmfHeader;
    BITMAPINFOHEADER   bi;
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bmp.bmWidth;
    bi.biHeight = bmp.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = 32;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;
    DWORD dwBmpSize = ((bmp.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmp.bmHeight;
    HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
    char *lpbitmap = (char*)GlobalLock(hDIB);
    GetDIBits(winDC, hbmWin, 0,
        (UINT)bmp.bmHeight,
        lpbitmap,
        (BITMAPINFO*)&bi, DIB_RGB_COLORS);
    HANDLE hFile = CreateFile("./captureqwsx.bmp",
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
    DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
    bmfHeader.bfType = 0x4D42;
    DWORD dwBytesWritten = 0;
    WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
    GlobalUnlock(hDIB);
    GlobalFree(hDIB);
    CloseHandle(hFile);
}
#endif

static vector<DWORD> *pid_list = new vector<DWORD>();
static inline void fill_pid_list(const wchar_t* exeName) {
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapshot != INVALID_HANDLE_VALUE) {
        PROCESSENTRY32W processEntry;
        processEntry.dwSize = sizeof(PROCESSENTRY32W);
        if (Process32FirstW(hSnapshot, &processEntry)) {
            do {
                if (wcsstr(processEntry.szExeFile, exeName) != NULL) {
                    (*pid_list).push_back(processEntry.th32ProcessID);
                }
            } while (Process32NextW(hSnapshot, &processEntry));
        };
        CloseHandle(hSnapshot);
    }
}

typedef struct {
    size_t list_sz;
    WindowHandle win;
}clbckParam;

static inline BOOL CALLBACK winListPidClbck(WindowHandle handle, LPARAM param) {
    DWORD pid = 0;
    clbckParam *cp = (clbckParam *)param;
    GetWindowThreadProcessId(handle, &pid);
    for(size_t i = 0; i < cp->list_sz; i++) {
        if(pid == (*pid_list)[i]) {
            if (IsWindowVisible(handle)) {
                cp->win = handle;
                return FALSE;
            }
            break;
        }
    }
    return TRUE;
}

static inline WindowHandle get_win_from_pid(const wchar_t* exeName) {
    fill_pid_list(exeName);
    clbckParam cp = { .list_sz = (*pid_list).size(), cp.win = NULL };
    EnumWindows(winListPidClbck, (LPARAM)&cp);
    delete pid_list;
    return cp.win;
}

void CaptureWindow(const wchar_t* exeName) {
    HWND win = get_win_from_pid(exeName);
    ASSERT_EXIT(win, "Failed to get window handle");
    RECT rect;
    GetClientRect(win, &rect);
    LONG w = rect.right - rect.left;
    LONG h = rect.bottom - rect.top;
#ifdef TEST
    printf("h:%ld, w:%ld", h, w);
#endif
    HDC winDC = GetDC(win); /* if i change win to NULL it captures the whole screen and produces and image I have no problem captures w x h size of the entire screen */ 
    ASSERT_EXIT(winDC, "Failed to get winDC");
    HDC memDC = CreateCompatibleDC(winDC);
    ASSERT_EXIT(memDC, "Failed to create memDC", ReleaseDC(win, winDC));
    HBITMAP hbmWin = CreateCompatibleBitmap(winDC, w, h);
    ASSERT_EXIT(hbmWin, "Failed to create hbmWin", DeleteDC(memDC), ReleaseDC(win, winDC));
    SelectObject(memDC, hbmWin);
    auto cleanup = [&]() {
        DeleteObject(hbmWin);
        DeleteDC(memDC);
        ReleaseDC(win, winDC);
    };
    ASSERT_EXIT(BitBlt(memDC, 0, 0, w, h, winDC, 0, 0, SRCCOPY), "BitBlt failed", cleanup());
    BITMAP bmpWin;
    GetObject(hbmWin, sizeof(BITMAP), &bmpWin);
#ifdef TEST
    save_bmp(bmpWin, winDC, hbmWin);
#endif
    cleanup();
}

#ifdef TEST

int main() {
    const wchar_t* exeName = L"Code";
    CaptureWindow(exeName);
    return EXIT_SUCCESS;
}
#endif

Title: Issue with Capturing Specific Window Image Using GetDC in Windows

Question:
I’m trying to capture an image of a specific window in Windows using the GetDC function. When I pass NULL to GetDC, it captures the whole screen correctly and produces an image without any issues. However, when I pass a specific window handle win to GetDC, it produces a black image.
This is the relevant line of code HDC winDC = GetDC(win); and change this code to HDC winDC = GetDC(NULL); it will generate a image(not correctly entirely because the rect dimensions are not correct) but it shows proof of concept. So, question is why passing win to capture a specific window does not work? how to fix it?

I am sure that I am getting the correct window before the rect dimensions match the window.

Any insights or suggestions would be greatly appreciated! (MINIMUM REPRODUCIBLE CODE ATTACHTED AT THE TOP)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật