473,508 Members | 2,236 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!
Nov 15 '05 #1
5 1698
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!

Nov 15 '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!


Nov 15 '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!



Nov 15 '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!
>
>



Nov 15 '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
Nov 15 '05 #6

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

Similar topics

5
2462
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!
97
27692
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
6
3165
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
5569
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
2368
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: ...
9
1444
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
9782
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
2288
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
2133
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,...
1
5204
by: sophia | last post by:
Dear all, the following are the differences b/w #define and typedef ,which i have seen in Peter van der lindens book. is there any other difference between thes two ? The right way to...
0
7227
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
7127
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
7331
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,...
0
7391
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...
1
5056
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
4713
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
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1564
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 ...
0
424
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...

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.