I am a complete newbie and these are the first lines of code that I write, so please be patient :(
I need to create a C# exe file that calls a c++ function
What I wrote always throws an error at runtime:
Expand|Select|Wrap|Line Numbers
- Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
- at DDecrypt.DefaultDecrypt(String s)
- at DDecrypt.Main()
[indent]...
Expand|Select|Wrap|Line Numbers
- void CLSCrypt::`default constructor closure'(void)
- CLSCrypt::CLSCrypt(bool,class _bstr_t const &)
- class _bstr_t CLSCrypt::DecryptString(class _bstr_t const &,enum STRCODING,bool)
- class _bstr_t CLSCrypt::DefaultDecrypt(class _bstr_t const &)
- class _bstr_t CLSCrypt::DefaultEncrypt(class _bstr_t const &)
- void CLSCrypt::Delete(void)
- class _bstr_t CLSCrypt::EncryptString(class _bstr_t const &,enum STRCODING)
- class _bstr_t CLSCrypt::GetBase64(unsigned char *,unsigned long)
- bool CLSCrypt::GetFromBase64(class _bstr_t const &,unsigned char * &,unsigned long &)
- bool CLSCrypt::GetFromHEX(class _bstr_t const &,unsigned char * &,unsigned long &)
- class _bstr_t CLSCrypt::GetHEX(unsigned char *,unsigned long)
- class _bstr_t CLSCrypt::GetMD5String(unsigned char *,unsigned long,enum STRCODING)
- void CLSCrypt::SetAccess(class _bstr_t const &,unsigned long)
- CLSCrypt::~CLSCrypt(void)
the function I want to call is
Expand|Select|Wrap|Line Numbers
- class _bstr_t CLSCrypt::DefaultDecrypt(class _bstr_t const &)
and it's mangled code is
Expand|Select|Wrap|Line Numbers
- ?DefaultDecrypt@CLSCrypt@@SA?AV_bstr_t@@ABV2@@Z
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Runtime.InteropServices;
- public class DDecrypt
- {
- [DllImport("LSUtils.dll", EntryPoint = "?DefaultDecrypt@CLSCrypt@@SA?AV_bstr_t@@ABV2@@Z", ExactSpelling = true)]
- public static extern String DefaultDecrypt(String s);
- public static void Main()
- {
- String s = "this is a test!";
- String y = DefaultDecrypt(s);
- Console.WriteLine(y);
- }
- }
I suppose it is due to the data type that I want to pass (a String while the function wants something else)
I tried to import the function as
Expand|Select|Wrap|Line Numbers
- public static extern String DefaultDecrypt([MarshalAs(UnmanagedType.BStr)] String s);
Expand|Select|Wrap|Line Numbers
- IntPtr ptr = Marshal.PtrToStringBSTR(s);
Could somebody help me with this?
Thanks