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

Serguei TARASSOV serge at arbinada.com
Sat Jun 6 12:59:15 CEST 2015


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++.

On 06/06/2015 12:00, fpc-pascal-request at lists.freepascal.org wrote:
> Date: Sat, 6 Jun 2015 04:16:29 -0300
> From: luciano de souza<luchyanus at gmail.com>
> To: fpc-pascal<fpc-pascal at lists.freepascal.org>
> Subject: [fpc-pascal] Writing a DLL to be use in a VBA code
> Message-ID:
> 	<CAOUY=rWeygi89xf=67AXqFqZ8JcFtHk4okL18WoxP+BqP35dqA at mail.gmail.com>
> 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




More information about the fpc-pascal mailing list