Click to See Complete Forum and Search --> : FromHandle Permission problem (maybe)


wolfofthenorth
December 8th, 2002, 09:56 PM
Hi,

Can anyone figure out why this returns null?

//----------------------------------------------------------------------------
//UIPermission uip = new UIPermission(UIPermissionWindow.AllWindows);
//uip.Assert();

IntPtr pi = new IntPtr(GetDesktopWindow());
Control c = Control.FromChildHandle(pi);
if(c == null) MessageBox.Show("Didn't work");
//-----------------------------------------------------------------------------

I think it may have to do with security permission. I know for a fact the Hwnd to the desktop is correct. If I use this line I can create a control...

Control c = Control.FromChildHandle(this.Handle);

Although, I don't want that particular control; I want the desktop. I have also tried Control.FromHandle, Form.FromHandle, and Form.FromChildHandle without any better luck.

Can anyone please help? Thanks!

MartinL
December 9th, 2002, 04:19 AM
1.) desktop is not a control!

2.) FromChildHandle() method retrieves the control that contains the specific handle. So when you call that method with HWND of desktop window, you are expect to get the parent window of the desktop window. Try to use FromHandle method to do this.

However, due to the point 1, be really carefull when you are dealing with desktop window as control. It is not the control...

Martin

wolfofthenorth
December 9th, 2002, 12:44 PM
Thanks for your help.

I was first using Form.FromHandle without any luck. So since form inherits control, I tried using it instead... but that didn't work either.

I was reading the SDK and it stated that FromChildHandle was more reliable than FromHandle. It stated that it searched up the handle chain until it found the control. Maybe I'm using it incorrectly, but I have tried both methods and have not been able get an object representing the window I want from a handle.

I was just testing on the desktop window. I really want a reference to another window. I'm tring to build a preview for a screen saver. Durring preview, Windows passes a Hwnd on a command line. That Hwnd needs to be the parent of my form.

I want to set it to be my form's parent, but I can't get an object to set. All I can get is the Hwnd, and I can't set the parent equal to a hWnd.

With this new informtion, can you give any further advise?

Thanks!

MartinL
December 9th, 2002, 01:05 PM
using System.Runtime.InteropServices;

class SomeClass
{
// ...

[DllImport("user32.dll")]
public static int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

// ...
public viod someFunc(...)
{
// ...
IntPtr pi = new IntPtr(GetDesktopWindow());
SetParent(theWindowYouWantToSetParentFor.Handle, pi);
// ...
}
}


Martin

wolfofthenorth
December 9th, 2002, 02:07 PM
I tried that, too, but it didn't update all the framework objects. They mentioned something about the Sendmessage API to refresh the objects; which I haven't got the message worked out yet?

If I reference the parent, it thinks it's null. For example the following blows up -- this.Parent.size

Every road I take has some kind of obstacle. :(

Thanks for your help!

wolfofthenorth
December 9th, 2002, 05:04 PM
Well I got around the problem, but I had to use more APIs to do it.

So much for the FromHandle method. :confused: :confused:

If someone does know why that doesn't seem to be working, I would still be interested in hearing why.


Thanks for the help.