0

I have a C++ DLL which I am trying to call from C#.

Here is the C++ prototype:

bool t_init(const char* a, const char* b, const char* c, const char* d);

Here is my C# declaration:

[DllImport("a.dll")]
public static extern bool t_init(string a, string b, string c, string d);

However calling this function in the code causes this Access Violation exception:

System.AccessViolationException was unhandled
HResult=-2147467261
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=WindowsFormsApplication1
StackTrace:
   at Test.t_init(String a, String b, String c, String d)
   at WindowsFormsApplication1.Form1..ctor() in c:\Users\rfonseka\Documents\Visual Studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 20
   at WindowsFormsApplication1.Program.Main() in c:\Users\rfonseka\Documents\Visual Studio 2013\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException: 

Any ideas? I know the DLL is loaded ok, because I called a simple function that just returns the version number as an integer and that executed successfully.

rukiman
  • 597
  • 10
  • 32
  • Also I called another function that returns a const char * and simply replaced that with 'string' in the c# prototype and when I execute that the application process dies without an exception. Must be something about the const char * ?? – rukiman Jul 06 '15 at 06:43
  • This is not a duplicate of the question Hans. This questions is referring to a memory exception while the other question is talking about differences between __stdCall and cdecl etc... – rukiman Jul 06 '15 at 07:12

0 Answers0