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.