467,136 Members | 1,252 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,136 developers. It's quick & easy.

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
  • viewed: 2177
Share:
5 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

97 posts views Thread by s | last post: by
28 posts views Thread by Wonder | last post: by
5 posts views Thread by ZhangZQ | last post: by
29 posts views Thread by Ancient_Hacker | last post: by
6 posts views Thread by wongjoekmeu@yahoo.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.