473,237 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 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 1971
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
4
by: Tamir Khason | last post by:
Is it possible? or just my fantasy? I did it before with cpp. Handle and change base classes such as dialogs, windows etc. Is it possible to do with C#? Someone knows good reference to such kind...
5
by: The Real Andy | last post by:
Sorry if this question sounds stupid, its early days for me when it comes to c# and com interop. I have a method imported from Win Media PLayer SDK, IWMPPluginUI:DisplayPropertyPage, like so: ...
8
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As...
11
by: James Juno | last post by:
Hello, I want to do something like this: class A { ... }; class B : public A
2
by: Alexander Schmolck | last post by:
what's the best approach to write C(++)-extension code that has to create a python int from a C pointer and vice versa so that it works smoothly on 32 bit and 64 platforms (on which sizeof(int) !=...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.