Hottest Forum Q&A on CodeGuru for the week of April 11th, 2004


Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:

How can I prevent the startup pulse from occurring when working with serial ports? (top)  


hmc is working with serial port programming. When he tries to open the serial port with CreateFile(), there is a short pulse on the DTR line that causes the LED to blink. Now we he wants to get rid of that. Do you know how to achieve that?

I have connected a led to the DTR line of the serial port just
for testing purposes.

Turning the led on/off works just fine with the EscapeCommFunction()
function that sets or clears the DTR line.

However, there is a problem. As soon as the serial port is opened
with CreateFile(), there is a short pulse on the DTR line; this
causes the led to blink very shortly. Does someone knows how to
prevent this startup pulse from occurring?
m_hPort = CreateFile(
   portName,                      // Pointer to the name of the port
   GENERIC_READ | GENERIC_WRITE,  // Access (read/write) mode
   0,                             // 0, serial ports cannot be shared
   NULL,                          // Pointer to the security
                                  // attribute
   OPEN_EXISTING,                 // How to open the serial port
   0,                             // Port attributes
   NULL);                         // Handle to port with attribute
                                  // to copy

// If it fails to open the port, return false.
   return false;

return true;

TDM described the solution very well. He suggest enabling the setting of the fDtrControl member of the DCB struct before opening the port. This should solve the problem. This might look like this:

h_Port = CreateFile(m_PortName,GENERIC_READ |  




if (!SetCommState(h_Port,m_dcb))
   MessageBox("Open serial port failed");
   return 0;

How do I start my program automatically when the user double-clicks the file? (top)  


lugangxyz wants his application to start automatically when a user clicks on a file with a specific extension.

The file is specific to my program. How should I do it?

Well, when you create a new MFC Doc/view project, you can select in the wizard to automatically assign files with a specific extension. But what do you do if you forgot to do this in the wizard? To achieve this, you have to change the resource for the document specifying file type etc. First, you'll need to change the resource for the document specifying file type. This is in the string tables. For example, if the extension is .map:

My Application\n\nMAP\nMy Application files
(*.map)\\nMyApplication.Document\nMy Application Document

Then, insert the following two lines into the InitInstance of your application class after AddDocTemplate:


If you are working with Dialog based applications, then you should use the code like this:

void CYourApp::RegisterShellFileTypes(BOOL bCompat)

How can I block all keyboard and mouse input from all active windows except from one window? (top)  


Organize asked a very interesting question. He needs to write a program that blocks all keyboard and mouse input from all windows except from his program.

HI. I am in a bit of a spot, I have some trouble. I have to make
this program that blocks all keyboard and mouse input from all
active windows except one. The one window where the keyboard and
mouse input are not blocked is the one where you are supposed to
enter a password. If this password is correct, you can unlock the
other windows as well (they will once again be able to recieve
keyboard and mouse input).

I am using windows hooks (if you know another way, I am all ears).
So, my problem is that after the user inserts the correct password
and clicks OK, the program terminates but the hooks that have been
set on the other windows still run, so even after the user inserts
the correct pass he still can't work with the windows that have
been activated during the time that the program has been active.

Okay, so I am using to DLLs and a main program. One DLL is for the
keyboard hook, one is for the mouse hook, and one for the mouse
hook. I am only going to post about the mouse hook because I think
this is the most important one of them all.
// ... code goes here ...
// Because the code which Orgranize uses is pretty long, I won't
// paste it here.
// Instead I request you to take a look at the thread

Well, the only solution would be to use the LockWorkStation() function, which submits a request to lock the station. Something like this:

// This should be defined before the #includes in your stdafx.h
#define _WIN32_WINNT 0x0500

Then, you can call the LockWorkStation() function from anywhere in your code.

Why does free() not work for me? (top)  


kfaday needs to free the memory after usage. Unfortunately, it does not work as excepted.

I've got a char array:
char *vec;
And I allocate memory for a number of elemets
vec = (char*) malloc ((sizeof(char))*(number_of_elemets));
I do that in main. then i call a free function
free_mem (vec);
the function code is:
void free_mem (char *vec,number_of _elements)
   for (i=0;i<(number_of _elements);i++)
      free (vec[i]);
It gives me errors. What's wrong?

Well, you're calling free() on each of the characters, which is invalid and leads to undefined behavior. You can just simply use:


About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at:


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Whether you're just now developing your first mobile app, refining an existing one, or deploying multiple apps, the process for doing so can be complicated. Utilizing a Rapid Mobile Application Development (RMAD) platform can help you not only make that process easier, but also help the business reach its goals in a timely, cost-efficient manner. This eBook outlines seven key factors to consider as you choose the right RMAD platform to meet your needs, and includes a quick-reference checklist.

  • You've managed to piece together the "DREAM TEAM." Your tech staff works like a well-oiled machine, keeping your company humming and thriving. And then it happens: dissension. For whatever reason, your employees have grown unhappy and you find out they're searching for new employment or losing productivity. What did you do wrong? Did you hire the wrong people? Did your company push them away? Or is it a combination of numerous factors? Read this white paper to learn how to build an environment that fosters …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date