0

How can I take a screenshot of the current screen without using atlimage.h?

I have tried several attempts which seem not to need atlimage.h but I get lots of unresolved externals errors from linker that can be solve by adding atlimage.

Below you can see one of my attempts:

#include <iostream>
#include <windows.h>
using namespace std;

void GetScreenShot(void)
{
    int x1, y1, x2, y2, w, h;

    // get screen dimensions
    x1 = GetSystemMetrics(SM_XVIRTUALSCREEN);
    y1 = GetSystemMetrics(SM_YVIRTUALSCREEN);
    x2 = GetSystemMetrics(SM_CXVIRTUALSCREEN);
    y2 = GetSystemMetrics(SM_CYVIRTUALSCREEN);
    w = x2 - x1;
    h = y2 - y1;

    // copy screen to bitmap
    HDC     hScreen = GetDC(NULL);
    HDC     hDC = CreateCompatibleDC(hScreen);
    HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h);
    HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
    BOOL    bRet = BitBlt(hDC, 0, 0, w, h, hScreen, x1, y1, SRCCOPY);

    // save bitmap to clipboard
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hBitmap);
    CloseClipboard();

    // clean up
    SelectObject(hDC, old_obj);
    DeleteDC(hDC);
    ReleaseDC(NULL, hScreen);
    DeleteObject(hBitmap);
}

my errors are:

Error   LNK1120 13 unresolved externals ScreenShot          
Error   LNK2019 unresolved external symbol __imp__BitBlt@36 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)   ScreenShot      
Error   LNK2019 unresolved external symbol __imp__CloseClipboard@0 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)    ScreenShot      
Error   LNK2019 unresolved external symbol __imp__CreateCompatibleBitmap@12 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)   ScreenShot      
Error   LNK2019 unresolved external symbol __imp__CreateCompatibleDC@4 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)    ScreenShot      
Error   LNK2019 unresolved external symbol __imp__DeleteDC@4 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)  ScreenShot  
Error   LNK2019 unresolved external symbol __imp__DeleteObject@4 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)  ScreenShot  
Error   LNK2019 unresolved external symbol __imp__EmptyClipboard@0 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)    ScreenShot      
Error   LNK2019 unresolved external symbol __imp__GetDC@4 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ) ScreenShot      
Error   LNK2019 unresolved external symbol __imp__GetSystemMetrics@4 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)  ScreenShot      
Error   LNK2019 unresolved external symbol __imp__OpenClipboard@4 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ) ScreenShot      
Error   LNK2019 unresolved external symbol __imp__ReleaseDC@8 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ) ScreenShot      
Error   LNK2019 unresolved external symbol __imp__SelectObject@8 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)  ScreenShot  
Error   LNK2019 unresolved external symbol __imp__SetClipboardData@8 referenced in function "void __cdecl GetScreenShot(void)" (?GetScreenShot@@YAXXZ)  ScreenShot  
M.Parker
  • 45
  • 6
  • What linker errors? I can compile your code without any errors and special linking using visual studio 2015.. – erg Jan 17 '17 at 11:15
  • 2
    how about link with `gdi32.lib` and `user32.lib`? – RbMm Jan 17 '17 at 11:24
  • and with `OpenClipboard(NULL)` copy to clipboard will be no work. need use !=0 hwnd. even temp message only window – RbMm Jan 17 '17 at 11:33
  • I found out that when program build in release mode, there is no error!!. It will be appreciated if anyone explain it's reason... – M.Parker Jan 17 '17 at 11:53
  • 1
    Your release configuration has different linker settings. I don't know why this is the case. – IInspectable Jan 17 '17 at 13:06

0 Answers0