Gdi + with MSVC ++ compiler gets strange error "Native Graphics"

Well, so far I have always encoded GCC, so I am new to MSVC ++ (and these are errors). I compiled a minimal Win Api program with GDI plus. It compiles fine, but closes with an error at runtime almost immediately after it starts (I think a runtime error occurs when the WM_PAINT message is called). Here is what I see: What i see

My code doesn't represent anything, it just displays a rectangle. (It works great when I do this with GDI, but does not work with GDI +).

My code is:

HDC hdc = GetDC(hwnd); InvalidateRect(hwnd,NULL,FALSE); ULONG_PTR token; GdiplusStartupInput inp; GdiplusStartup(&token,&inp,0); Graphics g(hdc); g.Clear(Color(0,0,0)); g.DrawRectangle(new Pen(Color(0,0,0)),10,10,100,100); GdiplusShutdown(token); ValidateRect(hwnd,NULL); 

Debugging I found that a runtime error appears almost immediately after calling the Graphics g(hdc); constructor Graphics g(hdc); . I can not find anything on Google etc. I am completely lost. Any help?

+4
source share
2 answers

WinAPI functions always indicate success anyway. You need to check if GetDC returned a valid handle. See Section Return Value in GetDC . Access violations at the output also point to an invalid pointer somewhere. If the DC is valid, check everything else. Make sure you pass pointers where an address is required, etc.

+1
source

What is the value of hdc?

By the way, you must destroy g before calling GdiplusShutdown.

0
source

Source: https://habr.com/ru/post/1381002/


All Articles