Casting a Windows Handle. Can it be Done?

  • Thread starter Thread starter ink
  • Start date Start date
I

ink

Hi all,

If I have a Windows 32 pointer to and object (Handle) and I know what that
object is (Button) can I some how cast that pointer to a type of
System.Windows.Forms.Button and then use its methods and properties?

I am using C# compact framework 2.0
I have obtained the handle using the Windows 32 API and PInvoce.
I am using one dot.net application to control another application.

Thanks,
ink
 
ink said:
Hi all,

If I have a Windows 32 pointer to and object (Handle) and I know what
that object is (Button) can I some how cast that pointer to a type of
System.Windows.Forms.Button and then use its methods and properties?

I am using C# compact framework 2.0
I have obtained the handle using the Windows 32 API and PInvoce.
I am using one dot.net application to control another application.

Thanks,
ink

No, sorry, the best you can do in this context is to send messages to
the other control using Win32 api.
 
ink said:
Hi all,

If I have a Windows 32 pointer to and object (Handle) and I know what
that object is (Button) can I some how cast that pointer to a type of
System.Windows.Forms.Button and then use its methods and properties?

I am using C# compact framework 2.0
I have obtained the handle using the Windows 32 API and PInvoce.
I am using one dot.net application to control another application.

Thanks,
ink

No. You will have to use interop/pinvoke, that is you have to call
Windows API, also things like SendMessage.
 
ink said:
If I have a Windows 32 pointer to and object (Handle) and I know what
that object is (Button) can I some how cast that pointer to a type of
System.Windows.Forms.Button and then use its methods and properties?

Yes.

For example:

Button ButtonFromHandle(IntPtr handle)
{
return Control.FromHandle(handle) as Button;
}

This will return a Button instance reference if the handle is valid and
represents a Button control, or null if not.

Pete
 
Bollocks..






Peter Duniho said:
Yes.

For example:

Button ButtonFromHandle(IntPtr handle)
{
return Control.FromHandle(handle) as Button;
}

This will return a Button instance reference if the handle is valid and
represents a Button control, or null if not.

Pete
 
Indeed. It's not supported in the CF, and if it were, it still won't turn a
handle from outside your managed app's scope of resources into a Control.
That said, you could still create your own wrapper that takesn in a Handle
and then exports things like Text by wrapping the necessary P/Invoke calls.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 
ink said:
Bollocks..

I don't know what your problem is. You claimed to have a Button
instance in your original post. If you don't actually have a Button
instance, it's true that FromHandle won't get you an instance, but that
doesn't change the veracity of my answer. It just means you need to
learn to ask better questions (ie, don't make statements that aren't true).

As far as whether it's supported in Compact Framework, well...this isn't
a CF newsgroup. You will need to take responsibility for any specific
difference in your own environment from the more general environment
relevant to the forum in which you ask your question.

I'm sorry that in your specific case my answer wasn't applicable. But I
don't see any need for you to be so rude about it.

Pete
 
not to butt in a make things worse but,
'microsoft.public.dotnet.framework.compactframework' is actually a .NETCF
newsgroup...
 
Hey Peter.



My appologies for the short response.



The first 2 responses that i got to the question were what i was expecting
(cant be done).

Then yours poped up and i thought that was interesting. i had never heard of
the method Control.FromHandle before and i found it strange that no one at
my company had mentioned it when i asked them the exact question i had
posted.



So naturally i rushed of to test it, but when it didn't appear in
intellsence and then the application would not compile i thought i had done
something wrong so i checked your response again to see if i had missed
something and by then Mark had posted a response, and that made me think
that you were pulling my leg.



I have now checked it in MSDN as well and seen that it does exist just not
in CF2.0 looks like it might be in the next one though.



As i did post to both CF and CSharp group the fault is mine for not pointing
out i was using CF2.0 for development.



Thanks,

ink
 
Hi ctacke

This is exactly what i have done already.

I loop around the a form from an App that is running in a different process.
I have created a class called Window and I create an instance of the window
class for every control on the form.

The Window class has 3 properties Handle, Caption and ClassName.

The problem is that when I have 2 ComboBox's they have neither of them have
a Caption so I have no way to tell them apart.

I am trying to find some why of getting at some other human readable
property or piece of information that is always the same(the problem is that
Handles keep changing). I cant seem to fined any way of distinguishing
between the 2 ComboBox's

I thought if I could convert it to a control I might have access to a Name
property. Both Applications are written in CF2.0

Any idea's?


Thanks,
ink
 
ink said:
My appologies for the short response.

Apology accepted.
[...]
As i did post to both CF and CSharp group the fault is mine for not
pointing out i was using CF2.0 for development.

