🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
8 messages
8 total messages Started by Mariusz Grecki Wed, 26 Mar 2003 08:40
Import functions from gll by index.
#17383
Author: Mariusz Grecki
Date: Wed, 26 Mar 2003 08:40
47 lines
2206 bytes
Hello all,

It is a shame, but I can not import a function from
kernel32.dll by index. Using the name it is possible
but by index GetProcAddress fails returning NULL.
What I am doing wrong?

Example:
=================================
HINSTANCE hlk32;
FARPROC LoadLibrary16;
FARPROC FreeLibrary16;
FARPROC GetProcAddress16;
FARPROC BeepA, BeepB;

.............
   hlk32=LoadLibrary("kernel32");

   BeepB=GetProcAddress(hlk32, (LPSTR)10);
   BeepA=GetProcAddress(hlk32, "Beep");
...........
   FreeLibrary(hlk32);
================================
BeepA is OK.
BeepB is NULL!

Any comments?

PS. In fact I do not need a Beep function,
I need functions for 16bit dll thunking
but I can not jump over that simple thing :-(

Thanks in advance.

--
Best regards.

Mariusz Grecki

****************************************************************************
 Dr Mariusz GRECKI               tel. +48 - 42  - 631 26 48
 Department of Microelectronics
 and Computer Science,           fax. +48 - 42  - 636 03 27
 Technical  University of Lodz     e-mail:grecki@DMCS.p.lodz.pl
 Al. Politechniki 11, 93-590 Lodz, Poland
 Url:                            http://www.dmcs.p.lodz.pl
****************************************************************************
Re: Import functions from gll by index.
#17384
Author: Vladimir Afanasyev
Date: Wed, 26 Mar 2003 08:54
17 lines
1340 bytes
Hi,

"Mariusz Grecki" <grecki@dmcs.p.lodz.pl> wrote:
>Hello all,
>
>It is a shame, but I can not import a function from
>kernel32.dll by index. Using the name it is possible
>but by index GetProcAddress fails returning NULL.
>What I am doing wrong?
>

It is really problem.
See my old answer about it:
http://216.101.185.148/scripts/isapi.dll/article?id=1A5ED5FD&article=3187234

Vladimir.
Re: Import functions from gll by index.
#17412
Author: Remy Lebeau (TeamB)
Date: Wed, 26 Mar 2003 19:10
16 lines
1399 bytes
"Mariusz Grecki" <grecki@dmcs.p.lodz.pl> wrote in message
news:3e8165eb@newsgroups.borland.com...

> Using the name it is possible but by index
> GetProcAddress fails returning NULL.
> What I am doing wrong?

You should be using MAKEINTRESOURECE, not LPSTR.  By using LPSTR, you're
passing the wrong value to GetProcAddress(), whereas MAKEINTRESOURECE
ensures the value is formatted correctly:

    BeepB = GetProcAddress(hlk32, MAKEINTRESOURCE(10));


Gambit
Re: Import functions from gll by index.
#17417
Author: Vladimir Afanasyev
Date: Thu, 27 Mar 2003 08:10
31 lines
2100 bytes
Hi,

"Remy Lebeau \(TeamB\)" <gambit47@yahoo.com> wrote:
>
>"Mariusz Grecki" <grecki@dmcs.p.lodz.pl> wrote in message
>news:3e8165eb@newsgroups.borland.com...
>
>> Using the name it is possible but by index
>> GetProcAddress fails returning NULL.
>> What I am doing wrong?
>
>You should be using MAKEINTRESOURECE, not LPSTR.  By using LPSTR, you're
>passing the wrong value to GetProcAddress(), whereas MAKEINTRESOURECE
>ensures the value is formatted correctly:
>
>    BeepB = GetProcAddress(hlk32, MAKEINTRESOURCE(10));

It is not exactly.
Off course you absolutely correct as a Perfect Programmer.
It is not good to use types that do not correctly coorrespond 
to meaning content of variable. And I agree with you in 
this case.
But practically both line give us the same.
It is possible to see it by debugger or CPU window.

The real problem it what I write in the reffer page in 
my another message: Microsoft did it specially to disable 
debugging kernel. That is funny.

Vladimir.
Re: Import functions from gll by index.
#17418
Author: Vladimir Afanasyev
Date: Thu, 27 Mar 2003 08:19
20 lines
1698 bytes
I can't enter in my ref.
So I put it here:

..
When I read these messages I remember old Matt Pietrek's book
"Windows95 System Programming Secrets" . 

..
And he tells that GetProcAddress is key function for Win95 because it works
with dll. The changes that was done for security:
1) There are no possibility to call GetProcAddress by ordinal for some
non-documented API function for KERNEL32.
2) There are no possibility to call GetProcAddress for functions from
system dll (>2GB boundary) in the debug mode (Ring3 restriction).
But Matt Pietrek say that it is funny because any good programmer can
write your own version of GetProcAddress if he know format of PE-module.
..

