Sign in Join

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

Mailing lists / fpc-pascal / 2015-12

Writing DLLs to by used in Excel VBA code

4 messages · 3 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,

I want to use a DLL writen in Pascal to provide functions for a Excel VBA code:

The example code of my DLL is:
library CE;
{$mode objfpc}
uses
Sysutils;
function level(x1, x2: integer): integer; cdecl; export;
begin
result := x1 + 2*x2;
end;
exports
level name 'level';
end.

In VBA, I did the following code:

Public Declare Function dlevel Lib "CE" Alias "level" (ByVal x1 As Integer, ByVal x2 As Integer) As Integer

Function nivel(ByVal x1 As Integer, ByVal x2 As Integer) As Integer nivel = dlevel(x1, x2)
End Function

In the sheet, I did:

=nivel(2;5)

And I got:
#value

I really don't know what to do. Seemlngly, VBA code is correct. I don't know so much about DLLs, would be possible to have a mistake in my Pascal code?

Does someone have any idea?

Best regards,

Luciano de Souza

Reply by email

Marc Santhoff
to luciano de souza
luciano de souza wrote 20 lines
Hello all, I want to use a DLL writen in Pascal to provide functions for a Excel VBA code: The example code of my DLL is: library CE; {$mode objfpc} uses Sysutils; function level(x1, x2: integer): integer; cdecl; export; begin result := x1 + 2*x2; end; exports level name 'level'; end.

If you're on windows the declaration "cdecl" should be "stdcall".

HTH,
Marc

Marc Santhoff

Reply by email

luciano de souza
to luciano de souza: I want to use a DLL writen in Pascal to provide functions for a Excel VBA code: The…

The problem is solved. Actually, I didn't know that, in my Linux, sharable libraries are only accessible from /usr/lib.

As I was trying to access a lib in another directory, Libreoffice was not able to find it.

Regards,

Luciano

2015-12-11 17:15 GMT-02:00, luciano de souza :

Show quoted text 51 lines
Hello all, I want to use a DLL writen in Pascal to provide functions for a Excel VBA code: The example code of my DLL is: library CE; {$mode objfpc} uses Sysutils; function level(x1, x2: integer): integer; cdecl; export; begin result := x1 + 2*x2; end; exports level name 'level'; end. In VBA, I did the following code: Public Declare Function dlevel Lib "CE" Alias "level" (ByVal x1 As Integer, ByVal x2 As Integer) As Integer Function nivel(ByVal x1 As Integer, ByVal x2 As Integer) As Integer nivel = dlevel(x1, x2) End Function In the sheet, I did: =nivel(2;5) And I got: #value I really don't know what to do. Seemlngly, VBA code is correct. I don't know so much about DLLs, would be possible to have a mistake in my Pascal code? Does someone have any idea? Best regards, -- Luciano de Souza
Luciano de Souza

Reply by email

Mark Morgan Lloyd
to luciano de souza
luciano de souza wrote 5 lines
The problem is solved. Actually, I didn't know that, in my Linux, sharable libraries are only accessible from /usr/lib. As I was trying to access a lib in another directory, Libreoffice was not able to find it.

That's more of a limitation of the application program than anything else. I've definitely got a Pascal program calling a Pascal-written .so from the same directory that contains the main binary, and in most cases it's possible to tailor the path using- among other things- LD_LIBRARY_PATH.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email