how to pass parameter "_bstr_t cons &" to invoke a dll written in C++?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • digonio
    New Member
    • Feb 2010
    • 2

    how to pass parameter "_bstr_t cons &" to invoke a dll written in C++?

    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:

    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()
    The function it calls is in a C++ dll ()closed source code); exploring the exposed functions I found:
    [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
    to test the import of the function I wrote:
    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);
    	}
    }
    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
    Code:
    public static extern String DefaultDecrypt([MarshalAs(UnmanagedType.BStr)] String s);
    or even to mark the code as 'unsafe' and use as input
    Code:
    IntPtr ptr = Marshal.PtrToStringBSTR(s);
    but the error is always the same;

    Could somebody help me with this?
    Thanks
    Last edited by tlhintoq; Mar 1 '10, 01:44 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • digonio
      New Member
      • Feb 2010
      • 2

      #3
      (thanks for the hint and the corrections to the format as well)

      Comment

      Working...