DllImport (C++ code -> C# code)

  • Thread starter Thread starter shaps
  • Start date Start date
S

shaps

hi!

how can i convert C++ code to
[b:086285aeef]C#[/b:086285aeef]?

[b:086285aeef]cpp: [/b:086285aeef]

#include "library.h"

TProcReadIEEE32 ReadIEEE32;
HINSTANCE hOwenLib;
...
hOwenLib = LoadLibrary("library.dll");
ReadIEEE32 = (TProcReadIEEE32)GetProcAddress(hOwenLib,"ReadIEEE32");
...

float val;
int tm;
...
res = ReadIEEE32(8,0,"rd", val, tm, 0);
printf("rd=[%f], t=[%5d]\n", val, tm);

[b:086285aeef]library.h: [/b:086285aeef]
...
typedef int (_stdcall *TProcReadIEEE32)
unsigned long, unsigned long, char *, float&, int&, int);
...
 
Hi Shaps,

if it's not a COM-dll, then you can use P/Invoke.

if you want to call the method "foo()" from your DLL, you must define
it in a C#-class with:

public class MyClass
{
[DllImport("library.dll")]
public static extern void foo();
}

that's it.

Now you can invoke your Dll-method foo() by calling MyClass.foo().
Of course it gets more complicated if you want to call a method with
more "sophisticated" parameters or return-types.
You must declare every DLL-method separately, you can't import the
methods of the whole DLL.

regards,
Jurujuba
 
Back
Top