0

I tried to call a dll that is coded in delphi in C#. (delphi 11 64bit) I believe there will be some problems. Therefore, I tried some example in these website: Calling a Delphi method in a dll from c# Calling a Delphi DLL from a C# .NET application

But there is no response, and the applicaiton closes.

These are my codes:

example 1

delphi

function DBConnect1(inputStr,connStr:PWideChar):PWideChar;stdcall;
begin
  try
     result:=PWideChar('Hello from Delphi!');
  except
     result:=PWideChar('Exception');
  end;
end;

C#

[DllImport("Project1.dll",
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string DBConnect1(string inputString, string connectionString);

string inputString = "Parker";
string connectionString = "MyComputer";
string dbStrObj1 = DBConnect1(inputString, connectionString);
MessageBox.Show(dbStrObj1);

example 2

delphi

function Test1(sFD,sVD,sINI,sCh,sSD: string): PWideChar;stdcall;
var
  tempStr:string;
  str:WideString;
begin
  tempStr:=sFD+sVD+sINI+sCh+sSD;
  try
   result:= PWideChar(tempStr);
  except
    str:='Error';
    result:=PWideChar(str);
  end;
    result:=PWideChar(str);
end;

C#

[DllImport("Project1.dll", EntryPoint = "LoginLic", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string LoginLic(string s1, string s2, string s3, string s4, string s5);

string strId = "PCMS";
string strVersion = "AD19";
string strIni = @"";
string strCheck = "0";
string strSubDate = null;
string aa;
aa = UseDll.LoginLic(strId, strVersion, strIni, strCheck, strSubDate);
8
  • 1
    second one has mismatched function names. For the first one, no error message from C# runtime at all? Make sure you are matching 32/64 bit
    – pm100
    Commented Jan 13, 2023 at 2:20
  • note that sample explicitly says not to use string as a return type
    – pm100
    Commented Jan 13, 2023 at 2:22
  • Hi, pm100. Thanks a lot for your response. First of all, I changed the platform targer to 64 bit in Build page. And solution platform is Any CPU. There is no response and no error message. Even, I used try-catch that still no response and the application closes. Second, I changed solution platform to x64 and Platform target is x64. The error is DllNotFoundException. And sorry about that, I didn't change the name in this page. The name is LoginLic. Commented Jan 13, 2023 at 2:51
  • did you read my second comment tho
    – pm100
    Commented Jan 13, 2023 at 4:33
  • 1
    another suggestion, write a very simple function, say one that returns 42, get tha going and gradually add more argument and return types
    – pm100
    Commented Jan 13, 2023 at 4:38

1 Answer 1

0

You have to marshall function parameters also. Refer this link for passing string with Marshalling https://learn.microsoft.com/en-us/dotnet/framework/interop/default-marshalling-for-strings

[DllImport("Project1.dll",CallingConvention = CallingConvention.StdCall,CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string DBConnect1(MarshalAs(UnmanagedType.LPWStr)]string inputString, MarshalAs(UnmanagedType.LPWStr)] string connectionString);
20
  • Hi Hari, thanks a lot for your reply. I tried your suggestion and edit my code. But there is still no response. Commented Jan 13, 2023 at 2:58
  • You can try with BSTR. Both in delphi and C# input strings. I used to interop between C++ and C# with BSTR strings
    – Hari E
    Commented Jan 13, 2023 at 3:09
  • Try to debug your code with native debugger. To enable native debugging Properties -> Debug -> Enable the check box "Enable Native Debugging' option
    – Hari E
    Commented Jan 13, 2023 at 3:12
  • Hari, I tried your suggestion that is 'enable native debuggin'. I got a exception thrown which is my.exe has triggered a breakpoint. And got a exception unhandled that detail is Unhandled exception at 0x00007FFC961AF609 (ntdll.dll) in my.exe: 0xC0000374: parameters: 0x00007FFC962197F0 Commented Jan 13, 2023 at 3:49
  • BTW, I call the dll and use that function to send the string to the server. That was a success. The server received the string I sent. But it still has the exception unhandled problem in C#. Commented Jan 13, 2023 at 4:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.