Win32 RegisterClass Error
GaboonViper
gaboonviper at gmx.net
Sat Nov 17 03:53:11 PST 2007
Hi,
I've been working with win32 libraries for a few weeks. I'm trying to
build a GUI framework, as I'm not satisfied with the ones currently
available.
So currently I'm working on detecting win32 errors and I've found that
RegisterClass is giving me an Error Code 2, which apparently translates to
"The system cannot find the file specified".
It doesn't really seem to matter what I change. It always gives me the
same problem. I'm using the win32 headers from the bindings project on
dsource.org. However I tried the std.c.windows.windows with the same
results.
Below is the code that reproduces the problem. It's basically a slice of
one of the tutorials on dsource.org.
Can anyone tell me what causes this error? And how to fix it?
Boyd.
module main;
import std.string;
import std.c.windows.windows;
static HINSTANCE ghInstance;
static HWND ghWndMain;
void Win32ErrorCheck(char[] aFunction, char[] aFile, int aLine){
int fLastError = GetLastError();
if (fLastError > 0){
char[256] fMessage;
/*FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,
null, fLastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
fMessage.ptr, 256, null );*/
throw new Exception(aFile~ "("~toString(aLine)~"): Win32 Error
"~toString(fLastError)~" for " ~ aFunction ~ ", ");
} else {
//ShowMessage(aFunction ~ " succeeded");
}
}
void DoMessagePump()
{
MSG msg;
while (GetMessageA(&msg, cast(HWND) null, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
}
void CreateMainWindow()
{
HWND hWnd;
hWnd = CreateWindowA("DWndClass",
"MiniCalc",
WS_THICKFRAME | WS_MAXIMIZEBOX
| WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
125, 150, HWND_DESKTOP,
cast(HMENU) null, ghInstance, null);
assert(hWnd);
ghWndMain = hWnd;
}
void InitApplication()
{
WNDCLASS wc;
wc.lpszClassName = "DWndClass";
wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = &WindowProc;
wc.hInstance = ghInstance;
wc.hIcon = LoadIconA(cast(HINSTANCE) null, IDI_APPLICATION);
wc.hCursor = LoadCursorA(cast(HINSTANCE) null, IDC_CROSS);
wc.hbrBackground = cast(HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = null;
wc.cbClsExtra = wc.cbWndExtra = 0;
assert(RegisterClassA(&wc));
Win32ErrorCheck("RegisterClassA", __FILE__, __LINE__);
}
void InitInstance()
{
ghInstance = GetModuleHandleA(null);
InitApplication();
CreateMainWindow();
}
/**********************************************************/
/* Note the similarity of this code to the console D startup
* code in \dmd\src\phobos\dmain2.d
* You'll also need a .def file with at least the following in it:
* EXETYPE NT
* SUBSYSTEM WINDOWS
*/
extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleUnitTests();
extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
int result = 1;
gc_init(); // initialize garbage collector
_minit(); // initialize module constructor table
try {
_moduleCtor(); // call module constructors
_moduleUnitTests(); // run unit tests (optional)
InitInstance();
DoMessagePump();
}
catch (Object o) { // catch any uncaught exceptions
MessageBoxA(null, cast(char *)o.toString(), "Error",
MB_OK | MB_ICONEXCLAMATION);
result = 0; // failed
}
gc_term(); // run finalizers; terminate garbage collector
return result;
}
extern(Windows)
int WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_NOTIFY:
break;
default:
break;
}
return DefWindowProcA(hWnd, uMsg, wParam, lParam);
}
More information about the Digitalmars-d-learn
mailing list