Discussion:
Dynamically load function from DLL by Index (not by Name)
(too old to reply)
Bart
2011-01-02 23:34:55 UTC
Permalink
Hi,

I want something unusual.

I need to dynamically (as opposed to statically) load some functions
from a windows dll.
Normally this is not such a big problem using
Handle := LoadLibrary('some.dll')
and
FunctionAddress := GetProcAddres(Handle, Name)

However the functions I need are only exported by index, not by name,
so no luck with GetProcAddres().

A static example that _won't_ work:
function CharUpperBuffWrapW(lpsz:LPWSTR; cchLength:DWORD):DWORD;
stdcall; external 'shlwapi.dll' name 'CharUpperBuffWrapW';

A static example that _will_ work:
function CharUpperBuffWrapW(lpsz:LPWSTR; cchLength:DWORD):DWORD;
stdcall; external 'shlwapi.dll' index 44;

(MS info on shlwapi.dll also only provides indexes)

Does anyone have a suggestion how to load these functions dynamically?

Bart
--
Bart Broersma
***@tiscali.nl
(ff _ANTISPAM_ wegpoetsen uit dit adres natuurlijk)
Rudy Velthuis
2011-01-03 02:17:03 UTC
Permalink
Bart
FunctionAddress := GetProcAddress(Handle, PChar(44));

FWIW, CharUpperBuffWrapW is exported from shlwapi.dll and is available
in XP, but probably not in later versions.

Use CharUpperBuffW from user32 instead. CharUpperBuffWrapW simply wraps
that function anyway.

Functions that are only available by index are probably deprecated.
--
Rudy Velthuis http://rvelthuis.de

"Getting an education was a bit like a communicable sexual disease.
It made you unsuitable for a lot of jobs and then you had the urge
to pass it on." -- Terry Pratchett, Hogfather
Bart
2011-01-03 11:12:01 UTC
Permalink
Op Mon, 3 Jan 2011 03:17:03 +0100 schreef "Rudy Velthuis"
Post by Rudy Velthuis
Bart
FunctionAddress := GetProcAddress(Handle, PChar(44));
FWIW, CharUpperBuffWrapW is exported from shlwapi.dll and is available
in XP, but probably not in later versions.
Use CharUpperBuffW from user32 instead. CharUpperBuffWrapW simply wraps
that function anyway.
Functions that are only available by index are probably deprecated.
I know it is deprecated.
And it will probably not be available in future Windows versions.
It is however meant do be used in win9x.
That's why I want to dynamically load the function (only if needed)
and not statically, since the app will mostly run on XP+ systems.

Widestring Upper- and Lowercase functions are by default bound to
CharUpperBuffW and CharLowerBuffW in the RTL anyway.

Bye,

Bart
--
Bart Broersma
***@tiscali.nl
(ff _ANTISPAM_ wegpoetsen uit dit adres natuurlijk)
a***@aol.com
2011-01-03 14:03:28 UTC
Permalink
Post by Bart
Hi,
I want something unusual.
I need to dynamically (as opposed to statically) load some functions
from a windows dll.
Normally this is not such a big problem using
Handle := LoadLibrary('some.dll')
and
FunctionAddress := GetProcAddres(Handle, Name)
However the functions I need are only exported by index, not by name,
so no luck with GetProcAddres().
<snip>
Post by Bart
Does anyone have a suggestion how to load these functions dynamically?
Works for me :

var
CharUpperBuffWrapW : function(lpsz:LPWSTR; cchLength:DWORD):DWORD;
stdcall;

procedure TForm1.Button1Click(Sender: TObject);
var
HndDLL : THandle;
begin
HndDLL := LoadLibrary('shlwapi.dll');
try
if (HndDLL > 32) then
@CharUpperBuffWrapW := GetProcAddress(HndDLL, PChar(MakeLong(44,
0)));
Assert(Assigned(CharUpperBuffWrapW), 'Failed CharUpperBuffWrapW');
except
on E: Exception do
ShowMessage(E.Message);
end;
end;

Note MSDN Library comment on GetProcAddress :

"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."

RTFM <g>

Alan Lloyd
Bart
2011-01-04 13:11:33 UTC
Permalink
Op Mon, 3 Jan 2011 06:03:28 -0800 (PST) schreef "***@aol.com"
<***@aol.com>:

Thanks.
Post by a***@aol.com
"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."
Did not see that on the MS page I looked it up.
Post by a***@aol.com
RTFM <g>
I did, but obviously the wrong page, or I'm partially blind ;-)

Bart
--
Bart Broersma
***@tiscali.nl
(ff _ANTISPAM_ wegpoetsen uit dit adres natuurlijk)
Loading...