Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2003

Tip: Looking for answers? Try searching our database.

how to define function pointer in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ZhangZQ - 31 Oct 2003 03:38 GMT
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!
Chris Taylor - 31 Oct 2003 04:03 GMT
Hi,

You would use a delegate.

delegate int AddCallback( int a, int b );

Hope this helps

Chris Taylor

> if there is a function in a win32 dll, it is definition is
>
[quoted text clipped - 3 lines]
>
> thank you very much!
ZhangZQ - 31 Oct 2003 04:03 GMT
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!

> Hi,
>
[quoted text clipped - 13 lines]
> >
> > thank you very much!
Chris Taylor - 31 Oct 2003 04:33 GMT
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=en&lr=&ie=UTF-8&oe=UTF-8&selm=ueqNoUdtCHA.254
0%40TK2MSFTNGP10


Cheers

Chris Taylor

> Thanks,
>
[quoted text clipped - 24 lines]
> > >
> > > thank you very much!
ZhangZQ - 31 Oct 2003 04:37 GMT
Wah!!! great, you are really an expert.

Thank you very much!

> Hi,
>
[quoted text clipped - 16 lines]
> The following is a more hardcoded approach to what I am suggesting, it might
> get you going

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=ueqNoUdtCHA.254
0%40TK2MSFTNGP10


> Cheers
>
[quoted text clipped - 28 lines]
> > > >
> > > > thank you very much!
Fabian Schmied - 31 Oct 2003 21:43 GMT
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.