Vladimir. 
Re: Import functions from gll by index.
#17277
Author: Mariusz Grecki
Date: Thu, 27 Mar 2003 17:35
26 lines
2066 bytes
Thanks for informations,

I spend huge amount of time only to gain knowledge
that M$ does not give the full information in MSDN!
Now I understand the problem and can find solution
by analysis of the PE format of kernel32.dll or other way.
However I wonder how it is made by internals of
Delphi compiler/linker, because I have Delphi program
(rawwrite - written by John Newbigin ) that declares
------------------------
function  LoadLibrary16;         external kernel32 index 35;
procedure FreeLibrary16;         external kernel32 index 36;
function  GetProcAddress16;      external kernel32 index 37;
-----------------------
and that imports are done "automagicaly"!
I was trying to import those functions in BC++ Builder -
you know with what results.......
At this moment I have an idea to make a dll in Delphi
(using "automagic" importing mechanism) and to use this
dll from C++ program - I expect it will be easier than to
analyse the PE of kernel32 (I know, it is not to complex,
but .....)

Many thanks

Mariusz Grecki
Re: Import functions from gll by index.
#17300
Author: Vladimir Afanasyev
Date: Fri, 28 Mar 2003 09:44
30 lines
2138 bytes
Hi Mariusz,

"Mariusz Grecki" <grecki@dmcs.p.lodz.pl> wrote:

>However I wonder how it is made by internals of
>Delphi compiler/linker, because I have Delphi program
>(rawwrite - written by John Newbigin ) that declares
>------------------------
>function  LoadLibrary16;         external kernel32 index 35;
>procedure FreeLibrary16;         external kernel32 index 36;
>function  GetProcAddress16;      external kernel32 index 37;
>-----------------------
>and that imports are done "automagicaly"!

I do not know how Delphi do it.
May be Delphi "know" that kernel32 it is a "naughty boy" and 
use own way of mapping these function in virtual space of 
process.
I tryed to work with PE format and you can see my code in 
codecentral:
FindFunction seach functions in dlls on HDD (source)
http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=18081
I prepare small article for BDN by they did not publish it.
May be my uggly English or another problems.

There are a lot Matt Pietrek articles in MSJ/MSDN that you 
can find in online.
Best Regards,
Vladimir.
Re: Import functions from gll by index.
#17127
Author: user
Date: Mon, 07 Apr 2003 04:28
47 lines
2272 bytes
Mariusz Grecki wrote:
> It is a shame, but I can not import a function from
> kernel32.dll by index. Using the name it is possible
> but by index GetProcAddress fails returning NULL.
> What I am doing wrong?
> 
> Example:
> =================================
> HINSTANCE hlk32;
> FARPROC LoadLibrary16;
> FARPROC FreeLibrary16;
> FARPROC GetProcAddress16;
> FARPROC BeepA, BeepB;
> 
> .............
>    hlk32=LoadLibrary("kernel32");
> 
>    BeepB=GetProcAddress(hlk32, (LPSTR)10);
>    BeepA=GetProcAddress(hlk32, "Beep");
> ...........
>    FreeLibrary(hlk32);
> ================================
> BeepA is OK.
> BeepB is NULL!
> 
> Any comments?

The ordinal for Beep is 13 in my KERNEL32.DLL. I was able to get the 
following code to work as expected (both BeepA and BeepB had the same 
value):

void __fastcall TForm1::Button1Click(TObject *Sender)
{
     typedef BOOL (WINAPI * BeepProc)(DWORD, DWORD);
     HINSTANCE hlk32;
     BeepProc BeepA, BeepB;

     hlk32=LoadLibrary("kernel32");

     BeepB=(BeepProc)GetProcAddress(hlk32, MAKEINTRESOURCE(13));
     BeepA=(BeepProc)GetProcAddress(hlk32, "Beep");

     BeepB(100, 100);
     BeepA(200, 100);

     FreeLibrary(hlk32);
}
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads