Windows c programing

G

Glen

Hello.
Can some one explain me why below example work


LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
DCB dcb;
HANDLE hCom;
BOOL fSuccess, fGet, fSet;
char *pcCommPort = "COM1";
DWORD errnum;





switch (iMsg)
{
case WM_CREATE :
hCom = CreateFile (pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);

fGet = GetCommState (hCom, &dcb);
fSet = SetCommState (hCom, &dcb);

return 0;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps);

GetClientRect (hwnd, &rect);

if (hCom == INVALID_HANDLE_VALUE)
{
DrawText (hdc, "Opening of com port failed", -1, &rect, DT_CENTER |
DT_VCENTER | DT_SINGLELINE);
return (1);
}


if (!fGet)
{
DrawText (hdc, "GetCommState failed" , -1, &rect, DT_LEFT |
DT_SINGLELINE);
errnum = GetLastError ();
DrawText (hdc, &errnum, -1, &rect, DT_BOTTOM );

return (2);
}

dcb.BaudRate = CBR_57600; //set the baud rate
dcb.ByteSize = 8; //data size, xmit, and rcv
dcb.Parity = NOPARITY; //no paritiy bit
dcb.StopBits = ONESTOPBIT; //one stop bit



if (!fSet)
{
DrawText (hdc, "SetCommState failed", -1 , &rect, DT_RIGHT |
DT_SINGLELINE);
return (3);
}

DrawText (hdc, "Serial port reconfigured", -1, &rect, DT_TOP |
DT_CENTER | DT_SINGLELINE);

EndPaint (hwnd, &ps);
return 0;

case WM_DESTROY :
PostQuitMessage (0);
return 0;
}

return DefWindowProc (hwnd, iMsg, wParam, lParam);
}

And this example does not work, beside that when above example is
compiled and linked with borland C5.0 it has some strange behavior on
every re-size window action result of com port opening is different with
visual c it has stable result.
________________________________________________________________________________________

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM1";
DWORD errnum;





LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM1";
DWORD errnum;





switch (iMsg)
{
case WM_CREATE :
hCom = CreateFile (pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);



return 0;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps);

GetClientRect (hwnd, &rect);

if (hCom == INVALID_HANDLE_VALUE)
{
DrawText (hdc, "Opening of Com port Failed", -1, &rect, DT_CENTER |
DT_VCENTER | DT_SINGLELINE);
return (1);
}
fSuccess = GetCommState (hCom, &dcb);

if (!fSuccess)
{
DrawText (hdc, "GetCommState failed" , -1, &rect, DT_LEFT |
DT_SINGLELINE);
errnum = GetLastError ();
DrawText (hdc, &errnum, -1, &rect, DT_BOTTOM );

return (2);
}

dcb.BaudRate = CBR_57600; //set the baud rate
dcb.ByteSize = 8; //data size, xmit, and rcv
dcb.Parity = NOPARITY; //no paritiy bit
dcb.StopBits = ONESTOPBIT; //one stop bit

fSuccess = SetCommState (hCom, &dcb);

if (!fSuccess)
{
DrawText (hdc, "SetCommState failed", -1 , &rect, DT_RIGHT |
DT_SINGLELINE);
return (3);
}

DrawText (hdc, "Serial port reconfigured", -1, &rect, DT_TOP |
DT_CENTER | DT_SINGLELINE);

EndPaint (hwnd, &ps);
return 0;

case WM_DESTROY :
PostQuitMessage (0);
return 0;
}

return DefWindowProc (hwnd, iMsg, wParam, lParam);
}
 
R

Richard Heathfield

Glen said:
Hello.
Can some one explain me why below example work


LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
lParam)

Try comp.os.ms-windows.programmer.win32 - where the Windows expertise is
much more reliable than in a general C newsgroup such as this.

<snip>
 
M

Malcolm

Glen said:
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
DCB dcb;
HANDLE hCom;
BOOL fSuccess, fGet, fSet;
char *pcCommPort = "COM1";
DWORD errnum;

switch (iMsg)
{
case WM_CREATE :
hCom = CreateFile (pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);

fGet = GetCommState (hCom, &dcb);
fSet = SetCommState (hCom, &dcb);

return 0;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps);

GetClientRect (hwnd, &rect);

if (hCom == INVALID_HANDLE_VALUE)
This is a C problem.

hCom hasn't been initialised here. The function is called each time with
different values of iMsg, and all the automatic variables are destroyed.
If you know that WM_CREATE is always passed first you could make hCom
static.
 
C

CBFalconer

Glen said:
Can some one explain me why below example work

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM
lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
DCB dcb;
HANDLE hCom;
BOOL fSuccess, fGet, fSet;
char *pcCommPort = "COM1";
DWORD errnum;

It doesn't. So far no identifiers have been defined. The commas
and parentheses might suggest, to a C programmer, that some sort
of function is being defined.

You might try some newsgroup where this collection of
nomenclatures has some meaning. c.l.c is not one.

I take it back. The identifier "char" has meaning, as does the
phrase following it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top