如何在 WindowsC++/CLI 中获取应用程序可执行文件名称? [英] How to get the application executable name in WindowsC++/CLI?

查看:39
本文介绍了如何在 WindowsC++/CLI 中获取应用程序可执行文件名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据可执行文件名称更改应用程序的功能.没什么大不了的,只是改变显示的字符串和一些内部标识符.该应用程序混合使用本机和 .Net C++-CLI 代码编写.

I need to change the functionality of an application based on the executable name. Nothing huge, just changing strings that are displayed and some internal identifiers. The application is written in a mixture of native and .Net C++-CLI code.

我研究过的两种方法是在 Win32 中解析 GetCommandLine() 函数并在 .Net 中填充 AppDomain 和其他东西.但是,当从调试器运行时,命令行为空时,使用 GetCommandLine 并不总是有效..Net AppDomain 的东西似乎需要很多东西.

Two ways that I have looked at are to parse the GetCommandLine() function in Win32 and stuffing around with the AppDomain and other things in .Net. However using GetCommandLine won't always work as when run from the debugger the command line is empty. And the .Net AppDomain stuff seems to require a lot of stuffing around.

那么在 C++/CLI 中确定可执行文件名称的最好/最简单/最有效的方法是什么?(我有点希望我刚刚错过了 .Net 中可用的一些简单内容.)

So what is the nicest/simplest/most efficient way of determining the executable name in C++/CLI? (I'm kind of hoping that I've just missed something simple that is available in .Net.)

我应该提到的一件事是,这是一个使用 C++/CLI 的 Windows GUI 应用程序,因此无法访问传统的 C 风格主函数,它使用 Windows WinMain() 函数.

One thing that I should mention is that this is a Windows GUI application using C++/CLI, therefore there's no access to the traditional C style main function, it uses the Windows WinMain() function.

推荐答案

调用 GetModuleFileName() 使用 0 作为模块句柄.

Call GetModuleFileName() using 0 as a module handle.

注意:也可以使用argv[0]参数来main或调用GetCommandLine()如果没有主线.但是,请记住,这些方法不一定会为您提供可执行文件的完整路径.他们将返回用于启动程序的相同字符串.相反,调用 GetModuleFileName() 将始终为您提供完整的路径和文件名.

Note: you can also use the argv[0] parameter to main or call GetCommandLine() if there is no main. However, keep in mind that these methods will not necessarily give you the complete path to the executable file. They will give back the same string of characters that was used to start the program. Calling GetModuleFileName(), instead, will always give you a complete path and file name.

这篇关于如何在 WindowsC++/CLI 中获取应用程序可执行文件名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