Sign in to follow this  
savedbygamesandcodes

Best way to make a executable file to open itself?

Recommended Posts

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)

Share this post


Link to post
Share on other sites
Main's argv[0] provides the location of the executable that's running if that's what you mean... o.o


-Etyrn

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
won't this crash? you open it, it opens itself, it opens itself again and again and again and again...

Share this post


Link to post
Share on other sites
Quote:
Original post by savedbygamesandcodes
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.


You aren't likely to find anything interesting/useful within the file that represents the executable - at least, nothing that you couldn't already use much more easily by using the appropriate resource APIs. If you mean the source file, keep in mind that there is no guarantee that the user kept that with the same name, in the same folder or even left it on the computer at all.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this