473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

figuring out c# declarations from a DLL of C functions

Hi,

I'm trying to use a C function ( long int myFunc() ) from a DLL in my
C# program. I use DllImport("dllname") a C function and declare it as

[DllImport("dllname")]
public static extern int myFunc();

At first I used

public static extern long myFunc();

but that returned a bizarre value. I incorrectly assumed a C long int
mapped
to C# long. Using C# int worked. But here's my question -

Since the DLL is known, is there a tool that will list the C#
equivalents for
the functions as long as the variable types are standard (i.e., int,
long, float, etc. )? Does it seems reasonable that C# should be able
to gather this info from the DLL?

Finally, what is the name of the process of using the [DllImport...]
specification for loading external functions?
Is this PInvoke?
Are there any books/articles describing this in detail, particularly
with
accessing and running C functions that return pointers to structures.

Thanks,

Ted
Nov 15 '05 #1
1 1236
yep, it's Platform Invoke. look in your .net sdk documentation, look under P/Invoke, it contains a standard C/C++ datatype to C# equivalent table, and also a lot of information on more complicated string, array, struct/union, input/output, callback, GC related marshalling topics

I'm also looking for ways to make the process easier. Dumpbin doesn't supply a whole lot of information (maybe I don't know how to use it, I don't know how much information is actually included in the DLLs). it's a pain to dig up information on poorly documented C APIs, like the original typedefs, when you only have the binary dlls to work with, not the C header files

----- Ted Sung wrote: ----

Hi

I'm trying to use a C function ( long int myFunc() ) from a DLL in m
C# program. I use DllImport("dllname") a C function and declare it a

[DllImport("dllname")
public static extern int myFunc()

At first I used

public static extern long myFunc()

but that returned a bizarre value. I incorrectly assumed a C long in
mappe
to C# long. Using C# int worked. But here's my question

Since the DLL is known, is there a tool that will list the C
equivalents fo
the functions as long as the variable types are standard (i.e., int
long, float, etc. )? Does it seems reasonable that C# should be abl
to gather this info from the DLL

Finally, what is the name of the process of using the [DllImport...
specification for loading external functions
Is this PInvoke
Are there any books/articles describing this in detail, particularl
wit
accessing and running C functions that return pointers to structures

Thanks

Te

Nov 15 '05 #2

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

Similar topics

134
7745
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
9
2984
by: MLH | last post by:
Would the following work if placed in a form module rather than a global module? Declare Sub InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long)
14
2132
by: Arthur J. O'Dwyer | last post by:
Well, I'm trying to write that program that was requested a few weeks back, the one that could take struct definitions and create portable functions to read and write those structs. Hence the...
1
11512
by: Jeff | last post by:
Hello everybody, I have a question concerning function declarations. When exactly do you have to declare functions if you want to use them? I have two functions main() and foo(), respectively...
20
12378
by: Elliot Marks | last post by:
If a struct or its members are passed to a function, must it be declared globally? #include <stdio.h> struct mystruct{ int a; int b; }; int structfunc(struct mystruct foo);
7
1376
by: Alan Holloway | last post by:
Hi all, Firstly thanks for your golden insight on my earlier post re bitwise operations. I now have another question! I've just been reading the excellent Peter van der Linden excerpt ...
10
1556
by: Xiaoshen Li | last post by:
Dear All, I am confused with prototypes in C. I saw the following code in a C book: void init_array_1(int data) { /* some code here */ }
12
12084
by: fox | last post by:
How do I (portably) make a forward reference to a static (I mean file-scope) variable? I've been using "extern" for years, for example: extern int x; int foo(void) { return x++; }
15
2152
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
0
7009
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
7178
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
7390
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...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
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
4602
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
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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
302
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.