How to Create DLL in C# and call in Delphi XE6
10,120
A C# DLL is a managed assembly and does not export its functionality via classic PE exports. Your options:
- Use C++/CLI mixed mode to wrap the C#. You can then export functions in unmanaged mode in the usual way.
- Use Robert Giesecke's UnmanagedExports. This is perhaps more convenient than a C++/CLI wrapper.
- Expose the managed functionality as a COM object.
Once you get as far as choosing one of these options you will have to deal with your misuse of the string
data type. That's a private Delphi data type that is not valid for interop. For the simple example in the question PWideChar
would suffice.
Comments
-
ary over 1 year
I created a DLL in VS2013 using File/New Project/Class Library. I then tried to load it dynamically in Delphi. But Delphiis returning
NIL
for procedureGetProcAddress
.My C# & Delphi code looks like what I have posted below. In the code
GetProcAddress
is returningNIL
. Please advise if I am missing something.C# Code
using System; namespace TestDLL { public class Class1 { public static string EchoString(string eString) { return eString; } } }
Delphi Code
Type TEchoString = function (eString:string) : integer;stdcall; function TForm1.EchoString(eString:string):integer; begin dllHandle := LoadLibrary('TestDLL.dll') ; if dllHandle <> 0 then begin @EchoString := GetProcAddress(dllHandle, 'EchoString') ; if Assigned (EchoString) then EchoString(eString) //call the function else result := 0; FreeLibrary(dllHandle) ; end else begin ShowMessage('dll not found ') ; end; end;
-
Belogix almost 9 yearsI just marked to close as duplicate and linked to your earlier answer! In the comments of your answer it even mentions how frequently this question is asked :-) I have still up-voted your answer though!
-
ary almost 9 yearsSo are these the addtional steps I need to take? 1.Install-Package UnmanagedExports in C#. Add [DllExport("EchoString")] above EchoString function in C#. I did the above steps and still its not working. Any thing else I am missing??
-
David Heffernan almost 9 yearsWell, not all of them. These are your options. UnmanagedExports is the simplest.
-
ary almost 9 yearsSo after installing Nuget package, I added this code [DllExport("echostring", CallingConvention = CallingConvention.StdCall)] above echostring function. Still delphi is not finding the procedure. What else am I missing. Thanks.
-
David Heffernan almost 9 yearsThe function names don't match. You have to get the letter case correct. Also, stop using strings for at least a little while. See if you can get success with simple integers. Then move on to more complex data. See my update. Finally, use
external
rather than runtime linking. It makes the code much simpler. -
ary almost 9 yearsAlso there was problem with "Platform Target" in C#. As soon as I changed it to x86, its working fine. Earlier it was set to "Any CPU". Thanks all for help.
-
MBWise almost 7 yearsAnyone tried this: managed-vcl.com/downloads/full ?