Taking a screenshot C++ cli

2019-06-02 21:20发布

问题:

I've seen the following code that takes a screenshot and saves it as jpg, I've managed to compile and run it as win32 CONSOLE application,

But when I tried to use the following code in A windowsForm/CLI (there is just a button that should take a screenshot) project, I got the following errors:

1>screenshoter.obj : warning LNK4248: unresolved typeref token (0100002C) for 'Gdiplus.GpCachedBitmap'; image may not run
1>screenshoter.obj : error LNK2028: unresolved token (0A000017) "extern "C" int __stdcall ReleaseDC(struct HWND__ *,struct HDC__ *)" (?ReleaseDC@@$$J18YGHPAUHWND__@@PAUHDC__@@@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A000018) "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject@@$$J14YGHPAX@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A000019) "extern "C" int __stdcall BitBlt(struct HDC__ *,int,int,int,int,struct HDC__ *,int,int,unsigned long)" (?BitBlt@@$$J236YGHPAUHDC__@@HHHH0HHK@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001A) "extern "C" void * __stdcall SelectObject(struct HDC__ *,void *)" (?SelectObject@@$$J18YGPAXPAUHDC__@@PAX@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001B) "extern "C" struct HBITMAP__ * __stdcall CreateCompatibleBitmap(struct HDC__ *,int,int)" (?CreateCompatibleBitmap@@$$J212YGPAUHBITMAP__@@PAUHDC__@@HH@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001C) "extern "C" struct HDC__ * __stdcall CreateCompatibleDC(struct HDC__ *)" (?CreateCompatibleDC@@$$J14YGPAUHDC__@@PAU1@@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001D) "extern "C" int __stdcall GetSystemMetrics(int)" (?GetSystemMetrics@@$$J14YGHH@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001E) "extern "C" struct HDC__ * __stdcall GetDC(struct HWND__ *)" (?GetDC@@$$J14YGPAUHDC__@@PAUHWND__@@@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall ReleaseDC(struct HWND__ *,struct HDC__ *)" (?ReleaseDC@@$$J18YGHPAUHWND__@@PAUHDC__@@@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject@@$$J14YGHPAX@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall BitBlt(struct HDC__ *,int,int,int,int,struct HDC__ *,int,int,unsigned long)" (?BitBlt@@$$J236YGHPAUHDC__@@HHHH0HHK@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" void * __stdcall SelectObject(struct HDC__ *,void *)" (?SelectObject@@$$J18YGPAXPAUHDC__@@PAX@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" struct HBITMAP__ * __stdcall CreateCompatibleBitmap(struct HDC__ *,int,int)" (?CreateCompatibleBitmap@@$$J212YGPAUHBITMAP__@@PAUHDC__@@HH@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" struct HDC__ * __stdcall CreateCompatibleDC(struct HDC__ *)" (?CreateCompatibleDC@@$$J14YGPAUHDC__@@PAU1@@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall GetSystemMetrics(int)" (?GetSystemMetrics@@$$J14YGHH@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" struct HDC__ * __stdcall GetDC(struct HWND__ *)" (?GetDC@@$$J14YGPAUHDC__@@PAUHWND__@@@Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen@Form1@screenshoter@@$$FA$AAMXXZ)

Note: That I've included all the files needed

I looked up in MSDN, but I couldn't really understand the error messeges. Moreover, I had encountered similar messages in the past, i solved it but adding a .lib file

This time I tried adding, however it didn't solve the problem

#pragma comment( lib, "GdiPlus.lib" )

回答1:

Those functions are declared in gdi32.lib - not gdiplus.lib - and some might be in user32.lib.



回答2:

Those error messages mean that your code (or rather, the 'screenshooter' code) used 'symbols' (could be function names or variables, for instance) for which there is no code linked into the final binary.

To fix this, look up what libraries you need to link against for each of those symbols. For instance, GetDC appears to be defined in user32.lib, so make sure to link against that (possibly by using a #pragma directive as you did for GdiPlus.lib).