0

When I try to import the function

extern __declspec(dllexport)  void SomeNativeFunction(const std::string param1, const std::string param2);  

defined and declared in SomeNative.Dll into a .NET console application using

class Program
{
        [DllImport("SomeNative", CallingConvention = CallingConvention.Cdecl)]
        extern static void SomeNativeFuntion(string param1, string param2);
        static void Main(string[] args)
        {   
            StartEventHandling("param1", "param2");
        }
}

I get the error

System.AccessViolationException' occurred in ManagedS7DosEventManagerClient.dll Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Which is probably an indication, that I need to marshal the parameters correctly.

This overview of default marshalings for strings unfortunately doesn't say anything about marshaling of standard library strings.

4
  • What have you tried so far? Commented Mar 2, 2022 at 7:20
  • You can't marshal a std::string, you need to do something different Commented Mar 2, 2022 at 7:26
  • i.e. change the interface of the unmanaged dll? Commented Mar 2, 2022 at 7:28
  • That's certainly the easiest option Commented Mar 2, 2022 at 7:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.