0

I have a C++ dll that contains this method in the header (C++ code):

   enum Error
   {
          Error1,
          Error2,
          Error3,
   }

   void foo(const char* str, Error &err) ;

I am trying to call this method using a DllImport attribute but with no success. All other methods on this dll I can call with no problem.

On the DllImport I converted foo method to this:

   enum Error
   {
          Error1,
          Error2,
          Error3,
   }

   [DllImport("dll_name.dll")]
   public static extern void foo(string str, ref Error err);

but this isn't working either. I've also tried to use 'MarshalAs(UnmanagedType.LPStruct)' on the string and tried to change the 'CharSet' on the dllImport attribute => but nothing works

Yanshof
  • 9,659
  • 21
  • 95
  • 195

1 Answers1

0

I would try this, I notice the lack of the void.

enum Error
{
      Error1,
      Error2,
      Error3,
}

[DllImport("dll_name.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void foo([MarshalAs(UnmanagedType.LPStr)] string str, ref Error err);

I would also make sure the method is visible in your application. Noted by karmasponge

EDIT: You can try changing calling convention. If the exception you get is a StackImbalance, this should fix the issue.