Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-06

Writing a DLL to be use in a VBA code

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

luciano de souza

Hello all,

At work, I need to create a Excel sheet containing certain more complex functions. I tried to use only VBA, but I released I will more productive if I could use a DLL writen in Pascal for the heavier code.

Let see this DLL:

library test;
{$mode objfpc}{$H+}
function concatenate(name: string): string; cdecl;
begin
result := 'Freepascal ' + name;
end;
function first(names: array of string):string; cdecl; // Warning:
cdecl'ared functions have no high parameter
begin
result := names[0];
end;
exports
concatenate;
first;
end.
The compiler also raised this error: "test.pas(10,1) Fatal: Internal
error 201003031".

I don't know what I am doing wrong. But actually, the reason of my message started from another point.

Freepascal codes works with strings as usual. DLLs works with pchar. I could have declared the functions as the following:

function concatenate(name: pchar): pchar; cdecl;
function first(names: array of pchar): pchar; cdecl;

In the body of the functions, I would treat the conversions from pchar to string and from string to pchar again. But, when I used "cdecl", am I making this conversion automatically? Is there a compilation switch that allows me to write strings and the compiled code having correctly treated the allocations and disallocation of strings?

Suppose this VBA code:

public declare function concatenate lib "test" (name as string) as string

Is this the correct way to write the called function in Freepascal?

function concatenate(name: string): string; cdecl;
begin
result := 'Freepascal ' + name;
end;
Luciano de Souza

Reply by email

Michael Van Canneyt
to luciano de souza
luciano de souza wrote 18 lines
Hello all, At work, I need to create a Excel sheet containing certain more complex functions. I tried to use only VBA, but I released I will more productive if I could use a DLL writen in Pascal for the heavier code. Let see this DLL: library test; {$mode objfpc}{$H+} function concatenate(name: string): string; cdecl; begin result := 'Freepascal ' + name; end; function first(names: array of string):string; cdecl; // Warning: cdecl'ared functions have no high parameter

I very much doubt VBA understands the 'array of string' concept. It is typical for pascal.

Show quoted text 20 lines
begin result := names[0]; end; exports concatenate; first; end. The compiler also raised this error: "test.pas(10,1) Fatal: Internal error 201003031". I don't know what I am doing wrong. But actually, the reason of my message started from another point. Freepascal codes works with strings as usual. DLLs works with pchar. I could have declared the functions as the following: function concatenate(name: pchar): pchar; cdecl; function first(names: array of pchar): pchar; cdecl;

I think you need to use stdcall calling convention.

Show quoted text 6 lines
In the body of the functions, I would treat the conversions from pchar to string and from string to pchar again. But, when I used "cdecl", am I making this conversion automatically? Is there a compilation switch that allows me to write strings and the compiled code having correctly treated the allocations and disallocation of strings?

No, there is not.

Show quoted text 11 lines
Suppose this VBA code: public declare function concatenate lib "test" (name as string) as string Is this the correct way to write the called function in Freepascal? function concatenate(name: string): string; cdecl; begin result := 'Freepascal ' + name; end;

No, IMHO you will need to use pchar everywhere.

Michael.

Reply by email

luciano de souza
to Michael Van Canneyt

Thank you for the always rapid and gentile reply. Well, if there's no way to avoid pchar, let's go to pchar!

2015-06-06 4:27 GMT-03:00, Michael Van Canneyt :

Show quoted text 77 lines
On Sat, 6 Jun 2015, luciano de souza wrote: > Hello all, > > At work, I need to create a Excel sheet containing certain more > complex functions. I tried to use only VBA, but I released I will more > productive if I could use a DLL writen in Pascal for the heavier code. > > Let see this DLL: > > library test; > {$mode objfpc}{$H+} > > function concatenate(name: string): string; cdecl; > begin > result := 'Freepascal ' + name; > end; > > function first(names: array of string):string; cdecl; // Warning: > cdecl'ared functions have no high parameter I very much doubt VBA understands the 'array of string' concept. It is typical for pascal. > begin > result := names[0]; > end; > > exports > concatenate; > first; > end. > > The compiler also raised this error: "test.pas(10,1) Fatal: Internal > error 201003031". > > I don't know what I am doing wrong. But actually, the reason of my > message started from another point. > > Freepascal codes works with strings as usual. DLLs works with pchar. I > could have declared the functions as the following: > > function concatenate(name: pchar): pchar; cdecl; > function first(names: array of pchar): pchar; cdecl; I think you need to use stdcall calling convention. > > In the body of the functions, I would treat the conversions from pchar > to string and from string to pchar again. But, when I used "cdecl", am > I making this conversion automatically? Is there a compilation switch > that allows me to write strings and the compiled code having correctly > treated the allocations and disallocation of strings? No, there is not. > > Suppose this VBA code: > > public declare function concatenate lib "test" (name as string) as string > > Is this the correct way to write the called function in Freepascal? > > function concatenate(name: string): string; cdecl; > begin > result := 'Freepascal ' + name; > end; No, IMHO you will need to use pchar everywhere. Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Luciano de Souza

Reply by email

Serguei TARASSOV

Luciano,

you should follow binary compatible DLL requirements :

- stdcall call convention
- ordinal types only (shortstring may work too because they are char[256] and 0-th byte is the length)
- pointers (be attentive: if memory was allocated in DLL, you should free it in DLL too)

You can also use any suggestions and examples for writing DLL in C/C++.

Show quoted text 67 lines
Date: Sat, 6 Jun 2015 04:16:29 -0300 From: luciano de souza To: fpc-pascal Subject: [fpc-pascal] Writing a DLL to be use in a VBA code Message-ID: Content-Type: text/plain; charset=UTF-8 Hello all, At work, I need to create a Excel sheet containing certain more complex functions. I tried to use only VBA, but I released I will more productive if I could use a DLL writen in Pascal for the heavier code. Let see this DLL: library test; {$mode objfpc}{$H+} function concatenate(name: string): string; cdecl; begin result := 'Freepascal ' + name; end; function first(names: array of string):string; cdecl; // Warning: cdecl'ared functions have no high parameter begin result := names[0]; end; exports concatenate; first; end. The compiler also raised this error: "test.pas(10,1) Fatal: Internal error 201003031". I don't know what I am doing wrong. But actually, the reason of my message started from another point. Freepascal codes works with strings as usual. DLLs works with pchar. I could have declared the functions as the following: function concatenate(name: pchar): pchar; cdecl; function first(names: array of pchar): pchar; cdecl; In the body of the functions, I would treat the conversions from pchar to string and from string to pchar again. But, when I used "cdecl", am I making this conversion automatically? Is there a compilation switch that allows me to write strings and the compiled code having correctly treated the allocations and disallocation of strings? Suppose this VBA code: public declare function concatenate lib "test" (name as string) as string Is this the correct way to write the called function in Freepascal? function concatenate(name: string): string; cdecl; begin result := 'Freepascal ' + name; end; -- Luciano de Souza

Reply by email