I need to pass string from Delphi to C# DLL and return integer in return. Its been awhile since I worked with DLL and google is not helping much. Any idea how to do this. An example will help. I did managed to created DLL in C# and pass and return integer from Delphi to C# DLL. Thanks.
Export the C# dll as COM, define your string as WideString in Delphi, and BSTR in C#.
ReplyDeleteOtherwise, you'll have to pass a PChar on the Delphi side, and something like a [MarshalAs(WideCharPtr)]StringBuilder or something to that effect.
ReplyDeleteThanks Bourchez, Martijn. I tried PChar from Delphi and below code in C# and still its not working . I keep getting external exception E0434352 error.
ReplyDeletepublic static int Test ([MarshalAs(UnmanagedType.BStr)] string teststr) {return 1;}
Arun Singh There's a reason why Martijn Coppoolse specified "WideCharPtr" and not "BStr" like A. Bouchez did.
ReplyDeleteA BStr is a special COM type with specific memory management: http://msdn.microsoft.com/en-us/library/windows/desktop/ms221069%28v=vs.85%29.aspx
You are using UnmanagedExports right?
ReplyDeleteMy C# code looks like this
ReplyDeleteusing System;
namespace TestDLL
{
public class Class1
{
[DllExport]
public static string Test(string sString)
{
return sString;
}
}
}
That's not going to work. Can't we do this on Stack Overflow? It's much easier there and it lives on for the benefits of others.
ReplyDeleteDavid Heffernan Yes I did put there about my initial problem of working with DLL created by C#. Now I am stuck with managing string in DLL. So came here to my home town where its like family & people are less rude :)
ReplyDeleteA. Bouchez Do I need to use Regasm.exe for exporting C# DLL as COM. This is really getting dirty :)
ReplyDeleteIf you don't want to write COM by yourself. Just refer to below link:
ReplyDeletehttps://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports
it may be easier to you.
Thanks Sam Shaw . That's what originally David Heffernan mentioned and I thought that I did just that. I did Install-Package UnmanagedExports and changed code as in my above comment. still getting errors.
ReplyDeletewell, If you decided to use that to export unmanaged function, you should really take deeply looking at the link i posted, which include some sample codes and remark.
ReplyDeleteThere are some key words you should know about.
[DllExport("XXXXXXX", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.BStr)]
I gotta go to bed. Good luck. Or you just post to stackoverflow, I think David will post full source code answer there. ;)
before I shutdown my computer, just googling an example which is similar to your case.
ReplyDeletehttp://stackoverflow.com/questions/20776847/returning-a-string-from-a-c-sharp-dll-with-unmanaged-exports-to-inno-setup-scrip
FYR
NOTE: calling convention is important and depends on delphi corresponding function decaration.
Good night.
Thanks Sam Shaw . I will look into that.
ReplyDeleteNote that InnoSetup is written... in Delphi... :)
ReplyDelete