* * *

Author Topic: DLL functions  (Read 621 times)

justnewbie

  • Full Member
  • ***
  • Posts: 129
DLL functions
« on: March 12, 2018, 12:28:26 pm »
Hi Lazarus Tutors,
Newbie here (please consider). I'm using the latest Lazarus version (32bit) and am trying to make a DLL that I want to use with MetaTrader.
It is a most basic DLL, just 1 function like this:

Code: Pascal  [Select]
  1. library firstdll;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   sysutils;
  7.  
  8. function foo(y: unicodestring; z: integer): PChar; stdcall;
  9. var
  10.   s : unicodestring;  
  11. begin  
  12.   s := y + ' '+ IntToStr(z);
  13.   result := PChar(s);
  14. end;
  15.  
  16. exports
  17.   foo;
  18.  
  19. begin
  20. end.

When I use it in the MT then these can be found as dependencies (attached picture).
My question: where those functions come from (I was only using sysutils)? Why are those functions red? I think they come from the sysutils.
I think my "foo" function is OK, because it is green (and works well). How can I make the others as well to green (or delete them if they are unnecessary)?
(Please ignore the difference between code/picture of the foo function.)
Thanks

« Last Edit: March 12, 2018, 01:33:07 pm by justnewbie »

PascalDragon

  • Full Member
  • ***
  • Posts: 114
  • Compiler Developer
Re: DLL functions
« Reply #1 on: March 12, 2018, 02:04:03 pm »
Those functions are imported by your library (or more precisely the RTL) from the system libraries named there (kernel32.dll, user32.dll, etc.). They are part of every Windows system so you don't need to care about them.

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: DLL functions
« Reply #2 on: March 12, 2018, 02:31:19 pm »
Thank you for the answer. But this thing is confusing for me. Why are these functions there? For example, there are 9 functions for user32.dll. But, user32.dll contains more hundreds functions. Why excatly these 9 are there? Do I need them? Or, if I do not need them, how can I remove them?

WooBean

  • Jr. Member
  • **
  • Posts: 71
Re: DLL functions
« Reply #3 on: March 12, 2018, 02:38:35 pm »
Hi justnewbie,

if you read more about using external functions you will find that using (unicode) strings as parameters or results can be a trouble. Your foo function code returns from dll only pointer to one character - far less then (in mind) designed.
Just to make your code working I changed foo definition (both in dll and a hosting project).
Code: Pascal  [Select]
  1. function foo(y: ANSIstring; z: integer): ANSIstring; stdcall; //dll library
  2. function foo(y: ANSIstring; z: integer): ANSIstring; stdcall; external 'firstdll'; //hosting project
  3.  
After the change I can compile and execute hosting project with references to external fibrary.
BTW, I did  it especially using Lazarus 1.8.0, FPC 3.0.4 in 32-bit version for Windows. 



WooBean, a newbie too.
« Last Edit: March 12, 2018, 04:03:28 pm by WooBean »
Win7/64, Lazarus 1.8 win64-win64, FPC 3.0.4

justnewbie

  • Full Member
  • ***
  • Posts: 129
Re: DLL functions
« Reply #4 on: March 12, 2018, 02:49:48 pm »
Thank you. To be honest, originally I used ansistring, because in the example https://www.forexfactory.com/showthread.php?t=219576 I saw there was ansistring. It did not work with MT, because MT uses unicodestring. My code worked well using unicodestring.
And what about the original question? Any help?
« Last Edit: March 12, 2018, 03:21:58 pm by justnewbie »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus