GDI+ problem on Windows Me

I have only just started trying GDI+. The 'draw-a-line'
sample program in 'Getting started wth GDI+' works fine
on XP, but does not work at all on Windows Me.
Here is the program:

#define UNICODE
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;

VOID OnPaint(HDC hdc)
{
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawLine(&pen, 0, 0, 200, 100);
}

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR,
INT iCmdShow)
{
HWND hWnd;
MSG msg;
WNDCLASS wndClass;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;

// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = TEXT("GettingStarted");
RegisterClass(&wndClass);
hWnd = CreateWindow(
TEXT("GettingStarted"), // window class name
TEXT("Getting Started"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return msg.wParam; //<<<<< went straight here, did not execute onpaint
} // WinMain

LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;

switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
OnPaint(hdc);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
} // WndProc

The program exited immediately.

I also tried rebuilding from source on the Windows Me machine,
but the result was the same.

Before trying, I had put the redistributable gdiplus.dll
(downloaded Microsoft) into the directory of .exe file on
the Windows Me machine.

If GDI+ does not run on Windows 98/Me/2000, then it is a
non-starter for my applications, but I think it probably
does. But how to make it?

Grateful for suggestions.
[3504 byte] By [resander] at [2007-11-18 19:18:17]
# 1 Re: GDI+ problem on Windows Me
It is #define UNICODE that caused the problem.
Taking it out makes the program work on Windows Me,
when built on XP and copied across as well as when
rebuilt from source on the Me.

I don't know why defining UNICODE interferes with the Windows
Me program but not with the XP program. Any ideas?

The second sample "Getting started draw-a-string' also works
on Windows Me.
resander at 2007-11-10 3:52:00 >
# 2 Re: GDI+ problem on Windows Me
Normally, Windows98/Me do not support UNICODE.
As I know, a patch is requred, but unfortunately I cannot advise how to do that.

To make your application run also under Windows 98/Me just make a not UNICODE build.

Because most of GDI+ methods thake only WCHAR* type parameters (UNICODE version of char*),
you can use mbstowcs conversion function.

To make your code compile both UNICODE and non-UNICODE, you can do something like in the next example:
void CGDIPBitmap::Save(LPCTSTR pszFile,
CLSID& clsid,
EncoderParameters* pParam)
{
#ifdef UNICODE
m_pBitmap->Save(pszFile, &clsid, pParam);
#else
const size_t size = 1 + strlen(pszFile);
WCHAR* wcsFile = new WCHAR[size];
mbstowcs(wcsFile, pszFile, size);
m_pBitmap->Save(wcsFile, &clsid, pParam);
delete[] wcsFile;
#endif // UNICODE
}
Note that you can have both UNICODE and non-UNICODE configurations in your project:
1. Remove #define UNICODE from your source files;
2. Add to project new configuration(s) for example "Win32 - Debug UNICODE";
3. In project settings, add UNICODE preprocessor constant only to configurations you want to build UNICODE.
Another note (even I think you already know this):
Gdiplus.dll is distributed only beginning with Window XP;
to make you application run under Windows NT/2000/98/Me you must put that dll in the application folder.
ovidiucucu at 2007-11-10 3:52:57 >