Title: DLL in Dephi to be called from Visual Basic
Question: How to create a DLL in Dephi to be called from Visual Basic
Answer:
This is an example of how to create a DLL in Dephi to be called from other development enviroment like Visual Basic.
The DLL uses TSringList, a powerful component in VCL Library, and wraps some of its properties and methods to manage list of strings.
The DLL code is listed below, also you can download the source and the compiled one with examples in Visual Basic and Delphi.
library TextDb;
{Example of a DLL}
{Always UnloadFile to free the memory}
uses
SysUtils, Classes;
var
List: TStringList; {Global variable}
function LoadFile(lpString: PChar; Sorted: Cardinal): integer; stdcall;
begin
try
if List nil then List.Free; {Destroy any Previous instance}
List := TstringList.create; {Creating an instance to List}
if Sorted 0 then
begin
List.Sorted := true;
List.Duplicates := dupAccept; {Allow duplicate values}
end;
if FileExists(lpString) then
List.LoadFromFile(lpString); {Load the file lines into our List}
result := List.Count; {Number of lines in the List}
except {If any thing goes wrong..}
List.Free; {Destroy the creature}
List := nil;
result := 0;
end;
end;
function UnloadFile: integer; stdcall;
begin
try
if List nil then List.Free;
List := nil;
result := 1;
except
result := 0;
end;
end;
function GetLine(lpString: PChar; nMaxCount, LineNum: Cardinal): integer; stdcall;
var
s: string;
begin
result := -1;
if List nil then
if Integer(LineNum) then
begin
s := List[LineNum];
StrLCopy(lpString, PChar(s), nMaxCount); {Convert Pascal string to PChar}
result := Length(S); {Len(s)}
end;
end;
function AddLine(lpString: PChar): integer; stdcall;
begin
result := -1;
if List nil then
begin
result := List.Add (lpString); {return the position of the line}
end;
end;
function SaveFile(lpString: PChar): integer; stdcall;
begin
try
if (List nil) and (Trim(lpString) '') then
List.SaveToFile(lpString); {Save lines into file}
result := List.Count; {Number of lines in the List}
except
result := 0;
end;
end;
function DeleteLine(LineNum: Cardinal): integer; stdcall;
begin
result := -1;
if (List nil) and (Integer(LineNum) then
begin
List.Delete(LineNum);
result := List.Count;
end;
end;
function UpdateLine(lpString: PChar; LineNum: Cardinal): integer; stdcall;
begin
result := -1;
if (List nil) and (Integer(LineNum) then
begin
List[LineNum] := lpString;
result := LineNum;
end;
end;
function LineCount: integer; stdcall;
begin
result := -1;
if List nil then
result := List.Count;
end;
function GetText(lpString: PChar; nMaxCount: Cardinal): integer; stdcall;
var
s: string;
begin
result := -1;
if List nil then
begin
s := List.Text;
StrLCopy(lpString, PChar(s), nMaxCount); {Convert Pascal string to PChar}
result := Length(S); {Len(s)}
end;
end;
exports
LoadFile, UnloadFile, GetLine, AddLine, DeleteLine, UpdateLine, SaveFile,
LineCount, GetText;
begin
end.Generated by PasToWeb, a tool by Marco Cant. Copyright 1997 ...