Delphi 4 (but I want to write them in my favorite text editor) and pascal compiler from freepascal.org.
Thanks,
TNC
Main Topics
Browse All TopicsI'd like to write a dll with pascal and reference it with VBA. How can I compile the dll such that Windows can use it?
Thanks,
TNC
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here's a simple .dll source (from borland pascal help):
library MinMax;
function min (x, y : integer) : integer; export;
begin
if x < y then min := x else min := y;
end;
function max (x, y : integer) : integer; export;
begin
if x > y then max := x else max := y;
end;
exports
min index 1,
max index 2;
begin
end.
Pascal (at least - Delphi) makes a valid library file.
Here's an example of a library file in pascal and a source from VBA (haven't coded in VB for a long time so there can be some imperfections, but it works as it should).
Notice that there's 'smallint' in library file, but 'integer' in vba source. That's because integer in vba is 16bit value, but in delphi it is 32bit value, so I used smallint (this is 16bit value in delphi).
Hope this will help.
----- ----- ----- ----- ----- ----- Project2.dpr:
library minmax;
function min (n1, n2 : smallint) : smallint; stdcall; /// didn't work without 'stdcall'
begin
if n1 < n2 then result := n1 else result := n2;
end;
function max (n1, n2 : smallint) : smallint; stdcall;
begin
if n1 > n2 then result := n1 else result := n2;
end;
exports
min name 'func1',
max name 'func2';
begin
end.
----- ----- ----- ----- ----- ----- VBA source (tested in MS Word 2000 VB Editor):
Private Declare Function min_f Lib "C:\Program Files\Borland\Delphi6\Bin\
Private Declare Function max_f Lib "C:\Program Files\Borland\Delphi6\Bin\
Private Sub CommandButton1_Click()
Dim n1 As Integer
Dim n2 As Integer
Dim n As Integer
'On Error Resume Next
n1 = CInt(TextBox1.Text)
n2 = CInt(TextBox2.Text)
n = min_f(n1, n2)
MsgBox n
End Sub
Private Sub CommandButton2_Click()
Dim n1 As Integer
Dim n2 As Integer
Dim n As Integer
On Error Resume Next
n1 = CInt(TextBox1.Text)
n2 = CInt(TextBox2.Text)
n = max_f(n1, n2)
MsgBox n
End Sub
Here's an example of passing string as a parameter in function/procedure.
------ ------ ------ VBA source:
Private Declare Function example Lib "C:\Program Files\Borland\Delphi6\Bin\
Private Sub CommandButton1_Click()
MsgBox example("one")
MsgBox example("two")
End Sub
------ ------ ------ Delphi source:
library sample;
uses
windows;
function example(var s : pchar) : integer; stdcall;
begin
if s = 'one' then result := 1 else
if s = 'two' then result := 2 else
result := 0;
end;
exports
example;
begin
end.
Tryin' to find/make any example of returning strings...
Oh! Actually it is so simple to return strings! Here's an example:
---- ---- ---- VBA source:
Private Declare Function example Lib "C:\Program Files\Borland\Delphi6\Bin\
Private Sub CommandButton1_Click()
MsgBox example
End Sub
---- ---- ---- Delphi source:
library sample;
function example: pchar; stdcall;
begin
GetMem(result, 17);
result := 'some simple text';
end;
exports
example;
begin
end.
Don't forget to use GetMem to get memory for string (will not work without GetMem()).
ZhaawZ,
Thanks for your help so far. That seems to work until I try to convert an integer to a string and return it. Here's my test code:
library sixOneThree;
uses
Windows, SysUtils;
function tryInteger(var test:integer):pchar; export; stdcall;
var
testString:string;
testLen:integer;
testStringP:pchar;
begin
str(test, testString);
testStringP := pchar(testString);
testLen := strLen(testStringP);
getMem(result, testLen);
result := testStringP;
end;
exports
tryInteger index 1;
end.
What gives?
Thanks,
TNC
Okay,
Now, the only functinal problem that I have is that the strings that are returned are too long. For some reason, it adds about 8 chars to the end. Sometimes, there is junk in those chars. The same funciton doesn't do that if I run it in console. It must be on the VB side. Any ideas on how to fix that?
Thanks again,
TNC
//from your source
var
testString:string;
testLen:integer;
testStringP:pchar;
begin
str(test, testString);
testStringP := pchar(testString);
testLen := strLen(testStringP);
getMem(result, testLen);
result := testStringP;
end;
There's no need to use getMem when you assign to a value some pchar(string) or a any variable of type PChar (as it is in this case).
Brrr... Actualy I'm totally confused from all of this... Now it works also without GetMem...
A very short example of converting an integer to a string:
----
Private Declare Function example _
Lib "C:\Program Files\Borland\Delphi6\Bin\
Alias "tryInteger" _
(ByVal n As Long) _
As String
Private Sub CommandButton1_Click()
MsgBox example(987)
End Sub
-----
library sixOneThree;
uses
sysutils;
function tryInteger (n : integer) : pchar; stdcall; export;
begin
result := pchar(format('%d', [n]));
end;
exports
tryInteger;
end.
Business Accounts
Answer for Membership
by: mlmccPosted on 2005-01-26 at 06:42:08ID: 13142293
What Pascal compiler?
mlmcc