How to make Dllimport correctly in c#?

Apr 13 2011 4:01 AM
Hey, ive had some problems building a correct .dll from a c++-project to my c#-project. I played around with the c++-project-properties and got a .dll file which i can add and refer to in my c# web-project. I use Dllimport to make a function call to the .dll like this:
[DllImport("Filename.dll", CallingConvention=CallingConvention.Cdecl)]
static extern void Function1(byte[] src,
  UInt32 srcLen,
  [MarshalAs(UnmanagedType.LPStr)] StringBuilder dst,
  UInt32 dstLen);
The c++ function header is:
__declspec(dllimport) void Function1(unsigned char *src,
                     unsigned long srclen,
                     unsigned char *dst,
                     unsigned long dstlen);
Im calling Function1 in c# with this:
StringBuilder d = new StringBuilder(50);
byte[] src = System.Text.Encoding.ASCII.GetBytes(stringFromDBWithencrypteddata);
Function1(src, srcLength, d, (uint)50);
No exceptions or errors are occuring, though im not getting the output im expecting. The function is a decrypting method that takes an encrypted string(src) and returns the decrypted version of this (dst).
Is it the way ive generated the .dll file or is it the wrong way im calling the function? Im running out of ideas ive tried most combinations.
Thanks in advice!


Answers (5)