[fpc-pascal] Writing a DLL to be use in a VBA code

Michael Van Canneyt michael at freepascal.org
Sat Jun 6 09:27:04 CEST 2015



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.



More information about the fpc-pascal mailing list