471,337 Members | 802 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Casting a Windows Handle. Can it be Done?

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
Aug 31 '07 #1
16 1885
ink wrote:
>
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.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
Aug 31 '07 #2
ink wrote:
>
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.
Aug 31 '07 #3
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
Aug 31 '07 #4
ink
Hi Peter

Thanks for the advice. I will defiantly give this a test.

thanks,
ink


"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
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
Sep 1 '07 #5
"ink" <in*@notmyemail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
I will defiantly give this a test.
That's the spirit! Don't let anyone get in your way... ;-)
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 1 '07 #6
ink

Bollocks..


"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
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
Sep 2 '07 #7
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" <in*@notmyemail.comwrote in message
news:eT**************@TK2MSFTNGP04.phx.gbl...
>
Bollocks..


"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
>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

Sep 2 '07 #8
ink wrote:
>
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
Sep 2 '07 #9
not to butt in a make things worse but,
'microsoft.public.dotnet.framework.compactframewor k' is actually a .NETCF
newsgroup...
"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
ink wrote:
>>
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
Sep 2 '07 #10
ink
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



"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
ink wrote:
>>
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
Sep 2 '07 #11
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


"<ctacke/>" <ctacke[at]opennetcf[dot]comwrote in message
news:ON**************@TK2MSFTNGP03.phx.gbl...
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" <in*@notmyemail.comwrote in message
news:eT**************@TK2MSFTNGP04.phx.gbl...
>>
Bollocks..


"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
>>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

Sep 2 '07 #12
ink wrote:
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
Sep 3 '07 #13
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" <in*@notmyemail.comwrote in message
news:u7**************@TK2MSFTNGP02.phx.gbl...
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]comwrote in message
news:ON**************@TK2MSFTNGP03.phx.gbl...
>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" <in*@notmyemail.comwrote in message
news:eT**************@TK2MSFTNGP04.phx.gbl...
>>>
Bollocks..


"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
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


Sep 3 '07 #14
ink
hi ctacke

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

What does IPC mean?

Thanks,
ink

"<ctacke/>" <ctacke[at]opennetcf[dot]comwrote in message
news:uQ*************@TK2MSFTNGP05.phx.gbl...
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" <in*@notmyemail.comwrote in message
news:u7**************@TK2MSFTNGP02.phx.gbl...
>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]comwrote in message
news:ON**************@TK2MSFTNGP03.phx.gbl...
>>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" <in*@notmyemail.comwrote in message
news:eT**************@TK2MSFTNGP04.phx.gbl...

Bollocks..


"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
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

Sep 4 '07 #15
ink

is it.

Inter-process communication


"ink" <in*@notmyemail.comwrote in message
news:u8*************@TK2MSFTNGP05.phx.gbl...
hi ctacke

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

What does IPC mean?

Thanks,
ink

"<ctacke/>" <ctacke[at]opennetcf[dot]comwrote in message
news:uQ*************@TK2MSFTNGP05.phx.gbl...
>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" <in*@notmyemail.comwrote in message
news:u7**************@TK2MSFTNGP02.phx.gbl...
>>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]comwrote in message
news:ON**************@TK2MSFTNGP03.phx.gbl...
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" <in*@notmyemail.comwrote in message
news:eT**************@TK2MSFTNGP04.phx.gbl...
>
Bollocks..
>
>
>
>
>
>
"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
>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
>

Sep 4 '07 #16
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" <in*@notmyemail.comwrote in message
news:u8*************@TK2MSFTNGP05.phx.gbl...
hi ctacke

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

What does IPC mean?

Thanks,
ink

"<ctacke/>" <ctacke[at]opennetcf[dot]comwrote in message
news:uQ*************@TK2MSFTNGP05.phx.gbl...
>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" <in*@notmyemail.comwrote in message
news:u7**************@TK2MSFTNGP02.phx.gbl...
>>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]comwrote in message
news:ON**************@TK2MSFTNGP03.phx.gbl...
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" <in*@notmyemail.comwrote in message
news:eT**************@TK2MSFTNGP04.phx.gbl...
>
Bollocks..
>
>
>
>
>
>
"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
>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
>


Sep 4 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jacob Jensen | last post: by
4 posts views Thread by Tamir Khason | last post: by
5 posts views Thread by The Real Andy | last post: by
11 posts views Thread by James Juno | last post: by
2 posts views Thread by Alexander Schmolck | last post: by
7 posts views Thread by S. Lorétan | last post: by
7 posts views Thread by Christopher Pisz | last post: by
32 posts views Thread by alex.j.k2 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.