C# 为x64驱动程序创建服务位置
我有一个既有x86位驱动程序又有x64位驱动程序的驱动程序。安装程序检查是否有C# 为x64驱动程序创建服务位置,c#,service,installation,C#,Service,Installation,我有一个既有x86位驱动程序又有x64位驱动程序的驱动程序。安装程序检查是否有Is64BitOS&&Is64BitProcess。如果这是真的,那么它抓住了我的x64驱动程序,并对我做了一些奇怪的事情。它将禁用禁用WOW64FSRedirection,将x64驱动程序复制到c:\windows\system32\drivers,然后恢复WOW64FSRedirection 我觉得这是不对的。哦,在复制完上述文件后,它创建了一个内核服务。我不知道为什么安装程序会这样做,或者甚至不知道它是否应该这样
Is64BitOS&&Is64BitProcess
。如果这是真的,那么它抓住了我的x64驱动程序,并对我做了一些奇怪的事情。它将禁用禁用WOW64FSRedirection
,将x64驱动程序复制到c:\windows\system32\drivers,然后恢复WOW64FSRedirection
我觉得这是不对的。哦,在复制完上述文件后,它创建了一个内核服务。我不知道为什么安装程序会这样做,或者甚至不知道它是否应该这样做。当我在HKLM\System\CurrentControlSet\Services\driver64
查看我的注册表时(出于好奇,我在我的x64机器上安装了它们),映像路径是\??\C:\Windows\System32\drivers\driver64.sys
但是当我看HKLM\System\CurrentControlSet\Services\driver86
时,图像路径只是driver86
甚至不是一个系统扩展。。。虽然我确实看到了另一个没有的WOW64标志。。。有趣
长话短说。我不喜欢那样。如果微软决定进行重定向,那么我也确信,当我为上述驱动程序创建一个SafeFileHandle时,它会重定向到正确的驱动程序。我疯了吗
我用C#制作了安装程序。这里有一些安装代码供参考
private static void InstallDriver()
{
string name = GetCorrectDriverName();
byte[] driver = is64Bit ? Properties.Resources.inpoutx64 : Properties.Resources.inpout32;
string path = Kernel32.CopyDriverToSystem32(is64Bit, name, driver);
ServiceInstaller.InstallAndStart(name, name, path);
}
Kernel32.cs
/// <summary>
/// Copies a driver with a specific name to the System32\Driver folder
/// </summary>
/// <param name="is64Bit">Value to determine if system is 32 or 64 bit. SYSTEM not program</param>
/// <param name="driverName">the name of the driver. file extension and path will be added to this, so just the name</param>
/// <param name="driver">the driver itself. In this case it is an embedded resource.</param>
/// <returns>the full path name of the driver that was intalled.</returns>
public static string CopyDriverToSystem32(bool is64Bit, string driverName, byte[] driver)
{
bool oldValue = false;
if (is64Bit) DisableWow64FSRedirection(out oldValue);
string path = Path.Combine(Environment.SystemDirectory, "drivers");
path = Path.Combine(path, driverName + ".sys");
File.WriteAllBytes(path, driver);
if (is64Bit) RevertWow64FSRedirection(oldValue);
return path;
}
public static void InstallAndStart(string serviceName, string displayName, string fileName)
{
IntPtr scm = OpenSCManager(ScmAccessRights.AllAccess);
try
{
IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess);
if (service == IntPtr.Zero)
{
service = CreateService(scm, serviceName, displayName, ServiceAccessRights.AllAccess, SERVICE_KERNEL_DRIVER, ServiceBootFlag.AutoStart, ServiceError.Normal, fileName, null, IntPtr.Zero, null, null, null);
}
if (service == IntPtr.Zero)
throw new ApplicationException("Failed to install service.");
try
{
StartService(service);
}
finally
{
CloseServiceHandle(service);
}
}
finally
{
CloseServiceHandle(scm);
}
}
private static IntPtr OpenSCManager(ScmAccessRights rights)
{
IntPtr scm = OpenSCManager(null, null, rights);
if (scm == IntPtr.Zero)
throw new ApplicationException("Could not connect to service control manager.");
return scm;
}
安装完成后,使用它是超级简单的,这就是我希望保持的
string driverName = GetCorrectDriverName();
try
{
var driverHandle = Kernel32.CreateExclusiveRWFile(driverName);
}
catch (System.IO.FileNotFoundException)
{
InstallDriver();
}
我认为代码不应该禁用FS重定向。相反,它应该复制到
sysnative
文件夹,这是64位系统上非重定向system32
的一个特殊别名。@DarkFalcon我很乐意这样做,最后我只需要它工作。删除注册表中的条目是否与停止服务相同?不,不是。SCM将不会意识到该更改。如果它当前已加载,也不会被卸载。@DarkFalcon好的,我就是这么想的。我为服务安装程序找到的代码我相信已卸载。为什么内核驱动程序不在GUI services.msc中?有什么地方我能看到他们吗?