473,657 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting a pointer to a method (as IntPtr)

How does one go about getting a pointer to method as an IntPtr?

Example:

public class A
{
public void X()
{
Y(Z);
}

public void Y(IntPtr z)
{
}

public void Z()
{
}
}

This won't compile, because the compiler thinks I meant Y(Z()).

I'm guessing this must be possible, because classes inheriting from
System.EventHan dler have ctors which takes a parameter called 'method',
whose type is IntPtr.

Cheers,
Rik
Nov 16 '05 #1
5 13463
Hi....

You would use a delegate.

John

"Rik Hemsley" <ri*@rikkus.inf o> wrote in message
news:ek******** ******@TK2MSFTN GP12.phx.gbl...
How does one go about getting a pointer to method as an IntPtr?

Example:

public class A
{
public void X()
{
Y(Z);
}

public void Y(IntPtr z)
{
}

public void Z()
{
}
}

This won't compile, because the compiler thinks I meant Y(Z()).

I'm guessing this must be possible, because classes inheriting from
System.EventHan dler have ctors which takes a parameter called 'method',
whose type is IntPtr.

Cheers,
Rik

Nov 16 '05 #2
What you want is a delegate. There are no pointers, per se. Nothing you
could use from unmanaged code.

Here's a description with samples:

http://www.akadia.com/services/dotne...nd_events.html

Pete

"Rik Hemsley" <ri*@rikkus.inf o> wrote in message
news:ek******** ******@TK2MSFTN GP12.phx.gbl...
How does one go about getting a pointer to method as an IntPtr?

Example:

public class A
{
public void X()
{
Y(Z);
}

public void Y(IntPtr z)
{
}

public void Z()
{
}
}

This won't compile, because the compiler thinks I meant Y(Z()).

I'm guessing this must be possible, because classes inheriting from
System.EventHan dler have ctors which takes a parameter called 'method',
whose type is IntPtr.

Cheers,
Rik

Nov 16 '05 #3
Pete Davis wrote:
What you want is a delegate.
See the ctor for System.Timers.E lapsedEventHand ler.

I'm trying to instantiate that class (and any other EventHandler-derived
class) using ConstructorInfo .Invoke.

I am writing a unit test framework which allows testing of async code,
so tricky things like this are necessary.
There are no pointers, per se. Nothing you could use from
unmanaged code.


I'm using managed code only.

Quoting myself:

I'm guessing this must be possible, because classes inheriting from
System.EventHan dler have ctors which takes a parameter called 'method',
whose type is IntPtr.

EventHandler-derived classes must be instantiated somehow. I just need
to know how!

Cheers,
Rik
Nov 16 '05 #4
Rik,

When creating delegates dynamically, you don't call the Invoke method on
the ConstructorInfo instance. Rather, you call the appropriate static
CreateDelegate method overload, which will take the Type information for the
delegate, the method info which you want to attach to, the instance, etc,
etc.

Once you have that, you can dynamically invoke your delegate calling the
DynamicInvoke method on the instance of the delegate.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Rik Hemsley" <ri*@rikkus.inf o> wrote in message
news:Ou******** ******@TK2MSFTN GP09.phx.gbl...
Pete Davis wrote:
What you want is a delegate.


See the ctor for System.Timers.E lapsedEventHand ler.

I'm trying to instantiate that class (and any other EventHandler-derived
class) using ConstructorInfo .Invoke.

I am writing a unit test framework which allows testing of async code, so
tricky things like this are necessary.
There are no pointers, per se. Nothing you could use from
unmanaged code.


I'm using managed code only.

Quoting myself:

I'm guessing this must be possible, because classes inheriting from
System.EventHan dler have ctors which takes a parameter called 'method',
whose type is IntPtr.

EventHandler-derived classes must be instantiated somehow. I just need to
know how!

Cheers,
Rik

Nov 16 '05 #5
Nicholas Paldino [.NET/C# MVP] wrote:
Rik,

When creating delegates dynamically, you don't call the Invoke method on
the ConstructorInfo instance. Rather, you call the appropriate static
CreateDelegate method overload, which will take the Type information for the
delegate, the method info which you want to attach to, the instance, etc,
etc.

Once you have that, you can dynamically invoke your delegate calling the
DynamicInvoke method on the instance of the delegate.

Hope this helps.


That's exactly what I was looking for, thanks very much.

Rik
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1857
by: Ken Kast | last post by:
Here's my situation. I have an object function Obj () { this.foo = null; } a function function bar() {...} another function
2
1333
by: Dirk | last post by:
Hello I override the WndProc method of a control-derived class and want to handle the WM_NOTIFY message. The following code leads to a NMHDR pointer that points to something where all members are shown as undefined in the debugger. NMHDR __nogc *pnmh = static_cast<NMHDR __nogc*>(msg->LParam.ToPointer()); if I redefine NMHDR as a __gc struct and do the following everything works
4
1784
by: astro | last post by:
I would like to build some generic code that is able to figure out the correct dataAdapter to apply changes to given a form with several dataAdapters. Any suggestions on the following? Thank you. ====================================== Private Sub SaveChanges(ByVal aContainer As Object)
4
3831
by: Abra | last post by:
I have an application where I need to send a inter-process message (a data stream) that contains among other the address of a function (member of a class). For that, I need to serialize it, so I tried to cast it to an IntPtr, but without success. And of course, on the receiever side, I need to cast it back from IntPtr to the function pointer, in order to be able to perform the callback function call, but this doesn't work either. How...
2
4787
by: ebiweb | last post by:
hi all, I need to send the address of a method to .dll function. but i don't know how can i get the address of a method ?! :( please help me. thnx a lot.
1
1600
by: =?Utf-8?B?VmlrcmFt?= | last post by:
is there any object available in c#/asp.net which can give me event name in that eevnt only. Like if I am in Page_load event, i should get Page_Load or if I am in Button_click event I should get Button_Click. SO that I can logg it in my cutom log file with a generic logic. Thanks
7
4214
by: Maciej Oszutowski | last post by:
Hi, I'm going to port my PE manipulation library (written in C) to managed c++ class library. I would like to have opportunity to read content not only from files, but also from memory, for example using MemoryStream. Is it any possibility to get the char* pointer to the data stored in MemoryStream *without* allocation of unmanaged memory and copying entire stream contents to the newly allocated buffer? Regards,
7
4694
by: Andrus | last post by:
How to get syntactically correct signature which compiles for code template grneration ? I tried code below but it creates syntactically incorrect signature. Andrus. using System; using System.Collections.Generic; public class MainClass
0
8325
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
8844
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
8742
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...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7354
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...
0
5643
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
4173
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...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1734
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.