Solved

How to use DLL built by Delphi in VB?

Posted on 1999-01-26
6
192 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
Comment Utility
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
Comment Utility
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
Comment Utility
the VB declaration of course shouldn't be Valid2, but Valid. Sorry.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

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

Expert Comment

by:lorenkoss
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

16 Experts available now in Live!

Get 1:1 Help Now