The problem here is not the lack of mention of the version of the CF
you're using (especially since you did include the version in your
original post). The problem is that you cross-posted and then
apparently were surprised that you got an answer not perfectly tailored
to your situation.

You cannot expect people reading your post in non-CF newsgroups to know
the limitations of the CF. You may get answers when you cross-post like
that that don't work in CF.

So, either don't cross-post, or treat answers that are generally correct
but not for your specific scenario with a little more civility.

(Actually, IMHO even incorrect answers deserve civility...there's really
no reason to be rude, whatever the replies).

Pete
 
If you've got code control over both apps then you really need to start
using IPC to have the two apps send meaningful info to one another in some
way. There's no other mechanism, short of doing things like relying on
screen location or text known to be in a list or something like that.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 
hi ctacke

this is exactly what i thought.
I do have control of both applications source code.

What does IPC mean?

Thanks,
ink
 
is it.

Inter-process communication








ink said:
hi ctacke

this is exactly what i thought.
I do have control of both applications source code.

What does IPC mean?

Thanks,
ink



If you've got code control over both apps then you really need to start
using IPC to have the two apps send meaningful info to one another in
some way. There's no other mechanism, short of doing things like relying
on screen location or text known to be in a list or something like that.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com



ink said:
Hi ctacke

This is exactly what i have done already.

I loop around the a form from an App that is running in a different
process. I have created a class called Window and I create an instance
of the window class for every control on the form.

The Window class has 3 properties Handle, Caption and ClassName.

The problem is that when I have 2 ComboBox's they have neither of them
have a Caption so I have no way to tell them apart.

I am trying to find some why of getting at some other human readable
property or piece of information that is always the same(the problem is
that Handles keep changing). I cant seem to fined any way of
distinguishing between the 2 ComboBox's

I thought if I could convert it to a control I might have access to a
Name property. Both Applications are written in CF2.0

Any idea's?


Thanks,
ink




"<ctacke/>" <ctacke[at]opennetcf[dot]com> wrote in message
Indeed. It's not supported in the CF, and if it were, it still won't
turn a handle from outside your managed app's scope of resources into a
Control. That said, you could still create your own wrapper that takesn
in a Handle and then exports things like Text by wrapping the necessary
P/Invoke calls.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com




Bollocks..






ink wrote:
If I have a Windows 32 pointer to and object (Handle) and I know
what that object is (Button) can I some how cast that pointer to a
type of System.Windows.Forms.Button and then use its methods and
properties?

Yes.

For example:

Button ButtonFromHandle(IntPtr handle)
{
return Control.FromHandle(handle) as Button;
}

This will return a Button instance reference if the handle is valid
and represents a Button control, or null if not.

Pete
 
IPC == Interprocess Communication. Daniel Moth has covered it for the CF
well in his blog.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com



ink said:
hi ctacke

this is exactly what i thought.
I do have control of both applications source code.

What does IPC mean?

Thanks,
ink



If you've got code control over both apps then you really need to start
using IPC to have the two apps send meaningful info to one another in
some way. There's no other mechanism, short of doing things like relying
on screen location or text known to be in a list or something like that.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com



ink said:
Hi ctacke

This is exactly what i have done already.

I loop around the a form from an App that is running in a different
process. I have created a class called Window and I create an instance
of the window class for every control on the form.

The Window class has 3 properties Handle, Caption and ClassName.

The problem is that when I have 2 ComboBox's they have neither of them
have a Caption so I have no way to tell them apart.

I am trying to find some why of getting at some other human readable
property or piece of information that is always the same(the problem is
that Handles keep changing). I cant seem to fined any way of
distinguishing between the 2 ComboBox's

I thought if I could convert it to a control I might have access to a
Name property. Both Applications are written in CF2.0

Any idea's?


Thanks,
ink




"<ctacke/>" <ctacke[at]opennetcf[dot]com> wrote in message
Indeed. It's not supported in the CF, and if it were, it still won't
turn a handle from outside your managed app's scope of resources into a
Control. That said, you could still create your own wrapper that takesn
in a Handle and then exports things like Text by wrapping the necessary
P/Invoke calls.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com




Bollocks..






ink wrote:
If I have a Windows 32 pointer to and object (Handle) and I know
what that object is (Button) can I some how cast that pointer to a
type of System.Windows.Forms.Button and then use its methods and
properties?

Yes.

For example:

Button ButtonFromHandle(IntPtr handle)
{
return Control.FromHandle(handle) as Button;
}

This will return a Button instance reference if the handle is valid
and represents a Button control, or null if not.

Pete
 
Back
Top