Delphi Can
Delphi dll metodundan C# a string dönmek - Baskı Önizleme

+- Delphi Can (https://www.delphican.com)
+-- Forum: Delphi (https://www.delphican.com/forumdisplay.php?fid=3)
+--- Forum: Genel Programlama (https://www.delphican.com/forumdisplay.php?fid=6)
+--- Konu Başlığı: Delphi dll metodundan C# a string dönmek (/showthread.php?tid=5687)



Delphi dll metodundan C# a string dönmek - Gürcan - 27-01-2021

Selam arkadaşlar.
Delphi' de oluşturulmuş bir dll' i C# içinde kullanıp, ondan string değer dönmek istediğimde C# da yazdığım program çakılıyor. Örneğin boolean tipinde parametre dönmekte sorun yaşamıyorum. String ve WideString olarak denedim, sonuç değişmedi.

String dönmenin pratik bir yolu var mı ? Deklerasyon ve kullanım şeklim aşağıdaki gibi ...

Delphi kodu :

    function fn_Test(out _bDosyaYok : Boolean; out _sSonucMesaji : WideString) : Boolean; cdecl;
   Begin
       Result := True;
       _bDosyaYok := True;
       _sSonucMesaji := 'ABCĞÜŞİÖÇ';
   End;


C# Deklerasyon :
       [DllImport("Test.dll", EntryPoint = "fn_Test", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
       private static extern Boolean fn_Test(out Boolean _bDosyaYok, [MarshalAs(UnmanagedType.LPStr)] out String _sSonucMesaji);

C# Kullanım :

Boolean bDosyaYok = false;
String sSonucMesaji = "";
fn_Test(out bDosyaYok, out sSonucMesaji);



Delphi dll metodundan C# a string dönmek - ismailkocacan - 27-01-2021

    [DllImport("Project1.dll", EntryPoint = "fn_Test", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
   private static extern Boolean fn_Test(out Boolean _bDosyaYok, [MarshalAs(UnmanagedType.LPWStr)] out string _sSonucMesaji);



function fn_Test(out _bDosyaYok : Boolean; out _sSonucMesaji : WideString) : Boolean; cdecl;
begin
  Result := True;
  _bDosyaYok := True;
  _sSonucMesaji := 'ABCĞÜŞİÖÇ';
end;


exports fn_Test;


https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.unmanagedtype?view=net-5.0

LPWStr 21 A 2-byte, null-terminated Unicode character string. You cannot use the LPWStr value with an unmanaged string unless the string was created by using the unmanaged CoTaskMemAlloc function.


Çakılıyordan kastınızı anlamadım.


Delphi dll metodundan C# a string dönmek - Gürcan - 27-01-2021

Sonunda bir yöntem buldum. Belki birilerinin işine yarar, C# tarafındaki deklerasyonu şu şekilde yapınca çalışıyor :

[DllImport("Test.dll", EntryPoint = "fn_Test", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern Boolean fn_Test(out Boolean _bDosyaYok, [MarshalAs(UnmanagedType.BStr)] out String _sSonucMesaji);

Yani kısaca, UnmanagedType.BStr kullanmak gerekiyor stringler için. Delphi tarafında WideString verilmesi de gerekiyor. String kabul etmiyor.
Bulduğum kaynak : https://scrapax.com/pass-data-csharp-to-delphi-dll/


(27-01-2021, Saat: 17:35)ismailkocacan Adlı Kullanıcıdan Alıntı:
    [DllImport("Project1.dll", EntryPoint = "fn_Test", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
   private static extern Boolean fn_Test(out Boolean _bDosyaYok, [MarshalAs(UnmanagedType.LPWStr)] out string _sSonucMesaji);



function fn_Test(out _bDosyaYok : Boolean; out _sSonucMesaji : WideString) : Boolean; cdecl;
begin
  Result := True;
  _bDosyaYok := True;
  _sSonucMesaji := 'ABCĞÜŞİÖÇ';
end;


exports fn_Test;


https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.unmanagedtype?view=net-5.0

LPWStr 21 A 2-byte, null-terminated Unicode character string. You cannot use the LPWStr value with an unmanaged string unless the string was created by using the unmanaged CoTaskMemAlloc function.


Çakılıyordan kastınızı anlamadım.


C# da uygulama hiçbir hata vermeden duruyor. Çakılıyordan kastım buydu.


Delphi dll metodundan C# a string dönmek - ismailkocacan - 27-01-2021

Paylaştığım kod çalışıyor.


Delphi dll metodundan C# a string dönmek - Gürcan - 27-01-2021

Teşekkürler, onu da inceleyeceğim.

Verdiğiniz kod [MarshalAs(UnmanagedType.BStr)] olarak çalışıyor. LPWStr de çalışmadı.


Delphi dll metodundan C# a string dönmek - ismailkocacan - 27-01-2021

JMTD06X.png


Delphi dll metodundan C# a string dönmek - Gürcan - 28-01-2021

CharSet benim Delphi metodlarımda Ansi. Bununla ilgili olabilir (mi?). BStr haricinde gerçekten çalışmadı.