Friday, August 28, 2009

Windows Message Handling (message pump) with Console
Couldn't get away from using the Win32 App (that uses WinMain()) to get this working.
  • Inside WinMain - CreateWindow with default style and options but don't ShowWindow. Register your WNDCLASS and WndProc using RegisterClass()
  • AllocConsole() - this is the key. This opens a console window and attaches it to the app
  • GetStdHandle() - Get the console handle for use in WriteConsole() and ReadConsole()
  • Run the usual GetMessage - TranslateMessage - DispatchMessage loop in WinMain (probably the last lines of code)
  • Messages (such as WM_CREATE, WM_CLOSE, WM_DEVICECHANGE, etc.) are handled in the WndProc CALLBACK.
  • Console events (CTRL+C, Close, etc.) needs to be handled in ConsoleHandler() that is registered via SetConsoleCtrlHandler() call.
  • Since you don't see the "window", the close/exit event will be received in the console and it is in the ConsoleHandler() that one should generate (using PostMessage)the WM_CLOSE message for the window (which is then handled in the WndProc()).
  • Just like malloc, the AllocConsole() has a corresponding FreeConsole(). It's logical place is in the WM_CLOSE message handler.
See also:

Wednesday, August 26, 2009

Windows Programming Pointers

Console Event Handling
http://www.codeproject.com/KB/winsdk/console_event_handling.aspx?msg=338390

Dialogs & Windows
http://www.codeproject.com/KB/dialog/index.aspx <== Here you can also find information about windows message handling using both MFC and SDK.


(From some webpost: http://stackoverflow.com/questions/764869/c-console-app-event-handling)
See the topic "Using Messages and Message Queues" in MSDN (under Win32 and COM Development > User Interface > Windows User Experience > Windows Management > Windows User Interface > Windowing > Messages and Message Queues; you'll probably need to take a look at the other articles and samples in the same section). Quick summary, omitting error handling and using C syntax rather than C# for reasons discussed below:

RegisterClass(...);
CreateWindow(...);
ShowWindow(...); // probably not in your case
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

As you can see from the window setup boilerplate, this still relies on "silent windows," albeit created and message-pumped via the Win32 API rather than through WinForms. So you're not really gaining anything by doing this way. Hence my feeling there's not much point translating this stuff into C# -- if the only solution to your problem is an invisible window, you may as well use an invisible Windows Form and all the friendly wrappers that come with that platform.

However, if you're not actually using a Windows Forms control like the poster of the linked question, then you can quite happily use .NET events in a console application. The restriction to STA and the need for a message pump is specific to receiving events from WinForms and ActiveX controls like the WebBrowser (or messages from Win32 HWNDs, though that doesn't necessarily require STA).


Console App vs Win32 App
Console application can easily be changed to Windows application (Win32 App) by simply changing the entrypoint main() to WinMain() and changing the Linker | System | SubSystem option in the project properties options. The only catch is that with Win32 App, the console input output is gone. Forms or dialog boxes are needed to get user input & display outputs.

Windows++ & PixieLib
Former is a lean MFC style framework (much leaner though) & later is an extension to MFC classes and provide other utility classes. However I could not get it to run properly on VC++2008 (even when i was able to compile it)


String Conversions
Convert between various string types: wchar to char, etc. - Here

Nice Tutorial on using Win32 API

Simple Word Processing with an Edit Control
MSDN Example

New Line & Edit Control
Use \r\n instead of \n. Just using\n will show as small square character in the edit control.


Adding a Status Bar
MSDN Example or search for Status Bars which is part of common control dynamic-link library

HID Notes

Compilation of various information I found when searching for/ working on creating custom HID application that would allow logging of sensor data coming over RF from a remote to a dongle connected to the PC (shows up as HID mouse/keyboard).
  • Good starting point for developing a custom HID application: Using the HID class eases the job of writing USB device drivers
  • PDF Version of above article can be found here.
  • Found another interesting and comprehensive article at CodeProject; HIDAche
  • HID Resource/Links: http://www.lvr.com/hidpage.htm ==> This is the website of the guy who wrote the “USB Complete” book.
  • HIDP_CAPS : The member InputReportByteLength will report size 1 greater than the device report size – the first byte is used for report identifier (e.g. HID report) and thus the size is increased by 1
  • Overlapped I/O operation is in progress: Got this error when I changed the report descriptor so that report count was 8 (while still trying to receive standard mouse packet with size 4 bytes). The mismatch of the sizes of report and the actual packet being received causes the ReadFile() to not accept that packet. (Makes sense as we can have more than one reports with different sizes and with Report ID not set)
  • Reading HID Mouse/Keyboard: Use RAWINPUTDEVICE as explained in the following places:

    1. MSDN Notes
    2. CodeGuru Example

Disclaimer - Beware!

Not really a blog but more like my notebook in public. Got this idea from a friend and liked it. So here I am using the blog; as a diary of interesting things to remember; as post it; as notes for technical stuff I am sure I won't remember when I need it again...
If anyone finds this interesting or useful - its a bonus - for them, not for me :)