开发者

shellexec and c:\program files - x86 and x64 portability

开发者 https://www.devze.com 2023-01-23 04:37 出处:网络
I\'m modifying an open-source GUI (written in c) to add a new menu item to the tray app. The new menu entry executes a file (update.exe) in the program root in c:\\program files directory. Im using sh

I'm modifying an open-source GUI (written in c) to add a new menu item to the tray app. The new menu entry executes a file (update.exe) in the program root in c:\program files directory. Im using shellexec() and it works fine on x86, but since the path is different on x64 - c:\program files (x86), it fails to load. I'd like to use something to the effect of an environment variable like %programfiles%. What is an elegant solution to this?

Alternatively, I do have the ability to change where th开发者_JAVA技巧e update.exe file is stored. Putting it in 'program files' just seemed the most logical. For the problem above, should it go outside of program files? If so where?


You can use GetModuleFileName() to retrieve the full path to the executable. Then you'll just have to cut the filename from the end, and you'll have the complete directory path.

Something like this should to the trick:

TCHAR szPath[MAX_PATH];
LPTSTR szFileName;

GetModuleFileName(g_hInstance, szPath, sizeof(szPath)/sizeof(TCHAR));
szFileName = _tcsrchr(szPath, TEXT("\\"));
*szFileName = TEXT('\0');

// szPath now contains the path

(Warning! Untested!)

0

精彩评论

暂无评论...
验证码 换一张
取 消