Link to home
Start Free TrialLog in
Avatar of rbohac
rbohac

asked on

GetProcAddress

I'm trying to dynamically load a DLL. I call LoadLibrary to get my handle, then I typically use GetProcAddress to assign the function to a variable. However, I need to be able to do it using the known index number instead.

Statically I do it like this: function Myfunction(MyData:Integer):Integer; stdcall external 'MyDLL.dll' index 76;

How can I do this dynamically?
Avatar of mokule
mokule
Flag of Poland image

var
  sr: TSearchRec;
  h: Longint;
  GetStatMenu: function: Pchar; stdcall;
  GetStatSubMenu: function: Pchar; stdcall;
begin
        h := LoadLibrary(PChar(sr.Name));
        if h <> 0 then
          begin
          GetStatMenu := GetProcAddress(h,PChar('GetStatMenu'));
          GetStatSubMenu := GetProcAddress(h,PChar('GetStatSubMenu'));
          if Assigned(GetStatMenu) and Assigned(GetStatSubMenu) then
            begin
// functions available
Avatar of rbohac
rbohac

ASKER

Right.. using the index number please. NOT the name
Avatar of rbohac

ASKER

Nevermind. I figured it out. You can pass the index as an ordinal parameter:

GetProcAddress(hDLL, PChar(Integer(76)));
Avatar of Wim ten Brink
I was about to give the correct answer but noticed the question has been answered here. :-)
However, this happens to be useful information so I suggest to refund, close and PAQ it instead. For completeness, I'll add more information from the MSDN site. Keep in mind that if you use an ordinal number, the high-word must be 0.

The GetProcAddress function retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName);

Parameters
hModule [in] Handle to the DLL module that contains the function or variable. The LoadLibrary or GetModuleHandle function returns this handle.
lpProcName [in] Pointer to a null-terminated string that specifies the function or variable name, or the function's ordinal value. If this parameter is an ordinal value, it must be in the low-order word; the high-order word must be zero.

Return Values: If the function succeeds, the return value is the address of the exported function or variable. If the function fails, the return value is NULL. To get extended error information, call GetLastError.
--> so I suggest to refund, close and PAQ it instead...
I agree with Workshop_Alex also due for his good additions

F68 ;-)
Nevermind, misreaded the -> PAQ-ing the question and refunding points
:)
Just a side note: Using GetProcAddress with index numbers doesn't work for kernel32 APIs in win9x. Microsoft did this to prevent system hackers from accessing undocumented APIs. Of course there are ways to work around that limitation...   :-)
Win9x? Does that still exist? ;-)
Oh yes. I still get even support requests about win95 now and then...   :-/
Unfortunately it still exists :( I've about 30 customers using already Win95 and there's no way to convince them to upgrade their systems... :(((
Win95... urggh... well, at least I managed to persuade all my Win95 users to upgrade to at least IE4 to get some of the *not-so-new-anymore* new features...
ASKER CERTIFIED SOLUTION
Avatar of Lunchy
Lunchy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ee_ai_construct, it has been answered by the expert himself. We just added a few more comments to this question to complete the answer. My suggestion was to PAQ it and am glad it is PAQ'ed.