Jump to content
  • Advertisement
Sign in to follow this  
savedbygamesandcodes

Best way to make a executable file to open itself?

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Guest Anonymous Poster
never mind...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!