473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Windows c programing

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

Nov 13 '05 #1
3 5825
Glen wrote:
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>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #2
Glen wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #3

"Glen" <glen@::FILTER::xmsg.com> wrote in message

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.
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: farshad | last post by:
Hello, Could someone tell me which way can we use windows command(for example Create New Folder) in visual c++ programing or use dos command without using dos prompt. best regards farshad
4
by: Tamir Khason | last post by:
Is it possible? or just my fantasy? I did it before with cpp. Handle and change base classes such as dialogs, windows etc. Is it possible to do with C#? Someone knows good reference to such kind...
2
by: cyshao | last post by:
How to reset Router by programing? For some resean, we need usually reset our Router. Now, we have to Reset Router manually(shot down and reopen). Are there any method to control and reset...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
2
by: sengpg345 | last post by:
hi everybody. i have a project in colage for messanger but i haven't idea of socket programing in c#.net . so plz give me the guidence i socket programing. if u have row matirial or tutorial then...
69
by: Jack Dowson | last post by:
Hello Everybody: I'm learning c now.I think it's really a tedious job following my textbook to write programs which are used to deal with math problems.I want to write some codes related with...
4
by: folen | last post by:
Hi to every body, I am reading this forum since about two weeks, this forum has solved a lot of question that i had, thank to every body. I have a question about messages (WM_PAINT, etc...) in...
3
by: Abhinay | last post by:
Hello, I am new in windows socket programing, I am unable to get exact difference between bind() and connect() functions provided by windows( winsock.h) ie when to use bind() and when to use...
2
by: maamirj | last post by:
Hi, I am newly start prgraming from very beigning using .net c# 2005 can u help me the concepts of programing, oops from begining and how to start programing wih right techniques. thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.