Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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
     result:=PWideChar('Hello from Delphi!');
  except
     result:=PWideChar('Exception');
[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;
  tempStr:string;
  str:WideString;
begin
  tempStr:=sFD+sVD+sINI+sCh+sSD;
   result:= PWideChar(tempStr);
  except
    str:='Error';
    result:=PWideChar(str);
    result:=PWideChar(str);
[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);
                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
                Jan 13 at 2:20
                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.
–  Parker Shiung
                Jan 13 at 2:51
                another suggestion, write a very simple function, say one that returns 42, get tha going and gradually add more argument and return types
– pm100
                Jan 13 at 4:38

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);
                Hi Hari, thanks a lot for your reply. I tried your suggestion and edit my code. But there is still no response.
–  Parker Shiung
                Jan 13 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
                Jan 13 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
                Jan 13 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
–  Parker Shiung
                Jan 13 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#.
–  Parker Shiung
                Jan 13 at 4:05
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.