Tuesday, September 30, 2014

Display Message From Service/Session 0

The conventional methods to show message box are MessageBox()/AfxMessageBox(). But is it useful if we want to display messages from a service/an application running in session 0?  Yes you can but it is not  convenient. The developer has to enable interactive services
There is an alternative method which uses WTSSendMessage() .

DWORD dwSession = WTSGetActiveConsoleSessionId();
DWORD dwResponse = 0;
LPWSTR lpwszMessage = L"Hellow VC++ From Session 0";
WTSSendMessage(WTS_CURRENT_SERVER_HANDLE, dwSession,L"",0, lpwszMessage, wcslen(lpwszMessage)* sizeof(wchar_t), 0, 0, &dwResponse, FALSE);

Sunday, September 7, 2014

Never initialize UI thread as MTA

Recently we have faced occasional freezing issue in an application.  I think i have to share some background implementation before jumping to the issue straight. It is an outproc STA server which is very similar to mobile app notification. Client applications or dlls can call this to notify some error/warning messages to end-user. This notification window will be always super top-most in z-order. To facilitate that the server iterates though all windows(including client window) and invokes SetWindowPos().


I have created a sample app to replicate the issue and found that SetWindowPos() is freezing during the iteration. It was due to a dead-lock happened between the server and client application.

To understand the dead-lock, we should know how COM communicates internally. Below is a basic description for STA and MTA communication.
STA uses windows messages for COM communication. When a client thread is initialized as STA, it will internally pumps messages while waiting for server response. For MTA it waits using WaitForSingleObject() and there is no message pumping.

In the issue scenario, client UI thread was initialized as MTA and as part of iteration to set z-order server tried to invoke SetWindowPos() on client window. The client was unable to process SetWindowPos() since it was waiting in WaitForSingleObject() which lead to dead-lock. Pretty intriguing right?

The fix is obvious from the title of this post.

Reference
========
https://devblogs.microsoft.com/oldnewthing/20080424-00/?p=22603

Tuesday, July 1, 2014

Hidden VC++ Compiler switch for showing class layout in Visual Studio

In case you want to dump the class layout, there is a hidden compiler switch in Visual Studio . It will show class layout during build in output window. Enable it in Project->Properties>C++>CommandLine>Additionaloptions. Add /d1reportAllClassLayout as shown below

 The class layout would show as shown in below

Thursday, February 6, 2014

Spying Windows Messages During Application/Dialog Startup

The traditional way of spying Windows Messages is to use Spy++. There are some other third-party tools are also available like WinSpector. But neither these tools are useful if we want to monitor the messages during dialog startup. Do not worry, we have Visual studio. Just follow below steps to achieve it.
1. Implement WindowProc() for the window you want to monitor and  just invoke base class WindowPorc().
2. Put a break point inside WindowProc().
3. Right-Click on break point and select “Actions”.

4. Add {message,wm} to "Log a message to Output Window".
5. Press F5 to debug and the messages can be viewed in Output Window.

Keep an eye on your Native API prototype for interop calls

Few weeks back, we have observed "an unexpected process termination" crash in a WPF app. From the crash dump, provides below calls...