Solved

How to use DLL built by Delphi in VB?

Posted on 1999-01-26
6
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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