In for example, I write

from random import randint 

Is there an analogue of this in ?

    2 answers 2

    Of course there is. Separate modules are represented as DLL or other PE files. There are two main ways of loading - static and dynamic.

    Static:

     interface ... function Nya(Arg1: Integer): Integer; stdcall; // соглашение вызова может быть и иным ... implementation ... function Nya; external 'Nya.dll'; 

    After that, the function Nya can be used wherever this .pas file is in the uses section. The function will be written to the exe import section and in the absence of the library the program will simply not start.

    Dynamic:

    You must declare a type for the function — so that the compiler knows what arguments it takes and what it returns. After that, load the library before use, find the function in it, get its address and use:

     type ... TNya = function(Arg1: Integer): Integer; stdcall; ... implementation ... function TestNya() var hDLL: THandle; Nya: TNya; begin // Попытка загрузки библиотеки hDLL := LoadLibrary('Nya.dll'); // Если библиотека загрузилась if (hDLL <> 0) then // Попытка найти функцию в библиотеке и получить адрес @Nya := GetProcAddress(hDLL, 'Nya'); // Функция имеется if(@Nya <> Nil) // Использование ShowMessage(IntToStr(Nya(123)); end; // Выгрузка библиотеки - она больше не нужна FreeLibrary(hDLL); end; end; 

    This way you can make a plugin system, for example. When library names are unknown in advance.

      I am not familiar with python, but when it comes to initializing a random number generator, this

      Randomize

      Well, and then to generate

      Random ()

      • It was just an example. I generally speak about import of separate functions from modules. - hamstertamer
      • Difficult to understand what it was about. To use individual modules, you just need to enter them in the uses clause and all functions (as well as everything else) will be available. Maybe if you can describe in detail the task, what needs to be done, I can tell you how to implement it in delphi. - Vahan Av
      • I meant import of one function from the module, but not its all. lano, no longer needed. - hamstertamer