Archived

This topic is now archived and is closed to further replies.

MFC: Getting actual executable path

This topic is 5044 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

Hi all, I''m working on a resident (taskbar) application that should run itself automatically at Windows'' startup. I''m using MSVC .NET 2002 with MFC (version 7 I guess). I figured out that I should add a new entry to the register in HKEY_LOCAL_MACHINE/ SOFTWARE/ Microsoft/ Windows/ CurrentVersion/ Run/ with a string value indicating what file to execute. But the problem is that I don''t want the application to have any kind of installation, so when you run it for the first time, it registers itself, and when you manually shut it down, it asks wether it should run itself at next startup, and if not, it removes the register entry. And here comes the problem: How does the application know what executable it''s being run from? I think that good ol'' console main() has the path in the first command line argument, but CWinApp object hasn''t got such a member. How can I get it? For example: If the application is in c:\program files\blah\foobar.exe, it should return "c:\program files\blah\foobar.exe", if it is c:\something.exe, it should return "c:\something.exe" and so on. I''m a newbie to MFC and I''m sorry if the solution is stupidly simple. And also be tolerant to my English... Any help appreciated.

Share this post


Link to post
Share on other sites
There are a variety of solutions to this...

CWinApp contains m_lpCmdLine, which may be of use to you (I've never used it myself). Alternative methods include using _tsplitpath to dissect the help file path (CWinApp::m_pszHelpFilePath) or using the SDK function GetModuleFilename e.g.

TCHAR szBuffer[MAX_PATH];
GetModuleFilename(NULL, szBuffer, MAX_PATH);

There's also AfxGetAppName(), but I'm not sure if the returned value is actually relevant...


Jans.


[edited by - jansic on February 21, 2004 6:54:06 AM]

Share this post


Link to post
Share on other sites
Thanks, man! GetModuleFilename works fine. I tried to use m_lpCmdLine before, but it returned only the extra parameters but not the path, and I also tried to use m_pszHelpFilePath before, and it remained unchanged when I copied the exe to any other path. But GetModuleFilename works exactly the way I suppose it to. Again, thanks!

Share this post


Link to post
Share on other sites