Hottest Forum Q&A on CodeGuru
Introduction:
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?
- How do I start my program automatically when the user double-clicks the file?
- How can I block all keyboard and mouse input from all active windows except from one window?
- Why does free() not work for me?
|
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. if (m_hPort == INVALID_HANDLE_VALUE) 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 | GENERIC_WRITE,0,0,OPEN_EXISTING,0,0); GetCommState(h_Port,&m_dcb); m_dcb->fDtrControl=DTR_CONTROL_ENABLE; SetupComm(h_Port,4096,4096); if (!SetCommState(h_Port,m_dcb)) { MessageBox("Open serial port failed"); return 0; }
|
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:
IDR_MAINFRAME My Application\n\nMAP\nMy Application files (*.map)\n.map\nMyApplication.Document\nMy Application Document
Then, insert the following two lines into the InitInstance of your application class after AddDocTemplate:
EnableShellOpen(); RegisterShellFileTypes(TRUE);
If you are working with Dialog based applications, then you should use the code like this:
void CYourApp::RegisterShellFileTypes(BOOL bCompat) { CWinApp::EnableShellOpen(); CWinApp::RegisterShellFileTypes(bCompat);//TRUE }
|
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.
|
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:
free(vec);
This article was originally published on April 19, 2004