delphi - Native DLL - C# -


i have native dll library wrote in delphi. want refer lib c# , returned string.

function

function getcode (auser,apassword: pansichar): pansichar; stdcall; 

c# code

[dllimport("c:\\library.dll", callingconvention = callingconvention.stdcall,         charset = charset.ansi)] private static extern string getcode([marshalas(unmanagedtype.lpstr)]string auser, [marshalas(unmanagedtype.lpstr)]string apassword);  public string getcode(string login, string password) {         return getcode(login, password); } 

trying call function application exits code -1073740940 (0xc0000374).

do have expirence it? thank help

with return value of string, marshaller assumes responsibility freeing returned string. call cotaskmemfree. no doubt string not allocated on com heap. hence error, ntstatus error code of status_heap_corruption.

your p/invoke should return intptr instead avoid this:

[dllimport("c:\\library.dll", callingconvention = callingconvention.stdcall,     charset = charset.ansi)] private static extern intptr getcode(string auser, string apassword); 

to string, pass return value marshal.ptrtostringansi.

public string getcode(string login, string password)  {     intptr retval = getcode(login, password);     string result = marshal.ptrtostringansi(retval);     // how deallocate retval     return result; } 

note remove superfluous marshalas attributes. charset.ansi takes care of input argument marshalling.

you still need work out how deallocate returned memory. cannot tell how allocated. you'll need contact developer of delphi code, or read source. not surprised if native delphi heap used, leaves in bind. using com heap calling cotaskmemalloc not bad option, , original p/invoke return value of string fine.

other options include exporting deallocator or asking caller allocate buffer. there great many examples on web of how implement various options.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -