problem with loading dll programmed by Delphi7

J

Jak

I made a dll file,Delphi.dll, with Delphi7, the sourcecode is:

library Delphi;
uses
SysUtils,
Classes;
{$R *.res}
function GetStr(s:ShortString ):ShortString ;stdcall;
begin
result:= s;
end;
exports
GetStr;
begin
end.



In the VB.NET2005 I declared the function as 'Declare Auto Function GetStr
Lib "Delphi.dll" (ByVal s As String) As String', but I would run into the
exception of not being able to load the function because could not find the
entry point of the function (translated from the error message in Chinese).
But I tested the dll within Delphi application, it worked well there.

Are there any special rules to program dll for VS.NET with Delphi or any
other languages?

Best regards,

Jack Zhong
 
J

Jani Järvinen [MVP]

Hi Jak,
In the VB.NET2005 I declared the function as 'Declare Auto Function
GetStr Lib "Delphi.dll" (ByVal s As String) As String', but I would run
into the exception of not being able to load the function because could
not find the entry point of the function.

Although I use C# myself, VB.NET is perfectly able to call Delphi DLLs. I've
written many Delphi DLLs and have been able to call all of them from .NET
code.

Carefully read the error message: does it say that the entry point was not
found, or that the DLL itself was not found? Try manually copying the Delphi
DLL to the same directory as your .EXE file from VB.NET.

Also, in your Delphi code you have:
function GetStr(s:ShortString ):ShortString ;stdcall;

Unless you have to, don't use a Delphi/Pascal ShortString type when you want
to pass the string to .NET code; .NET doesn't understand a Delphi string
that starts with a length byte and doesn't end in a null character (#0).
Instead, use a PChar.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
J

Jak

Carefully read the error message: does it say that the entry point was not
found, or that the DLL itself was not found? Try manually copying the
Delphi DLL to the same directory as your .EXE file from VB.NET.

It says it could not find the entry point.

Unless you have to, don't use a Delphi/Pascal ShortString type when you
want to pass the string to .NET code; .NET doesn't understand a Delphi
string that starts with a length byte and doesn't end in a null character
(#0). Instead, use a PChar.

I used the PChar type in the first, but encountered the same error.

Rgds,

Jack
 
J

Jani Järvinen [MVP]

Hi Jak,

If you get the error message saying:

Unable to find an entry point named 'GetStr' in DLL 'DelphiDLLTest.dll'.

....when you execute your .NET application, then there must be an error in
your Delphi or VB code, for example a typo in the external (VB) and/or
exported function name (Delphi).

To help you solve your issue, I just tested interoperability between Delphi
2006 and C#, and it works well. Here's what I did.

First, I created a Delphi DLL project with the following minimal code:

-----------------------
library DelphiDLLTest;

function GetStr(s : PChar) : PChar; stdcall;
begin
Result:= s;
end;

exports
GetStr;

begin
end.
-----------------------

Next, I started a new C# WinForms project (VB.NET should be just as fine),
and then chose the menu command "Project/Add Existing Item" and added the
compiled Delphi DLL to the project. (This is actually not necessary, but
makes it simpler to develop at the same time in both Delphi and Visual
Studio.)

Once the DLL was added to the project, I clicked the DLL in Solution
Explorer and set the property "Copy To Output Directory" to "Copy Always".
Now when I build the solution in Visual Studio, the Delphi DLL is
automatically copied to the bin\Debug directory.

Thirdly, I wrote the following C# code:

-----------------------
[DllImport("DelphiDLLTest.dll")]
public static extern string GetStr(string s);

private void button1_Click(object sender, EventArgs e)
{
string result = GetStr("Hello, World!");
MessageBox.Show("DelphiDLLTest.dll returns:\r\n" + result);
}
-----------------------

When I run the C# application and click the button to execute the above
code, it works nicely. So if you get errors at this point, try creating a
new Delphi DLL from scratch, and then following the instructions above (but
changing to VB.NET code if you want to).

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
J

Jak

Hi, Jani,

Thank you very much for warmheart and for your details instruction. I have
rewrite my Delphi DLL and it works fine with the .NET application now.

Thank you again.

Rgds,

Jack Zhong
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top