In python, for example, I write
from random import randint
Is there an analogue of this in delphi ?
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 ()
Source: https://ru.stackoverflow.com/questions/7664/
All Articles