This Blog Has Been Moved !

This Blog Has been moved to http://aleemkhan.wordpress.com

One of the things I really miss and loved working with was Win32, not that I am of the view that is flawless, but the control that I have over my application when I am writing code is awsome. Ofcourse, all .NET abstraction has countless advantages and is definately the future, but ocassionally its always nice to dig back to bottom.

Recently someone asked this on microsoft.public.dotnet.csharp.general newsgroup, that if you are calling a library function .NET which is some Dll, and is showing a MessageBox on some error or a "Trial" Message etc, how you would Supress/Hide that Message Box in .NET.

I wrote this following class DisableModalDialog to do the exact thing. To use the class see the following click event to see how u can disable/enable dialog boxes.

private void button1_Click(object sender, System.EventArgs e)
{
DisableModalDialog dmd = new DisableModalDialog();

dmd.DisableMessageBox();

MessageBox.Show("Hello");
MessageBox.Show("Hello");
MessageBox.Show("Hello");
MessageBox.Show("Hello");
MessageBox.Show("Hello");
MessageBox.Show("Hello");

dmd.EnableMessageBox();

}




// Class to Enable/Disable Windows Dialog Boxes
// NOTE : This Class Will Only Disable Dialog Boxes in Current Thread
public class DisableModalDialog
{
// Constructor
public DisableModalDialog() {}

public void DisableMessageBox()
{
this.myCallbackDelegate = new HookProc(this.MyHookCallbackFunction);
hHOOK = SetWindowsHookEx(HookType.WH_CALLWNDPROC, this.myCallbackDelegate, IntPtr.Zero, (uint)AppDomain.GetCurrentThreadId());
}

public void EnableMessageBox()
{
UnhookWindowsHookEx(hHOOK);
}

private IntPtr hHOOK;
private IntPtr OrigDlgProc;

private HookProc myCallbackDelegate = null;
delegate int HookProc(int code, IntPtr v1, IntPtr v2);

private CustomWindowProc myCallBackWndProcDelegate = null;
delegate IntPtr CustomWindowProc(IntPtr hwnd,uint uMsg,IntPtr wParam,IntPtr lParam);

[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(HookType hook, HookProc callback,
IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll")]
private static extern IntPtr UnhookWindowsHookEx(IntPtr hhk);

[DllImport("user32.dll")]
private static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, CustomWindowProc dwNewLong);

[DllImport("user32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

//[DllImport("user32.dll")]
//private static extern IntPtr SetClassLong(IntPtr hWnd, int nIndex, CustomWindowProc dwNewLong);

//[DllImport("user32.dll")]
//private static extern IntPtr DefWindowProc(IntPtr hWnd, uint Msg, IntPtr wParam,IntPtr lParam);

[DllImport("user32.dll")]
private static extern IntPtr DefDlgProc(IntPtr hDlg, uint Msg, IntPtr wParam,IntPtr lParam);


private enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}

[StructLayout(LayoutKind.Sequential)]
private struct CWPSTRUCT
{
public IntPtr lparam;
public IntPtr wparam;
public int message;
public IntPtr hwnd;
}

//private static int WM_CREATE = 0x0001;
//private static int HC_ACTION = 0;
//private static int GCL_WNDPROC = (-24);

private static int GWL_WNDPROC = (-4);
private static int WM_INITDIALOG = 0x0110;
private static int WM_SHOWWINDOW = 0x0018;
private static int WM_CLOSE = 0x0010;

// Hook Function to Hook Current Thread Messages
private int MyHookCallbackFunction(int code, IntPtr wParam, IntPtr lParam)
{
CWPSTRUCT cwpStruct = (CWPSTRUCT)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam,typeof(CWPSTRUCT));
if(cwpStruct.message == WM_INITDIALOG)
{
myCallBackWndProcDelegate = new CustomWindowProc(this.MyCustomDialogProc);
this.OrigDlgProc = (IntPtr) SetWindowLong(cwpStruct.hwnd, GWL_WNDPROC, this.myCallBackWndProcDelegate);

}
return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
}

// Custom Dialog Procedure for Handling Dialog Messages
private IntPtr MyCustomDialogProc(IntPtr hwnd,uint uMsg,IntPtr wParam,IntPtr lParam)
{
if(uMsg == WM_SHOWWINDOW)
{
SetWindowLong(hwnd,GWL_WNDPROC, this.OrigDlgProc);
uMsg = (uint) WM_CLOSE;
}

return DefDlgProc(hwnd,uMsg,wParam,lParam);
}
}

Comments

1 comments have been posted.