473,320 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2447
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.