# startup function

This topic is 4415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know there was a way to change your startup function in Turbo C Compiler #pragma startup abc But I want to do that in VC6.0 and DEV C++ Plz tell me that.

##### Share on other sites
I had never heard of this pragma before, and seriously, I don't see why you need it.

From what I could gather, this lets you define a function to be called before main()?

Can't you do your processing at the beginning of the main function?

static int myInitFunc(){    // do your stuff here}static int callInitHere = myInitFunc();int main(){}

or

struct InitStuff{    InitStuff()    {        // Stuff to be executed before main    }    ~InitStuff()    {        // Stuff to be called after main    }} Init;int main(){}

But once again, why would you need that?

##### Share on other sites
Quote:
 Original post by xEricx*** Source Snippet Removed ***

Why are you using static function and static int. Only global function and global int also works fine.

##### Share on other sites
By declaring them static it makes them only available from within the file where main() is.

##### Share on other sites
Quote:
 Original post by LessBreadYou have to build your own stub file. Here are a few links to get you started.Creating Small Win32 Executables - Fast BuildsReduce EXE and DLL Size with LIBCTINY.LIBTechniques for reducing Executable sizeCreating Self-Installing Packages

##### Share on other sites
No problem.

xEricx's approach is quite a lot easier if all you want to do is to run some code before entering main. But if you want to take complete control over the actual startup function, you'll need to replace the stub. That can be quite a chore - setting up environment/argument variables, exception frames, floating point precision in some cases, and calling constructors and the like. It's a lot of work. Craft a small project - console or gui - with a main or WinMain that does nothing but return 0. Compile it and then disassemble the resulting exe. Actually, to make it easy to locate the main/WinMain function assign an easily recognizable value to an integer inside the function (eg. int x = 0xDEADBEEF; ). Search the disassembly for 0xDEADBEEF and work backwards and fowards from there to determine the start and end points of the main/WinMain function in the disassembly. All of the other code in the disassembly constitutes the stub (for the most part - there might be some data values in the stub that don't end up in the code). If you're gonna replace the stub, it helps to understand what the default stub does. And it also helps to remember that the stub varies from compiler to compiler, so what you learn about the stub used by one compiler doesn't necessarily hold for every compiler.

##### Share on other sites
For VC you don't need a custom stub. Actually I'm not sure how a custom stub would be at all relevant.

All you need to do is to pass the /entry switch to the linker and give it the name of your startup function. Please note that doing this sort of thing is fairly serious mojo and you will be bypassing all the usual startup logic that needs to happen to (e.g.) get the C/C++ runtime libraries initialized properly and get the global state setup. You definitely shouldn't do that sort of thing unless you know exactly what you're getting into, have a very specific reason, and can't do it any other way.

##### Share on other sites
Quote:
 Original post by Anon MikeFor VC you don't need a custom stub. Actually I'm not sure how a custom stub would be at all relevant.All you need to do is to pass the /entry switch to the linker and give it the name of your startup function. Please note that doing this sort of thing is fairly serious mojo and you will be bypassing all the usual startup logic that needs to happen to (e.g.) get the C/C++ runtime libraries initialized properly and get the global state setup. You definitely shouldn't do that sort of thing unless you know exactly what you're getting into, have a very specific reason, and can't do it any other way.

The "serious mojo" that you describe amounts to the same thing as a custom stub.

##### Share on other sites
We must be talking about different things with the word "stub". By stub I mean the DOS stub that every Win32 executable has embedded in it who's original purpose was to tell a DOS user that this app isn't actually a DOS app and that you need to be running Windows. This code doesn't even get touched by modern versions of Windows and has no effect on application startup.

It sounds like you are using "stub" to mean the C/C++ runtime initialization code. I've never heard that referred to as a stub before.

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
17

• 10
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999987
×