Hi,
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:
The function it calls is in a C++ dll ()closed source code); exploring the exposed functions I found:
[INDENT]...
the function I want to call is
]
and it's mangled code is
to test the import of the function I wrote:
when I run it the error is the one above;
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
or even to mark the code as 'unsafe' and use as input
but the error is always the same;
Could somebody help me with this?
Thanks
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:
Code:
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]...
Code:
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) [U][B]class _bstr_t CLSCrypt::DefaultDecrypt(class _bstr_t const &)[/B][/U] 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
Code:
class _bstr_t CLSCrypt::DefaultDecrypt(class _bstr_t const &)
and it's mangled code is
Code:
?DefaultDecrypt@CLSCrypt@@SA?AV_bstr_t@@ABV2@@Z
Code:
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
Code:
public static extern String DefaultDecrypt([MarshalAs(UnmanagedType.BStr)] String s);
Code:
IntPtr ptr = Marshal.PtrToStringBSTR(s);
Could somebody help me with this?
Thanks
Comment