C -> delphi problem or a stupid mistake
Hi!
I''ve just finished reading WGPFD and i am now going through the chapters and trying the examples myself. I''m converting these examples, on the fly, from C to delphi.
Currently i cant get this code snipit to work.
The problem is probable easy to fix but ...
const
ClassName : pchar = ''WINCLASS1'';
AppName : pchar = ''My First Window'';
var
WndClass_ : WNDCLASSex;
error,sizeof_error_msg,
error_msg : pchar;
begin
WndClass_.cbSize := sizeof(WndClass_);
WndClass_.style := CS_HREDRAW or CS_VREDRAW;
wndclass_.lpfnWndProc := @DefWindowProc;
wndclass_.cbClsExtra := 0;
wndclass_.cbWndExtra := 0;
wndclass_.hinstance:= HINSTANCE;
wndclass_.hbrBackground := COLOR_WINDOW +1 ;
wndclass_.lpszMenuName := nil;
wndclass_.lpszClassName := ClassName;
wndclass_.hIcon := LoadIcon(0,IDI_APPLICATION);
wndclass_.hIconSm := LoadIcon(0,IDI_APPLICATION);
wndclass_.hCursor := LoadCursor(0,IDC_ARROW);
if registerclassex(WndClass_) = 0 then
begin
error := getlasterror;
sizeof_error_msg := FormatMessage(
// source and processing options
FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM,
nil, // pointer to message source
error, // requested message identifier
0, // language identifier for requested message
error_msg, // pointer to message buffer
0, // maximum size of message buffer
nil); // pointer to array of message inserts
...
The error returned by getlast error has the value of 2
Also the formatmessage doesnt work. How would i fix this?
Ps can you give me an example of how to free the memory allocated in the FormatMessagea proc ? Or is that needed?
Thanxs for any help provided.
I''m not sure, exactly, but here is how I would do it:
const
ClassName : string = ''WINCLASS1'';
AppName : string = ''My First Window'';
...
wndclass_.lpszClassName := PChar(ClassName);
You will probably want to make sure your definition of WNDCLASSex includes the "packed" directive:
type
WNDCLASSex=packed record
...
end;
Hope this helps. Without seeing the full converted file, it''s hard to be more specific.
DavidRM
Samu Games
const
ClassName : string = ''WINCLASS1'';
AppName : string = ''My First Window'';
...
wndclass_.lpszClassName := PChar(ClassName);
You will probably want to make sure your definition of WNDCLASSex includes the "packed" directive:
type
WNDCLASSex=packed record
...
end;
Hope this helps. Without seeing the full converted file, it''s hard to be more specific.
DavidRM
Samu Games
I found out what was wrong.
It was the STUPID if!!!!
Delphi5 cant handle an winapi call in the evaluation section of an if statment!!!!!!!!
Here is a chunk of code which will not work :
if registerclass(wndclass_) = 0 then
begin
messagebox(0,''Critical Error : Unable to register Window Class'',''WinTetris'',mb_ok);
...
Here is the working version
tempresult := registerclass(wndclass_);
if tempresult = 0 then
begin
messagebox(0,''Critical Error : Unable to register Window Class'',''WinTetris'',mb_ok);
...
Talk about a STUPID bug.
It was the STUPID if!!!!
Delphi5 cant handle an winapi call in the evaluation section of an if statment!!!!!!!!
Here is a chunk of code which will not work :
if registerclass(wndclass_) = 0 then
begin
messagebox(0,''Critical Error : Unable to register Window Class'',''WinTetris'',mb_ok);
...
Here is the working version
tempresult := registerclass(wndclass_);
if tempresult = 0 then
begin
messagebox(0,''Critical Error : Unable to register Window Class'',''WinTetris'',mb_ok);
...
Talk about a STUPID bug.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement