【问题标题】:Converting Deterministic Path Code from C# to C++将确定性路径代码从 C# 转换为 C++
【发布时间】:2011-01-20 22:21:12
【问题描述】:

有谁知道我如何将它(从 C#)转换为 C++?

string location = Path.GetDirectoryName(Assembly.GetAssembly(typeof(ZigbeeCommUSB)).CodeBase);
location = location.Substring(location.IndexOf("\\") + 1);

谢谢。

编辑:附加信息

这是从 C# 转换为本机 C++。如果有另一种方法来获取当前执行的可执行文件的文件路径,我愿意接受。谢谢。

【问题讨论】:

  • 您在哪个部分遇到了问题?您是否正在从 C# 转换为托管 C++?或者您是否正在尝试转换为原生 C++?
  • Visual C++ ?还是标准 C++?
  • 这是针对原生 C++ 的。我需要获取可执行文件的位置。

标签: c# c++ code-conversion native-code


【解决方案1】:

来自上述链接的 Assembly.GetAssembly 示例

Assembly^ SampleAssembly;
// Instantiate a target object.
Int32 Integer1(0);
Type^ Type1;
// Set the Type instance to the target class type.
Type1 = Integer1.GetType();
// Instantiate an Assembly class to the assembly housing the Integer type.  
SampleAssembly = Assembly::GetAssembly( Integer1.GetType() );
// Gets the location of the assembly using file: protocol.
Console::WriteLine( "CodeBase= {0}", SampleAssembly->CodeBase );

如果您结合以上内容,您可能会更接近您想要做的事情。 如果这将在 Native C++ 中完成,您将遇到更多问题,有一个名为 "Fusion API" 的东西可能会帮助您在 GAC 中查找程序集。

【讨论】:

  • 谢谢,菲利普。我在原始帖子中添加了一些额外的细节。
  • @Jim,检查我的第二点“获取当前目录的路径”,GetCurrentDir 应该是你要找的。​​span>
  • 谢谢。微软现在调用函数GetCurrentDirectory
【解决方案2】:

要检索当前可执行文件的路径,请使用 GetModuleFileName 并使用 NULL hModule 参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2013-06-30
    相关资源
    最近更新 更多