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

Home Posts Topics Members FAQ

how to define function pointer in C#

if there is a function in a win32 dll, it is definition is

int add(int a, int b);
how to define that function pointer in C#?
thank you very much!
Jul 21 '05 #1
5 2471
Hi,

You would use a delegate.

delegate int AddCallback( int a, int b );

Hope this helps

Chris Taylor

"ZhangZQ" <zh*******@hotm ail.com> wrote in message
news:eN******** ******@TK2MSFTN GP10.phx.gbl...
if there is a function in a win32 dll, it is definition is

int add(int a, int b);
how to define that function pointer in C#?
thank you very much!

Jul 21 '05 #2
Thanks,

In C the GetProcAddress can get the function pointer, for example

typedef int (*MYADD)(int, int);
MYADD add = (MYADD) GetProcAddress( hMyLib, "add");

but how to pass the function pointer to that delegate?
Thank you very much again!


"Chris Taylor" <ch************ *@hotmail.com> wrote in message
news:Of******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

You would use a delegate.

delegate int AddCallback( int a, int b );

Hope this helps

Chris Taylor

"ZhangZQ" <zh*******@hotm ail.com> wrote in message
news:eN******** ******@TK2MSFTN GP10.phx.gbl...
if there is a function in a win32 dll, it is definition is

int add(int a, int b);
how to define that function pointer in C#?
thank you very much!


Jul 21 '05 #3
Hi,

If you are going to bind statically to the function then you could use
interop aka P/Invoke from the System.Runtime. InteropServices name space.

[DllImport("dlln ame.dll")]
public int Add( int a, int b );

If you want to do this dynamically it is a little more work, I believe there
is a solution using the Relection Emit however I would rather want to test
this before commiting to it here :). But in principal, an I believe this
should answer your other post as well. You could use the Reflection classes
to create a temporary Assembly that can use interop to load the function and
use the Reflection Emit to create a .NET function which would perform the
actual invocation of the method. Being 5:31 in the morning I might just be
dreaming here but if I get a chance I will give it a try and post my
results.

The following is a more hardcoded approach to what I am suggesting, it might
get you going
http://groups.google.com/groups?hl=e...40TK2MSFTNGP10

Cheers

Chris Taylor

"ZhangZQ" <zh*******@hotm ail.com> wrote in message
news:uY******** ******@tk2msftn gp13.phx.gbl...
Thanks,

In C the GetProcAddress can get the function pointer, for example

typedef int (*MYADD)(int, int);
MYADD add = (MYADD) GetProcAddress( hMyLib, "add");

but how to pass the function pointer to that delegate?
Thank you very much again!


"Chris Taylor" <ch************ *@hotmail.com> wrote in message
news:Of******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

You would use a delegate.

delegate int AddCallback( int a, int b );

Hope this helps

Chris Taylor

"ZhangZQ" <zh*******@hotm ail.com> wrote in message
news:eN******** ******@TK2MSFTN GP10.phx.gbl...
if there is a function in a win32 dll, it is definition is

int add(int a, int b);
how to define that function pointer in C#?
thank you very much!



Jul 21 '05 #4
Wah!!! great, you are really an expert.

Thank you very much!

"Chris Taylor" <ch************ *@hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Hi,

If you are going to bind statically to the function then you could use
interop aka P/Invoke from the System.Runtime. InteropServices name space.

[DllImport("dlln ame.dll")]
public int Add( int a, int b );

If you want to do this dynamically it is a little more work, I believe there is a solution using the Relection Emit however I would rather want to test
this before commiting to it here :). But in principal, an I believe this
should answer your other post as well. You could use the Reflection classes to create a temporary Assembly that can use interop to load the function and use the Reflection Emit to create a .NET function which would perform the
actual invocation of the method. Being 5:31 in the morning I might just be
dreaming here but if I get a chance I will give it a try and post my
results.

The following is a more hardcoded approach to what I am suggesting, it might get you going
http://groups.google.com/groups?hl=e...40TK2MSFTNGP10
Cheers

Chris Taylor

"ZhangZQ" <zh*******@hotm ail.com> wrote in message
news:uY******** ******@tk2msftn gp13.phx.gbl...
Thanks,

In C the GetProcAddress can get the function pointer, for example

typedef int (*MYADD)(int, int);
MYADD add = (MYADD) GetProcAddress( hMyLib, "add");

but how to pass the function pointer to that delegate?
Thank you very much again!


"Chris Taylor" <ch************ *@hotmail.com> wrote in message
news:Of******** ******@TK2MSFTN GP10.phx.gbl...
Hi,

You would use a delegate.

delegate int AddCallback( int a, int b );

Hope this helps

Chris Taylor

"ZhangZQ" <zh*******@hotm ail.com> wrote in message
news:eN******** ******@TK2MSFTN GP10.phx.gbl...
> if there is a function in a win32 dll, it is definition is
>
> int add(int a, int b);
>
>
> how to define that function pointer in C#?
>
>
> thank you very much!
>
>



Jul 21 '05 #5
> In C the GetProcAddress can get the function pointer, for example

typedef int (*MYADD)(int, int);
MYADD add = (MYADD) GetProcAddress( hMyLib, "add");

but how to pass the function pointer to that delegate?


In addition to what Chris Taylor posted, I thought you might also be
interested in http://www.bearcanyon.com/dotnet/#getprocaddress, this sounds
pretty similar to what you want to achieve.

Fabian
Jul 21 '05 #6

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

Similar topics

97
27772
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
6
3172
by: | last post by:
Say we have the following code defining TMyMsgHandler and TMyClass typedef void (*TOnMsgReceive) (TMyMessage Msg); class TMyMsgHandler { public: TMyMsgHandler(); virtual ~TMyMsgHandler(); TOnMsgReceive *OnMsgReceive; };
42
5601
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
28
2388
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: int main() {
5
1704
by: ZhangZQ | last post by:
if there is a function in a win32 dll, it is definition is int add(int a, int b); how to define that function pointer in C#? thank you very much!
9
1447
by: RalphTheExpert | last post by:
Under MC++, what is the right way to define a (typedef'd) pointer to a static function? When I call a function through a pointer I get a stack overflow. For those having the same problem, I worked around the problem by replacing the pointer to a function with a functor. If you need to know what a functor is, just ask.
3
9797
by: robin liu | last post by:
What's the difference between these two declarations ? 1) typedef void (*pf)(void); 2) typedef void f(void); the first declaration is define a function pointer, what is the second ? define a function model? And can use the second declaration to define a function pointer as follow:
29
2316
by: Ancient_Hacker | last post by:
It sure would be nice if I could have a macro that add a level of indirection to its argument. So if I write: AddIndirection( X ) The macro AddIndirection will do: #define X (*X) so after that point whenever you use X it gets replaced by (*X)
6
2151
by: wongjoekmeu | last post by:
I need to rewrite some typedef to #define. For instance I rewrote typedef void* handle to #define handle void* But I saw for instance another typedef in my code which I don't understand, which is
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
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
8837
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
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6175
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
5638
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
4171
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
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.