Is there any way to do this

Started by
17 comments, last by discman1028 18 years, 1 month ago
http://img127.imageshack.us/img127/6796/screenshot3ye.jpg ...
Advertisement
#include <iostream>std::ostream & operator<<(std::ostream & stream, char const * const data){	return std::operator<<(std::operator<<(std::operator<<(stream, "Initialize\n"), data), "Clean Up\n");}int main(){	std::cout << "Hello World!\n";	return 0;}
or
#include <iostream>namespace my{int main(){	std::cout << "Hello World!\n";	return 0;}}int main(){	std::cout << "Initialize\n";	int r = my::main();	std::cout << "Clean Up\n";	return r;}
Σnigma
If you're just interested in hijacking main, why bother with #defines and namespaces when you can just change the entrypoint? :)
Quote:Original post by Enigma
code


Nice! No ugly #defines [smile]
Quote:Original post by bpoint
If you're just interested in hijacking main, why bother with #defines and namespaces when you can just change the entrypoint? :)


Nice idea, you can do something like this:

#pragma comment(linker, "/entry:Dummy")int main(void){	std::cout << "Hello World!" << std::endl;	return NULL;}void Dummy(void){	std::cout << "Foo" << std::endl;	main();	std::cout << "Bar" << std::endl;}
Ha, I forgot to log-in... That's me above :|
Quote:Original post by Anonymous Poster
Quote:Original post by bpoint
If you're just interested in hijacking main, why bother with #defines and namespaces when you can just change the entrypoint? :)


Nice idea, you can do something like this:

*** Source Snippet Removed ***


It crashes in VC6.0
Quote:Original post by TEUTON
It crashes in VC6.0

That source by itself probably would. None of the ctors are being called, which means libc and stdio file streams aren't even being initialized.

You'll have to call the ctor function table manually first.

Edit: Okay, I had a few minutes over lunch to whip up some code to do it right. This code works on VC6, 2003 and 2005. VC6's libc startup is different than 2003/2005, hence the changed initialization path. It also seems that VC2005's std::cout does not have any dependance on libc initialization, so the last post by raz0r does actually work.

Anyway, here's the code. It's not pretty. :)
#include <iostream>#define _CRTALLOC(x)typedef void (__cdecl *_PVFV)(void);extern "C" {// pointers to initialization sectionsextern _CRTALLOC(".CRT$XIA") _PVFV __xi_a[];extern _CRTALLOC(".CRT$XIZ") _PVFV __xi_z[];// C initializersextern _CRTALLOC(".CRT$XCA") _PVFV __xc_a[];extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z[];// C++ initializersextern int _heap_init(int);extern void _ioinit(void);extern void _cinit(void);// taken from internal.hextern void __cdecl _initterm(_PVFV *, _PVFV *);extern void _RTC_Initialize(void);};#pragma comment(linker, "/entry:entrypoint")int main(){	std::cout<<"Hello World!\n";	return 0;}void initialize(){#if _MSC_VER < 1300	// initialize heap	_heap_init(0);	// initialize lowio	_ioinit();	// do C data initialize	_cinit();#else	// initialize runtime C library	_RTC_Initialize();	// call C/C++ initializers	_initterm(__xi_a, __xi_z);	_initterm(__xc_a, __xc_z);#endif}void entrypoint(){	initialize();	std::cout<<"Initialize\n";	main();	std::cout<<"Clean Up\n";}


Note that dtors aren't called, and the onexit/atexit table is not even initialized. If you want full functionally, feel free to implement them. :)


[Edited by - bpoint on March 1, 2006 10:10:11 PM]
Nice solution Enigma! That's my fav. ;-0 Clean!
--== discman1028 ==--

This topic is closed to new replies.

Advertisement