473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem with loading dll programmed by Delphi7

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
Jan 18 '07 #1
4 3441
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
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Jan 18 '07 #2
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
Jan 19 '07 #3
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
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Jan 19 '07 #4
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
Jan 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Piterek | last post by:
Hello. I have a question, did someone can convert from VB.NET source code to Delphi7 code? Dim n, x As Double Dim tab() As Double = {-3, 0.5, -0.05}...
0
by: Stefan Mähr | last post by:
Hello Community, I got a problem using a Delphi COM DLL (written in Delphi7) within my VB.NET 2005 Project, I can create an Instance of the variable without Problems, but when I call a Function...
3
by: Aaron | last post by:
I'm working on a program the goes out to a website and pulls down some data using cURL. The page I'm pulling from has a loading screen before the data I want is displayed. The code I have so far...
7
xNephilimx
by: xNephilimx | last post by:
lHi guys! I'm having a little problem that's getting on my nerves, I couldn't find a solution, I also tryed googling it and I found nothing... (my field of expertise is in AS 2 and 3, but I still...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.