Recent

Author Topic: Can my dll be used in another programming language  (Read 3933 times)

vkhoa

  • New Member
  • *
  • Posts: 47
Can my dll be used in another programming language
« on: June 18, 2015, 08:54:28 am »
I wrote a library like this
Code: [Select]
library library_delegate;
uses Classes, SysUtils;
type commandevent=procedure (text:string);
var ocommand:commandevent;
procedure create(cmd:commandevent);stdcall;
begin
  ocommand:=cmd;
end;

procedure writetext(text:string);stdcall;
begin
  if assigned(ocommand) then
     ocommand(text);
end;
exports create,writetext;
begin
end.
then compiled it to "library_delegate.dll"
after that I wrote a program
Code: [Select]
program use_library_delegate;
uses Classes, SysUtils;
type commandevent=procedure (text:string);
procedure create(cmd:commandevent);stdcall;external 'library_delegate';
procedure writetext(text:string);stdcall;external 'library_delegate';
procedure writeatext(text:string);
begin
  writeln(text);
end;
var i:integer;
begin
  create(@writeatext);
  for i:=1 to 100 do
      writetext('Text line '+inttostr(i));
  readln;
end.
the program run as I expected, it wrote 100 text line to the console
I want to know if this dll could be used in visual basic or C(or other windows programming language). And I am curious to know how simple it is to use the dll's function in those language (especially at passing the procedure type parameter in the "create" procedure), can you give me any examples.
another question, can we assign or read the "ocommand" directly from the program?
« Last Edit: June 18, 2015, 09:04:06 am by vkhoa »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Can my dll be used in another programming language
« Reply #1 on: June 18, 2015, 09:02:20 am »
I want to know if this dll could be used in visual basic or C(or other windows programming language).

No it can't. You have to stop using the string data type if you want to be compatible. In short you can only use C compatible data types ee Char, pchar etc.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vkhoa

  • New Member
  • *
  • Posts: 47
Re: Can my dll be used in another programming language
« Reply #2 on: June 18, 2015, 09:20:46 am »
 :)Thanks for the reply!
Is visual basic compatible data types is the same as  C compatible data types ?
And can we declare and pass the procedure type parameter in those language
and assume that I use the right data type, can it call successfully the procedure which was passed to "create" procedure when "writetext"  is called
Can anyone tell me detail about the C an VB compatible data types, for example for byte, longint, ect...
« Last Edit: June 18, 2015, 09:37:06 am by vkhoa »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Can my dll be used in another programming language
« Reply #3 on: June 18, 2015, 09:37:31 am »
As far as I know yes Visual basic has the ability to pass a procedure/function address to your code. Make sure that you include the calling convention on your requirements and you are set.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018