c++ - Using DLL in C# -


i´m trying use old c dll in c# windows forms app. seems i´m using wrong datatype output parameter.

dll source code:

#ifdef __cplusplus #define export extern "c" __declspec (dllexport) #else #define export __declspec (dllexport) #endif  export int callback complemento (lpstr) ; export int callback matchcode (lpstr, lpstr, lpstr, lpstr); 

c# dll export

[dllimport(     @"<mydir>fonetica.dll",     charset = charset.ansi)] public static extern int matchcode(     string n,     string s,     string e,     [marshalas(unmanagedtype.lpstr)] out string retorno); 

c# dll usage code

string match = string.empty;  matchcode(pefi.nome, string.empty, string.empty, out match); 

the tree first parameters input , last 1 output.

i´m receiving exception:

system.accessviolationexception unhandled   message=attempted read or write protected memory. indication other memory corrupt.   source=mscorlib   stacktrace:        @ system.string..ctor(sbyte* value)        @ system.stubhelpers.cstrmarshaler.converttomanaged(intptr cstr)        @ wfmatchcode.form1.matchcode(string n, string s, string e, string& retorno)        @ wfmatchcode.form1.button1_click(object sender, eventargs e) in c:\users\computecnica.alexand\documents\visual studio 2010\projects\wfmatchcode\wfmatchcode\form1.cs:line 91        @ system.windows.forms.control.onclick(eventargs e)        @ system.windows.forms.button.onclick(eventargs e)        @ system.windows.forms.button.onmouseup(mouseeventargs mevent)        @ system.windows.forms.control.wmmouseup(message& m, mousebuttons button, int32 clicks)        @ system.windows.forms.control.wndproc(message& m)        @ system.windows.forms.buttonbase.wndproc(message& m)        @ system.windows.forms.button.wndproc(message& m)        @ system.windows.forms.control.controlnativewindow.onmessage(message& m)        @ system.windows.forms.control.controlnativewindow.wndproc(message& m)        @ system.windows.forms.nativewindow.debuggablecallback(intptr hwnd, int32 msg, intptr wparam, intptr lparam)        @ system.windows.forms.unsafenativemethods.dispatchmessagew(msg& msg)        @ system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(intptr dwcomponentid, int32 reason, int32 pvloopdata)        @ system.windows.forms.application.threadcontext.runmessageloopinner(int32 reason, applicationcontext context)        @ system.windows.forms.application.threadcontext.runmessageloop(int32 reason, applicationcontext context)        @ system.windows.forms.application.run(form mainform)        @ wfmatchcode.program.main() in c:\users\computecnica.alexand\documents\visual studio 2010\projects\wfmatchcode\wfmatchcode\program.cs:line 18        @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args)        @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args)        @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly()        @ system.threading.threadhelper.threadstart_context(object state)        @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean ignoresyncctx)        @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state)        @ system.threading.threadhelper.threadstart()   innerexception:  

[dllimport( @"fonetica.dll", charset = charset.ansi)] public static extern int matchcode(string n, string s, string e, stringbuilder retorno); 

you must pass stringbuilder has enough capacity take whatever gets written it. same way must pass array that's large enough in c:

var capacity = 1000; // change whatever need var buffer = new stringbuilder(capacity);  var result = matchcode("test", "test", "test", buffer);  var output = buffer.tostring(); 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -