获取完整路径并转换为常量

时间:2018-05-16 21:41:09

标签: c#

美好的一天。我正在尝试为我的需求重新制作dll。即,将搜索路径更改为dll。

的DllImport:

[DllImport(NativeLibraryName, EntryPoint = "SteamAPI_Init", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool SteamAPI_Init();

常数:

internal const string NativeLibraryName = @"steam_api64";

获取文件夹路径:

public static string SteamFolderPath()
{
      var r = Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Valve\Steam", "InstallPath", null);
      var rpath = r + "/steamapps/common/mygame/steam_api64";
      return r.ToString();
}

当我尝试做这样的事情时,错误就会消失:

internal const string NativeLibraryName = SteamFolderPath() + @"steam_api64";

我试过静态只读字符串。什么都没有帮助。希望只为你)怎么样?如何首先尝试获取第一个路径,然后将其置于常量?

P.S。我正在重做Steamworks.NET库。

1 个答案:

答案 0 :(得分:0)

必须在编译时知道常量。它不能取决于函数的返回值。

您需要使用SetDllDirectory将Steam路径添加到DLL搜索路径:

[DllImport("kernel32")]
static extern bool SetDllDirectory(string lpPathName);
string SteamPath = Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Valve\Steam", "InstallPath", null) + @"\steamapps\common\mygame\steam_api64";
SetDllDirectory(SteamPath); 

[DllImport("steam_api64")]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool SteamAPI_Init();