ApprenticeHacker
ApprenticeHacker

Reputation: 22011

Gdi+ with MSVC++ Compiler getting weird "Native Graphics" runtime error

Ok until now I've always coded with GCC so I am new to MSVC++ (and it's errors). I compiled a minimal Win Api program with GDI plus. It compiles fine but closes with a runtime error almost as soon as it starts (I think the runtime error comes when the WM_PAINT message is called). Here's what I see: What I See

My code is nothing fancy it just displays a rectangle. ( It works fine when I do it with GDI but doesn't work with GDI+).

My Code:

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);

By debugging I found that the runtime error comes almost as soon as the Graphics g(hdc); constructor is called. I can't find anything on google etc. I'm completely lost. Any help?

Upvotes: 4

Views: 729

Answers (1)

Tamás Szelei
Tamás Szelei

Reputation: 23921

WinAPI functions always indicate success one way or another. You need to check if GetDC returned a valid handle. See the Return value section of GetDC. The access violations in the output also point towards an invalid pointer somewhere. If the DC is valid, check everything else. Make sure you pass pointers where an address is required etc.

Upvotes: 1

Related Questions