I am seeing more and more code using 400000h instead of hInstance for the HINSTANCE value in CreateWindowEx. 400000h = 4194304 in decimal right? Isn't the handle to my exe different every time I start it? If so, why use a fixed number?
Posted on 2002-06-22 22:19:41 by Gunner
It's always 4194304. :) You can eliminate the use of GetModuleHandle but this should not be the case. What if MS changes the routines in the future and that value will no longer be 4194304 ??? Your app will break!! :)...
Posted on 2002-06-22 22:25:27 by stryker
Thanks Stryker! I just thought GetModuleHandle returns something different every time.. So, every exe, has the same hInstance is what you are saying?
Posted on 2002-06-22 22:52:08 by Gunner
yep!!! :)
Posted on 2002-06-22 22:57:27 by stryker
Stryker and Gunner, I think 400000h will always work, as long as the "ImageBase" in PE header is 400000h.


I realise I was wrong - ImageBase is only a perfered address, it may change if the image can't be loaded at that address.

And by the way is the standard definition of "module handle" the RVA of its image?
Posted on 2002-06-22 23:25:09 by C.Z.
yes I think ms has a reason for using that particular number (not sure what it is) so it is unlikely to change but personally I can not see anything to be gained in not using GetModuleHandle.
Posted on 2002-06-22 23:27:56 by Kudos
not every exe uses 400000h. IE doesn't, and i don't think vb applications do either
Posted on 2002-06-23 00:43:48 by Sloat
Gunner,

Stryker is right, the hInstance that is returned from GetModuleHandle is the actual start address of the EXE file in memory which currently is 400000h but if Microsoft ever change their mind with their OS design, this could change.

You are safer to use GetModuleHandle because it will get whatever the load address of the EXE file may be in another OS version. Each running application has its own address space so the hInstance for one app will not work in another, even though in each app, its the same address.

Note that DLLs do not load at 400000h and their load address will vary depending on what else is loaded at the time so you will always have to use GetModuleHandle if you need the hInstance in a DLL.

Regards,

hutch@movsd.com
Posted on 2002-06-23 02:19:42 by hutch--
For what it's worth, 400000h *will* *always* work, because most EXE's have no reloc section anyway, and if MS makes 400000h a "unloadable" address in a future Win32 implementation, those programs (99% out there) will break anyway. So this will never happen.

DLL's instead should never rely on fixed addresses.
Posted on 2002-06-23 02:42:43 by Maverick
Interesting! Thanks all.
Posted on 2002-06-23 08:58:09 by Gunner

So this will never happen.



And if it does, you can earn some funds by releasing "Version 2.x, works on WindowsXTRA 2022, upgade only $499"

:)
Posted on 2002-06-23 09:46:44 by bazik
Port to Windows RG! Then you're program will *always* run as it is expected to in RG :grin:
Posted on 2002-06-23 13:56:19 by jademtech
Your initial question is wrong.
It supposed to be why use base constant.
it's by LINK default is 400000h, but can be change by option BASE:.
For example BASE:700000
image base will be 700000h
and module handle will be 700000h
For PE in Win32, handle of your app is equal image base
in other words equal to linear address were your app is loaded by loader, loader tries to load to adress speciafied as base if it's initial executable it always secceed.

It was funny to read about other horrable times from people here, 'cause by default of usual make file they use relocation stripped from their exe's and it doesn't bother them at all :)

Why use it?
Usually to save clocks for GetModuleHandle, and 1 clock each for pushing it as a param when you need pass your app handle as param.
Not big deal, matter of taste mainly.

As far as I recall, I was almost the only one for the first time who start post code with
hInstance equ 400000h.

But I did it in purpose mostly not as example of optimization,
You see Iczelion did a greate thing with his tuts, and others too, but lot of people starting with Win32asm,
didn't use their brains reading Iczelion explonations and just copy\paste his code as template, with my bad English and their stupid approach I couldn't bring their attention to something important for Win32 coding, 'cause I knew - they fistly tried the code, and only then maybe would try to understand it.
So I started post code that worked but use image base as hInstance, didn't have WinMain, exit thread without
testing return from GetMessage etc. etc.
Just in hope that they see unusual for them pieces of code and maybe start think "Why"?
As you did for example.
You probably read Iczelion tutorials, but did it the same why - not paying attention for his theoretical parts.
He already explained it.
But you start asking about 400000h not after having read it, but after you started spotting 400000h instead of hInstance from GetModuleHandle :)
That's the whole point why I posted examples here with code that doesn't look as templates they got used to.
I'm not against other languages but think that main power of asm is better chance to understand and control code. So if people get used to use some code they don't understand, or uses just the same technique that compilers do, there is no point for them use assembly, thy just torchering themselves without any proffit :)
Posted on 2002-06-23 15:00:30 by The Svin




And if it does, you can earn some funds by releasing "Version 2.x, works on WindowsXTRA 2022, upgade only 9"

:)
bAZiK: in that eventuality, on WindowsXTRA 2022 almost nothing will run. It's normal and absolutely usual for PE EXE's to have no reloc.. so it either gets 400000h, or doesn't load at all. This is not only true for Mr. BadKoder new program, it's true for most of the programs released until today. Quite many. :)
Posted on 2002-06-23 15:31:33 by Maverick


And if it does, you can earn some funds by releasing "Version 2.x, works on WindowsXTRA 2022, upgade only 9"


Not too bad... after the hyper inflation that followed the Second World Tech War of 2017 and normal inflation, that equates to about 17c (US).
Posted on 2002-06-23 20:46:02 by jademtech

So if people get used to use some code they don't understand, or uses just the same technique that compilers do, there is no point for them use assembly, thy just torchering themselves without any proffit :)

Your correct. When I started in asm I always used a WimMain and all my code was similar to the way I would code in c, I thought just because I was using asm that my programs will always be faster. But compilers are doing optimizations so I learned there is no point in coding everything in asm with I'm not going to learn how to optimize properly.
Posted on 2002-06-23 21:35:40 by Kudos
If anything I spend too much time thinking about code rather than doing anything. I read a lot of stuff about optimization and other junk but I have no real code to show for it.

<Off Topic>
I thought when someone excluded DispatchMessage (called the callback function manually), that that was really cool. Because it gave you soo much freedom but I don't know how reliable that is, anyone have any info on that.
</Off Topic>

I think it is possible for a later version of windows to have relocation tables. I think they are usually created by the linker and if microsoft had reason too then they'll convince the people who make compilers and such to be aware of it and add the relocation table code. Eventually most software will run except for older apps that will run in a ClassicWindows box:) they'll probably be too fast on modern computers so they can take some kind of emulation.

anyways that's my two (hunderd) cents, I'm going to bed.
Posted on 2002-06-24 00:52:54 by Satrukaan
I agree with The Svin a lot!! Things get boring when they have become the stereotype.
Posted on 2002-06-24 05:57:06 by C.Z.
Oh, this is a realy interesting thread !!! That's why I've rated it with four stars (especially the post from The Svin.:alright:
Posted on 2002-06-24 06:27:41 by Marwin
Originally Posted by The Svin

it's by LINK default is 400000h, but can be change by option BASE:.
For example BASE:700000
image base will be 700000h
and module handle will be 700000h
I seem to remember I read this somewhere but I think 400000h should be use for windows 95/98 EXE's, for NT you can change it through the linker options. I've read that Windows 95/98 EXE's cannot have an image base aside from 400000h. If this is true, 400000h should be use for win95/98 and winNT compatibilities.

Can anyone confirm this? I don't have a 95/98 machine.
Posted on 2002-06-24 13:38:19 by stryker