Best way to make a executable file to open itself?

Started by
9 comments, last by Zahlman 17 years, 10 months ago
I can make a executable file to open itself if I know it's name(does it work on any OS?). I want to do that I don't need know it's name use C std lib, can I?(I don't know whether there's a C std lib function to get itself's name)
Advertisement
Were those questions? lol. Maybe you can rephrase your sentences...I can't even understand what you're asking..
Main's argv[0] provides the location of the executable that's running if that's what you mean... o.o


-Etyrn
Under UNIX, there is no good way of making an executable open itself, although depending on the OS, you may be able to walk /proc or some similar API to find the executable file handle or image.

Under Windows, it's easy to find your own executable; you can use GetModuleFileName() on the hInstance passed to your WinMain(). If you're a DLL, you can use the Snap32 API and get a process snapshot, then walk the images until you find one that contains one of your functions (the address of which you get by taking address-of-function, natch :-).

However, Windows doesn't necessarily like additional opens of an executable file. You're probably better off using the resources API, which loads data from the .EXE file. Under UNIX, you can play tricks with the loader and ELF sections to put whatever data you want into some large global char array and reference it by variable name.
enum Bool { True, False, FileNotFound };
I mean whether a exe can get itself's name use C std lib to make it open itself...
Quote:Original post by Etyrn
Main's argv[0] provides the location of the executable that's running if that's what you mean... o.o

Thanks! I just forgot it! but do it(this C language feature) be implemented on any system?
Quote:Original post by hplus0603
Under UNIX, there is no good way of making an executable open itself, although depending on the OS, you may be able to walk /proc or some similar API to find the executable file handle or image.
...


Forking is pretty close.
Quote:Original post by hplus0603
Under UNIX, there is no good way of making an executable open itself, although depending on the OS, you may be able to walk /proc or some similar API to find the executable file handle or image.

Thank you, hplus0603. So, I can't do that in UNIX?

And my "open" mean open to read/write a file, not mean run a file.
won't this crash? you open it, it opens itself, it opens itself again and again and again and again...
never mind...
Here read my post.

This topic is closed to new replies.

Advertisement