Einzelnen Beitrag anzeigen

sonny2007

Registriert seit: 27. Aug 2009
39 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#1

C# DLL in Delphi nutzen

  Alt 27. Mär 2022, 20:15
Hallo Zusammen

(Delphi 10.4)

Ich möchte gerne eine function aus einer C# DLL in delphi aufrufen und verwenden.
Die C# Dll sieht so aus

Code:
using MessagePack;
using RGiesecke.DllExport;
using System;
using System.Runtime.InteropServices;

namespace MsgPack.Native
{
   public class Exports
   {
      [DllExport(CallingConvention = CallingConvention.StdCall)]
      [return: MarshalAs(UnmanagedType.BStr)]
      public static string ConvertToJson(IntPtr pBytes, int cBytes)
      {
         byte[] data = new byte[cBytes];
         Marshal.Copy(pBytes, data, 0, cBytes);
         string result = MessagePackSerializer.ConvertToJson(data);
         return result;
      }
   }
}

Die definition in Delphi sieht aktuell so aus.
Delphi-Quellcode:
type
  TConvertToJson = function(pBytes: Pbyte; cBytes: Integer): string; stdcall;
Der Aufruf
Delphi-Quellcode:
dllhandle := loadlibrary('MsgPack.Native.dll') ;
  if dllhandle <> 0 then
    @ConverToJson := GetProcAddress(dllhandle, 'ConvertToJson');
    if @ConverToJson <> nil then // <<--------------- Ist immer NIL
    begin
und so würde der Aufruf selber aussehen.
tmp := ConverToJson(@buf[0], length(Buf)); Jedoch gib GetProcAdress gibt immer NIL aus
Was mache ich falsch?

Grüße
s0n
  Mit Zitat antworten Zitat