Skip to content

dotnet C# 判断应用程序在 Wine 里运行的方法

Updated: at 02:05,Created: at 02:05

本文将记录如何在 C# dotnet 代码里面判断当前进程运行在 Wine 里面

本文记录的方法由 lsj 提供,我只是代为记录的工具人

根据 Wine 官方文档: https://gitlab.winehq.org/wine/wine/-/wikis/Developer-FAQ#how-can-i-detect-wine

可通过 ntdll 导出的 wine_get_version 方法进行判断。正常不运行在 Wine 里面的程序,拿到的 ntdll 是不会存在 wine_get_version 方法导出的。换句话说就是能调用到 wine_get_version 方法则证明跑在 Wine 环境里面

以下是我新建的 .NET 9 控制台项目代码,理论上以下代码在低至 .NET Core 3.0 里面也能跑,但我自己没试过哈

using System.Diagnostics;
using System.Runtime.InteropServices;
if (WineDetector.IsRunningOnWine())
{
Console.WriteLine($"在 Wine 里运行");
}
else
{
Console.WriteLine($"不在 Wine 里运行");
}
class WineDetector
{
public static bool IsRunningOnWine()
{
// https://gitlab.winehq.org/wine/wine/-/wikis/Developer-FAQ#how-can-i-detect-wine
// If you still really want to detect Wine, check whether ntdll exports the function wine_get_version.
try
{
var handle = NativeLibrary.Load("ntdll.dll");
if (NativeLibrary.TryGetExport(handle, "wine_get_version", out var address))
{
Debug.Assert(address != IntPtr.Zero);
return true;
}
else
{
return false;
}
}
catch
{
// 不应该发生,Windows 下 ntdll.dll 不可能不存在
return false;
}
}
}

本文代码放在 githubgitee 上,可以使用如下命令行拉取代码。我整个代码仓库比较庞大,使用以下命令行可以进行部分拉取,拉取速度比较快

先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 358c342b5c92600c96b1d974420a2212453254b6

以上使用的是国内的 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码。如果依然拉取不到代码,可以发邮件向我要代码

git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin 358c342b5c92600c96b1d974420a2212453254b6

获取代码之后,进入 Workbench/HaherefeanernawafalCochereliwemkee 文件夹,即可获取到源代码


知识共享许可协议

原文链接: http://blog.lindexi.com/post/dotnet-C-%E5%88%A4%E6%96%AD%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E5%9C%A8-Wine-%E9%87%8C%E8%BF%90%E8%A1%8C%E7%9A%84%E6%96%B9%E6%B3%95

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。 欢迎转载、使用、重新发布,但务必保留文章署名 林德熙 (包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我 联系