473,471 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create 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 2450
Hi,

You would use a delegate.

delegate int AddCallback( int a, int b );

Hope this helps

Chris Taylor

"ZhangZQ" <zh*******@hotmail.com> wrote in message
news:eN**************@TK2MSFTNGP10.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**************@TK2MSFTNGP10.phx.gbl...
Hi,

You would use a delegate.

delegate int AddCallback( int a, int b );

Hope this helps

Chris Taylor

"ZhangZQ" <zh*******@hotmail.com> wrote in message
news:eN**************@TK2MSFTNGP10.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("dllname.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*******@hotmail.com> wrote in message
news:uY**************@tk2msftngp13.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**************@TK2MSFTNGP10.phx.gbl...
Hi,

You would use a delegate.

delegate int AddCallback( int a, int b );

Hope this helps

Chris Taylor

"ZhangZQ" <zh*******@hotmail.com> wrote in message
news:eN**************@TK2MSFTNGP10.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****************@TK2MSFTNGP11.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("dllname.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*******@hotmail.com> wrote in message
news:uY**************@tk2msftngp13.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**************@TK2MSFTNGP10.phx.gbl...
Hi,

You would use a delegate.

delegate int AddCallback( int a, int b );

Hope this helps

Chris Taylor

"ZhangZQ" <zh*******@hotmail.com> wrote in message
news:eN**************@TK2MSFTNGP10.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
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
6
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();...
42
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
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: ...
5
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
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...
3
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...
29
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) ...
6
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,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.