Easiest way to do P/Invoke

  • Thread starter Razvan Dragomirescu
  • Start date
R

Razvan Dragomirescu

Hello everyone,

I'm pretty new to this P/Invoke thing so excuse me if I'm asking the
obvious. I have a C# .NET CF application that needs to call into an
unmanaged DLL. I do not have access to the sources of that DLL (it contains
some proprietary code that we're not allowed to see), but I can ask the
developers of that DLL to modify their code to make it easier for us to call
it from the managed app.

Ideally, I would be looking at 3 function calls into that dll:

public void Initialize(); // initialize the engine
public void Process(byte[] input); // called multiple times with various
byte arrays to process
public byte[] Digest(); // complete the processing and return a digest

Could anyone help me with a bit of skeleton code in unmanaged code to make
this happen? I've read a bunch of tutorials on the subject but I can't say
I'm confident enough to do this on my own just yet.

Thank you,
Razvan
 
G

Guest

Add the dll to your project by right clicking on the project in the solution explorer and chosing add-->add existing item and chosing the dll file.
having done that you can add refrences to your unmanaged functions in the dll using the following skeleton code:

[DllImport("dllname.dll", EntryPoint="functionName")]
private static extern int functionName(ref IntPtr message);

in the above code, you can replace dllname.dll with the name of ur dll, and entrypoint specifies the unmanaged function you want to call form ur code. below the dllimport line you specify the prototype for the unmanaged function you want to call from your code. Now you can call the function from your code. You add similar lines for each of the function u want to call.
Hope this helps.

Ali Gardezi
 
G

Guest

Add the dll to your project by right clicking on the project in the solution explorer and chosing add-->add existing item and chosing the dll file.
having done that you can add refrences to your unmanaged functions in the dll using the following skeleton code:

[DllImport("dllname.dll", EntryPoint="functionName")]
private static extern int functionName(ref IntPtr message);

in the above code, you can replace dllname.dll with the name of ur dll, and entrypoint specifies the unmanaged function you want to call form ur code. below the dllimport line you specify the prototype for the unmanaged function you want to call from your code. Now you can call the function from your code. You add similar lines for each of the function u want to call.
Hope this helps.

Ali Gardezi
 
R

Razvan Dragomirescu

Thank you Ali. How do I get the return value from the Digest function (a
byte array)? And how do I need to define the functions in C++ in order to
make them visible in my managed code? Does byte[] translated to byte*?

Razvan


Ali said:
Add the dll to your project by right clicking on the project in the
solution explorer and chosing add-->add existing item and chosing the dll
file.
having done that you can add refrences to your unmanaged functions in the
dll using the following skeleton code:
[DllImport("dllname.dll", EntryPoint="functionName")]
private static extern int functionName(ref IntPtr message);

in the above code, you can replace dllname.dll with the name of ur dll,
and entrypoint specifies the unmanaged function you want to call form ur
code. below the dllimport line you specify the prototype for the unmanaged
function you want to call from your code. Now you can call the function from
your code. You add similar lines for each of the function u want to call.
 
P

Paul G. Tobey [eMVP]

Yes, byte[] is just like byte[] in C, which can be used as byte*, if you
want. You should think about passing an input array, preallocated in
managed code and filled by the C DLL, rather than using unmanaged code to
allocate the array.

Paul T.


Razvan Dragomirescu said:
Thank you Ali. How do I get the return value from the Digest function (a
byte array)? And how do I need to define the functions in C++ in order to
make them visible in my managed code? Does byte[] translated to byte*?

Razvan


Ali said:
Add the dll to your project by right clicking on the project in the
solution explorer and chosing add-->add existing item and chosing the dll
file.
having done that you can add refrences to your unmanaged functions in
the
dll using the following skeleton code:
[DllImport("dllname.dll", EntryPoint="functionName")]
private static extern int functionName(ref IntPtr message);

in the above code, you can replace dllname.dll with the name of ur dll,
and entrypoint specifies the unmanaged function you want to call form ur
code. below the dllimport line you specify the prototype for the unmanaged
function you want to call from your code. Now you can call the function from
your code. You add similar lines for each of the function u want to call.
Hope this helps.

Ali Gardezi
 
P

Paul G. Tobey [eMVP]

Oh, and declare your functions

#ifdef __cplusplus
extern "C"
{
#endif

declspec( dllexport ) <return value> FunctionName( <parameters> );

#ifdef __cplusplus
}
#endif

Paul T.

Razvan Dragomirescu said:
Thank you Ali. How do I get the return value from the Digest function (a
byte array)? And how do I need to define the functions in C++ in order to
make them visible in my managed code? Does byte[] translated to byte*?

Razvan


Ali said:
Add the dll to your project by right clicking on the project in the
solution explorer and chosing add-->add existing item and chosing the dll
file.
having done that you can add refrences to your unmanaged functions in
the
dll using the following skeleton code:
[DllImport("dllname.dll", EntryPoint="functionName")]
private static extern int functionName(ref IntPtr message);

in the above code, you can replace dllname.dll with the name of ur dll,
and entrypoint specifies the unmanaged function you want to call form ur
code. below the dllimport line you specify the prototype for the unmanaged
function you want to call from your code. Now you can call the function from
your code. You add similar lines for each of the function u want to call.
Hope this helps.

Ali Gardezi
 
A

Alex Feinman [MVP]

To elaborate, declare your managed function as

extern static int MyFunction(byte[] Buffer, ref int SizeBuffer)

and in unmanaged code as
extern "C" __declspec(dllexport) BOOL MyFunction(LPBYTE lpBuffer, LPDWORD
lpdwSizeBuffer )

Basically model your unmanaged function behavior after GetModuleFileName or
such

--
Alex Feinman
---
Visit http://www.opennetcf.org
Paul G. Tobey said:
Yes, byte[] is just like byte[] in C, which can be used as byte*, if you
want. You should think about passing an input array, preallocated in
managed code and filled by the C DLL, rather than using unmanaged code to
allocate the array.

Paul T.


Razvan Dragomirescu said:
Thank you Ali. How do I get the return value from the Digest function (a
byte array)? And how do I need to define the functions in C++ in order to
make them visible in my managed code? Does byte[] translated to byte*?

Razvan


Ali said:
Add the dll to your project by right clicking on the project in the
solution explorer and chosing add-->add existing item and chosing the dll
file.
having done that you can add refrences to your unmanaged functions in
the
dll using the following skeleton code:
[DllImport("dllname.dll", EntryPoint="functionName")]
private static extern int functionName(ref IntPtr message);

in the above code, you can replace dllname.dll with the name of ur
dll,
and entrypoint specifies the unmanaged function you want to call form ur
code. below the dllimport line you specify the prototype for the unmanaged
function you want to call from your code. Now you can call the function from
your code. You add similar lines for each of the function u want to call.
Hope this helps.

Ali Gardezi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top