Hottest Forum Q&A on CodeGuru
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:
- Where is <dirent.h>?
- How do I disable the close button?
- How can I get the window handle from a process ID?
- How can I use UNICODE in a MessageBox without a UNICODE compilation?
- Should I replace CAsyncSockets with WinSock?
SeventhStar is working on a code in which he uses functions and structs defined in the header <dirent.h>, but unfortunaly he cannot find the header file. Do you know where to find the header?
I have a code that I need to compile and it uses the functions and structs defined in <dirent.h>. I found out that this is not a msvc standard header but I need to use msvc ( for other stuff in the project ). So I searched in the internet a lot and could only find the file but now I need to find a .lib or .obj file that contains the functions bodies, and unfortunately I cannot... Can someone please tell be where I can find this?
So what can be done here? Should he search the Internet to find the file? But, one second; he did that already and only found the header file but not the libs.
<dirent.h> is part of POSIX, so one could think that it is only available for *NIX systems. But, DJGPP and DEV C++ are both Windows compilers and both include these files. So, the only solution is to rewrite the functions for your own needs and forget about trying to find the files for MSVC. You may never find a header file or lib file to call your functions exactly as before, so you use the functions that are available that mimic or closely resembles what the other functions did.
Radu is working on an application in which he has a modeless dialog box. He wants the user to not able to close the dialog.
I have a modeless dialog. While it is opened I want to prevent the user from being able to close the calling application using the destroy button. How do I do that?
Although this is a simple question, it is always good to know such things, especially for beginners.
One solution is to handle the WM_CLOSE Handle; this handles the X in the top right corner of the title bar. If you override the WM_CLOSE message, that traps the:
- X in the title bar
- exit from the system menu
- ALT + F4
- the end task from the task manager
mctpursuer knows how to get the process ID for the newly created process using CreateProcess. But, he doesn't know how to get the handle of the application's main window. Do you know?
One interesting solution came from Marc G:
You can use WaitForIdle, but that sometimes returns when the splash screen of the launched app is shown, so this does not work in all cases.
A second way, but also a much more complicated way, is the following. Use SetWindowsHookEx to create a hook for WH_SHELL. In your ShellProc, handle HSHELL_WINDOWCREATED. After you've launched your app, start waiting. For each new window created, your ShellProc will be called. For each window, call GetWindowThreadProcessId(hWnd, &dwProcessID); Compare the dwProcessID of the newly created window with the processid you got from CreateProces. If those two processids are equal you've found your window.
But note the following points:
- Even this complicated way does not always work, so make sure you add some kind of timeout in your app so that you do not keep waiting for your window.
- Try to do as little as possible in your ShellProc because it's called for each new shell-window that is created.
AnitaEugene is working on a project in which he needs some MessageBoxes and menus in Unicode, without a UNICODE compilation.
I have a huge old project. Converting it to total UNICODE compilation is very difficult. I need few menus and Messages boxes in UNICODE (Need in Japanese). How to achieve this without UNICODE compilation? Is this possible?
Of course, this is possible. The MessageBoxW function can be used to display Unicode characters. However, if you are running under Win 9x or Win Me, you will need the aid of the Microsoft Layer for Unicode to run that functions. If you are running under W2000 or XP, you won't need the layer for Unicode and you can call all the Unicode functions directly. To paint some text, you can use the TextOutW() function with datatypes such as wchar_t.
s_k is working on a large FTP Server project that hangs while high utilization. Should he replace his base class CAsyncSockets with the WSAXxx() functions so that the application does not hang?
I've a large project (FTP Server) which is based on CAsyncSockets. Unfortunately, sometimes while high utilization, it doesn't manage it (eg. it hangs up in CAsyncSocket::Receive()). And here comes the question: I'm interested in the fact, whether it would help to replace MFC Sockets with WinSock sockets (I mean WSAXxx() functions). Would the result be worth the effort to replace it? You see, it's not easy to force myself to do such a big change in this project, which is otherwise completed.
Well, it is actually not that difficult. You can create your own class derived from CWnd that, on connection, calls ::connect. Then, use ::WSAAsyncSelect to process messages and pass them to your own OnReceive/OnClose etc virtual functions.
So, in conclusion, just implement all the same functions as in CAsyncSocket (Create, Send, OnReceive, OnClose, and so forth) in your own class but use Winsock instead and it will solve your blocking problem because you have complete control over the operation of the socket—which you don't have with CAsyncSocket.