dll from C to C#

P

piotrek

Hi.
I have a problem.
Function in C is :
__declspec(dllimport) void cipher(char *key, char *input, char *output, int
choice)

Then i created code in C#:

[DllImport("rc2lib.dll")]

public static extern void cipher(????????);



How should i declare function in C# code?

When i try i get error ( cant fint entry point in dll );

PK



Is there posibility to make it correctly. I Really have to leave the
definition __declspec(dllimport) void cipher(char *key, char *input, char
*output, int choice) unchanged.
 
G

Guest

How should i declare function in C# code?

Probably something like this

[DllImport("rc2lib.dll", CallingConvention=CallingConvetion.Cdecl,
EntryPoint="...")]
static extern void cipher(string key, string input, StringBuilder output,
int choice);

Or if you're dealing with binary data

[DllImport("rc2lib.dll", CallingConvention=CallingConvetion.Cdecl,
EntryPoint="...")]
static extern void cipher(byte[] key, byte[] input, byte[] output, int
choice);

When i try i get error ( cant fint entry point in dll );

The function may be exported with a mangled name. Try running Dumpbin
/exports on the DLL to see what the exporten entry point is named. Then put
that in the EntryPoint property of the DllImport attribute.


Mattias
 
G

Guest

The function in C should declare dllexport not dllimport, since you wanna
export the function.

and I think you have to put the following codes into the file (which declare
the export function) in order to the .net runtime that this file is the entry
point of the dll file:

BOOL WINAPI DllMain(
HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved
);

cheers,
ivan
 
D

Dmytro Lapshyn [MVP]

Hi,

Not sure about this part:

__declspec(dllimport) - whether it would be the WinAPI calling convention,
but as for the arguments:

[DllImport("rc2lib.dll")]
public static extern void cipher(IntPtr key, IntPtr input, IntPtr output,
int choice);

The main trick is getting virtual memory addresses for all the buffers in
..NET code. I remember I used the Marshal class and the GCHandle structure
for this.
 
K

Kevin Spencer

[DllImport("rc2lib.dll")]
public static extern void cipher(string lpszKey, string lpszInput, string
lpszOutput, int choice);

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
P

piotrek

I changed import to export and it works fine.
It was my first time with dll's and the code from codeproject seems to be
wrong, but thanks again.
PK
 
K

Kevin Spencer

Is the DLL in your system path?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 

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