Solved

How to use DLL built by Delphi in VB?

Posted on 1999-01-26
6
193 Views
Last Modified: 2010-04-06
I need to know how I can implement a DLL that has been compiled by Delphi 3.0 in a 32 bit VB application.

Can anyone tell me what datatypes are compatible (Delphi versus VB) or how I can modify a parameter's contents when passing it to a DLL function?

For example, when passing a string to a Delphi DLL function from VB: how do I do that?
Does Delphi support the Variant datatype or an equivalent, like VB does?
0
Comment
Question by:kulikuli
  • 3
  • 2
6 Comments
 

Expert Comment

by:lorenkoss
ID: 1363401
I have done these many times.  Basically, I only pass PChars, integers, and pointers.  Be sure also to specify either CDECL or STDCALL in your delphi app so that it puts your variables in the right order.  In your VB app, you will prototype the function with the appropriate CDECL or STDCALL so that the variable go in the right order (pascal type is right to left (i think) and cdecl is left to right) based on the call stack.

I wouldn't play with the Variant type.  I never properly got it work.  As long as you know what type to expect, use pointers to objects.
0
 
LVL 5

Author Comment

by:kulikuli
ID: 1363402
lorenkoss, I have created a function in Delphi that is compiled in a DLL, which expects a PChar like you said.I have specified STDCALL in the DLL-file.
Unfotunately, it keeps crashing......

The dll's source code in Delphi:
----------------------------
library docver;

uses
  SysUtils,
  Classes;

function valid(fname : pchar) : integer; stdcall;
var fs : TFilestream;
    l  : longint;
begin
  fs := TFileStream.create(fname,fmOpenRead);
  fs.Seek(512,soFromBeginning);
  fs.Read(l,3);
  if l = 12690924 then
    valid := 1
  else
    valid := 0;
  fs.destroy;
end;

exports
 valid index 1;

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


My module in VB:
--------------Declarations section-----------------
Private Declare Function valid2 Lib "docver.dll" (ByVal strName as String) as Integer
-------------------------------------------------------------
Function Test()

Dim intReturn as Integer
intReturn = Valid2( "Hello" & vbNull ) 'call dll-function with null-terminated string

End Function

0
 
LVL 5

Author Comment

by:kulikuli
ID: 1363403
the VB declaration of course shouldn't be Valid2, but Valid. Sorry.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:kulikuli
ID: 1363404
The question is reopened, because I need a solution fast!
0
 

Expert Comment

by:lorenkoss
ID: 1363405
Make sure the valid function is being called by running the DLL from delphi and setting the run parameters in Delphi to point to the "host application" (the VB app).  I have a feeling the error may be happening in your filestream implementation.  Put a break point on the create line and step through..  It should get at least to that line.
0
 
LVL 15

Accepted Solution

by:
simonet earned 30 total points
ID: 1363406
In Delphi 3, use the ShortString type rather than pChar.

You should only use pchar if you're going to change it's value. Otherwise, if it's going to behave as a "const", make it a ShortString.

That said, your Delphi declaration should look like:

function valid(fname : ShortString) : integer; stdcall;
var
  fs : TFilestream;
  l  : longint;
begin
  fs := TFileStream.create(AnsiString(fname), fmOpenRead);
  ...
  ...
  ...

end;

See if that works. If not, drop me a line.

yours,

Alex

   



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now