I am complete new to pascal.
I want to call my function in .dll file in free pascal and I get following error when I run the project:
The procedure entry point GetProcAddress could not be located in the dynamic link library HNLib.dll.
here is the code:
Program Test;
function GetProcAddress : Integer; cdecl; external 'HNLib.dll';
function GetProcAddress : Single; cdecl; external 'HNLib.dll';
procedure GetProcAddress( X : Single); cdecl; external 'HNLib.dll';
procedure GetProcAddress; cdecl; external 'HNLib.dll';
begin
GetProcAddress( 5.5 );
readln;
end.
.pas file and dll are in one directory.
Please Help ME!
GetProcAddressis not what you seem to think it is; it’s purpose is to locate named procedures or functions in a DLL and return the address of that function so it can be called from your code. You have to first useLoadLibraryto load the dynamic link library (DLL) into memory, and then pass a handle to that DLL as the first parameter ofGetProcAddressand the name of the function whose address you want as the second parameter. If the function can be found in the DLL, it’s address is returned, and you can use that address to call the function.(In addition,
GetProcAddressis pretty Windows-specific, and the majority of functions in the WinAPI arestdcalland notcdecl. Unless you have documentation saying that the functions are using thecdeclcalling convention, you should probably usestdcall.)You would also need at least the
Windowsunit in your uses clause, since that’s whereGetProcAddressandLoadLibraryare declared.See the WinAPI documentation on LoadLibrary and GetProcAddress for more information.
For a beginning programmer, you’ll probably find it easier to use static linking of the functions instead of dynamic (which you get with
GetProcAddress). An example of static linking would be (untested !!!- just a quick code example, since I don’t have ‘HNLib.DLL’ to link against):Note that when statically linking DLL functions like this, your DLL must be available when your app starts, and the function must be contained in that DLL; if not, your application won’t load.
Also, note that you can’t typically have multiple functions with the same name in the DLL, as there’s no information available to use to figure which one to load when the load is being done. Each should have a separate, distinct name or the loading will probably fail.