* * *

Author Topic: DLL functions  (Read 247 times)


  • New member
  • *
  • Posts: 5
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;
  3. {$mode objfpc}{$H+}
  5. uses
  6.   sysutils;
  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;
  16. exports
  17.   foo;
  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.)

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


  • Jr. Member
  • **
  • Posts: 81
  • 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.


  • New member
  • *
  • Posts: 5
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?


  • Jr. Member
  • **
  • Posts: 57
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
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


  • New member
  • *
  • Posts: 5
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 »



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