Hi Gents,
As great experts in COM Interop that u r, I'd like to have your counsel on how to pass a Win32 window handler (HWND) to a C# dll. Would it be possible to use this handle in C# as a "this"-type object for the form (subclassed?) ? Many thanks!
Bechir BejaouiPosted Dec 7, 2008, 4:52 AM
OparitiPosted Dec 6, 2008, 9:53 AM
Neat implementation! I would propose you have the same vision and different approach (in fact that's what I'm missing!)
This is the classic win32 implementation, no need for COM interop mechanism. Isn't it another way to marshall hwnd to IntPtr (I even wonder if this is the right object, see below), and backwards, as per COM wrappers (without iDisposable)? What I want is to get access to the message execution of the unmanaged window via a normal .net synthax, something like subclassing it "in the background". As I initially said, get the handle (or pick it up somewhow - that's easy) and wrapping it into an "this" object. As the handle is unique at the system level (in addition, in this case the .net classes are just win32 wrappers) then I think we can find a straight way to hook the .net message pump (associated to the handles's instance) into the unmanaged window. The key here is to pass the hwnd into a "this" object (is an instance) via COM Interop, not win32.
Bechir BejaouiPosted Dec 5, 2008, 5:51 PM
Take a look on this code snippet and give me your mind
interface IWindowHandler
{
void Close(int handle);
void Dispose();
int GetForegroundWnd();
int GetWnd(int processID);
void Maximize(int handle);
void Minimize(int handle);
void Normalize(int handle);
}
public sealed class WindowHandler:IDisposable, IWindowHandler
{
/* I used the singleton design pattern within this
* context because I remarqued that serveral instances
* could lead to some unhandled troubles */
private WindowHandler(){}
public static WindowHandler Activate()
{
return new WindowHandler();
}
/* Those constant have to be used within the
exported method from the unmanaged code */
private const int CLOSE_PARAM = 0x0010;
private const int SHOWNORMAL_PARAM = 1;
private const int SHOWMINIMIZED_PARAM = 2;
private const int SHOWMAXIMIZED_PARAM = 3;
//This function is used to send a message
[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd,
int Msg,
int wParam,
int lParam);
/* This function is very important because she returns the
windows handler*/
[DllImport("user32.dll")]
private static extern int GetForegroundWindow();
//This function shows the windows
[DllImport("user32")]
private static extern int ShowWindow(int hwnd, int nCmdShow);
/* This is also an important function as it chatches a given
* window from a given access
*/
public Int32 GetWnd(int processID)
{
Process oProcess = Process.GetProcessById(processID);
Int32 value = Convert.ToInt32(oProcess.MainWindowHandle);
return value;
}
//This function gets the foreground of the window
public Int32 GetForegroundWnd()
{
return GetForegroundWindow();
}
//This method will close the window
public void Close(Int32 handle)
{
SendMessage(handle, CLOSE_PARAM, 0, 0);
}
/*This will show the minimized state of the
*given windows*/
public void Minimize(Int32 handle)
{
ShowWindow(handle, SHOWMINIMIZED_PARAM);
}
/*This will show the maximized state of the
*given windows*/
public void Maximize(Int32 handle)
{
ShowWindow(handle, SHOWMAXIMIZED_PARAM);
}
/*This will show the normal state of the
*given windows*/
public void Normalize(Int32 handle)
{
ShowWindow(handle, SHOWNORMAL_PARAM);
}
//This will dispose the object
#region IDisposable Members
public void Dispose()
{
GC.ReRegisterForFinalize(this);
}
#endregion
}
OparitiPosted Dec 5, 2008, 12:59 AM
Bechir BejaouiPosted Dec 4, 2008, 7:45 PM