Sign in Join

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

Mailing lists / lazarus / 2010-02

Call Load/Unload library from procedure

6 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

fabio

Hi,
I need help with libraries. I'm working on Win XP with lazarus 0.9.29 and fpc 2.4.1.
I attached sample project app and project library. I'm not sure whether this is a bug or something I do not know, the problem is that when I use library in public form procedure I get the Access Violation when procedure is ended.

I'll be grateful for help.

regards
Fabio
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.zip
Type: application/zip
Size: 4931 bytes
Desc: not available
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20100203/26457116/attachment.zip>

Reply by email

cobines
to fabio

2010/2/3 fabio :

Show quoted text 9 lines
Hi, I need help with libraries. I'm working on Win XP with lazarus 0.9.29 and fpc 2.4.1. I attached sample project app and project library. I'm not sure whether this is a bug or something I do not know, the problem is that when I use library in public form procedure I get the Access Violation when procedure is ended. I'll be grateful for help.

You have specified different calling conventions: cdecl in main program and stdcall in the library. I think calling conventions should match.

cobines

Reply by email

fabio
to cobines

Dnia 2010-02-03, o godz. 09:24:47
cobines napisa?(a):

You have specified different calling conventions: cdecl in main program and stdcall in the library. I think calling conventions should match.

I do not know what to do, it works only with cdecl in main and stdcall in lib. If I set correct calling conventions then I get Access Violation in in both cases.

regards
Fabio

Reply by email

cobines
to fabio

I'm not exactly sure, but it might have something to do with memory allocation, because it works with Word type, but not with strings. You cannot return strings because I think they are allocated by one memory manager (in dll) and deallocated by another (in main program). You would have to link main program and the dll to the same RTL. Maybe ask about this on the FPC mailing list if you don't get a better answer here.

cobines

Reply by email

Thierry Coq
to cobines: 2010/2/3 fabio : You have specified different calling conventions: cdecl in main program…

Hello,

I confirm the calling conventions should match. Maybe try using the pascal calling convention to debug your code, it is more robust.

In addition, using strings in DLL calls is a <bad idea> for a beginner. Start with arrays of char or PChar.
Allocate the memory to the PCHAR and pass it as a parameter to the DLL. Don't forget to provide the length of the string, so there is no buffer overrun.

For more complex calls, and if you compile your own DLLs, you might want to look here for an example (Dynamic Loading Library): http://www.tcoq.org/Lazarus.html, it reproduces the dynamic loading equivalent in Delphi.

BR
Thierry

cobines wrote 28 lines
2010/2/3 fabio : > Hi, > I need help with libraries. I'm working on Win XP with lazarus 0.9.29 > and fpc 2.4.1. > I attached sample project app and project library. > I'm not sure whether this is a bug or something I do not know, the > problem is that when I use library in public form procedure I get the > Access Violation when procedure is ended. > > I'll be grateful for help. > You have specified different calling conventions: cdecl in main program and stdcall in the library. I think calling conventions should match. -- cobines -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

fabio
to Thierry Coq

Dnia 2010-02-03, o godz. 10:49:32
Thierry Coq napisa?(a):

Show quoted text 11 lines
Hello, I confirm the calling conventions should match. Maybe try using the pascal calling convention to debug your code, it is more robust. In addition, using strings in DLL calls is a <bad idea> for a beginner. Start with arrays of char or PChar. Allocate the memory to the PCHAR and pass it as a parameter to the DLL. Don't forget to provide the length of the string, so there is no buffer overrun. ...
Thanks, I change to procedure and set additional parameter as pointer to
my string type Tstr:
   Tstr = record
      p : string;
   end;
   Pstr = ^Tstr;

SiTTestP = procedure (k: integer; outP: Pstr):

and it seems works ok :) Thanks.

regards
Fabio

Reply by email