delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


DLL in Dephi to be called from Visual BasicComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Object Pascal
Skill Level:
Scoring:
Last Update:
05/26/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Dll VB Visual Basic string
Times Scored:
2
Visits:
3407
Uploader: Khaled Shagrouni
Company:
Reference: http://www.shagrouni.com
Component Download: http://www.shagrouni.com/download/textdb.zip
 
Question/Problem/Abstract:
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) < List.Count 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) < (List.Count)) 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) < (List.Count)) 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 ...
            


            

      





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
S. Kucherov
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)