473,806 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
16 2012
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.FromHan dle 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*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.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******** ******@TK2MSFTN GP03.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******** ******@TK2MSFTN GP04.phx.gbl...
>>
Bollocks..


"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13******* ******@corp.sup ernews.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.Windo ws.Forms.Button and then use its methods and properties?

Yes.

For example:

Button ButtonFromHandl e(IntPtr handle)
{
return Control.FromHan dle(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...ther e'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******** ******@TK2MSFTN GP02.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******** ******@TK2MSFTN GP03.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******* *******@TK2MSFT NGP04.phx.gbl.. .
>>>
Bollocks..


"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13****** *******@corp.su pernews.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.Wind ows.Forms.Butto n and then use its methods and properties?

Yes.

For example:

Button ButtonFromHandl e(IntPtr handle)
{
return Control.FromHan dle(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******** *****@TK2MSFTNG P05.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******** ******@TK2MSFTN GP02.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
distinguishi ng 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******* *******@TK2MSFT NGP03.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****** ********@TK2MSF TNGP04.phx.gbl. ..

Bollocks..


"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote in message
news:13***** ********@corp.s upernews.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.Win dows.Forms.Butt on and then use its methods and properties?
>
Yes.
>
For example:
>
Button ButtonFromHandl e(IntPtr handle)
{
return Control.FromHan dle(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******** *****@TK2MSFTNG P05.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******** *****@TK2MSFTNG P05.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******* *******@TK2MSFT NGP02.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
distinguishin g 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****** ********@TK2MSF TNGP03.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***** *********@TK2MS FTNGP04.phx.gbl ...
>
Bollocks. .
>
>
>
>
>
>
"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote 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
>>propertie s?
>>
>Yes.
>>
>For example:
>>
> Button ButtonFromHandl e(IntPtr handle)
> {
> return Control.FromHan dle(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******** *****@TK2MSFTNG P05.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******** *****@TK2MSFTNG P05.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******* *******@TK2MSFT NGP02.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
distinguishin g 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****** ********@TK2MSF TNGP03.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***** *********@TK2MS FTNGP04.phx.gbl ...
>
Bollocks. .
>
>
>
>
>
>
"Peter Duniho" <Np*********@Nn OwSlPiAnMk.comw rote 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
>>propertie s?
>>
>Yes.
>>
>For example:
>>
> Button ButtonFromHandl e(IntPtr handle)
> {
> return Control.FromHan dle(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
3484
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 good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
4
1395
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 of programming? TNX -- Tamir Khason
5
12250
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: void DisplayPropertyPage(IntPtr hwndParent) I need to pass hwndParent to a dialog, but I cant for the life of me figure out how to cast it to a windows.forms.form handle.
8
9480
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 System.Security.Principal.WindowsImpersonationContext Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
11
4742
by: James Juno | last post by:
Hello, I want to do something like this: class A { ... }; class B : public A
2
1607
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) != sizeof(*void)) equally work (under unix,mac&windows and with gcc, vc and borland)? Currently the relevant code (in mlabraw.cpp available from <http://mlabwrap.sf.net>) looks like somthing like this: /* C++ -> py */ Engine *ep;
7
13717
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
2914
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 method called and then the derived class have its method called. What am I not understanding? Int the following code, my Event Tester class is getting called twice for keyboard events when I step through the debugger:...
32
2398
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" variable, which of the following lines are correct:
0
9598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10623
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.