安装 C# 游标安装程序 (.inf) 文件

huangapple go评论71阅读模式
英文:

C# install Cursor installer (.inf) file

问题

I have a cursor installer file (installer.inf). Is it possible to install the cursor using C#?

我有一个光标安装程序文件(installer.inf)。是否可以使用C#安装光标?

I already tried the following code but it doesn't work and nothing happens when getting executed:

我已经尝试了以下代码,但它不起作用,执行时什么也不发生:

[DllImport("Setupapi.dll", EntryPoint = "InstallHinfSection", CallingConvention = CallingConvention.StdCall)]
public static extern void InstallHinfSection([In] IntPtr hwnd, [In] IntPtr ModuleHandle, [In, MarshalAs(UnmanagedType.LPWStr)] string CmdLineBuffer, int nCmdShow);

public static void InstallCursor(string installerFilePath)
{
    InstallHinfSection(IntPtr.Zero, IntPtr.Zero, installerFilePath, 0);
}

By cursor installer, I mean the following file that is present when installing a new cursor on Windows:

通过光标安装程序,我指的是在Windows上安装新光标时存在的以下文件:

安装 C# 游标安装程序 (.inf) 文件

Here, the installer.inf file looks like this:

这里,installer.inf文件如下所示:

[Version]
signature="$CHICAGO$"

[DefaultInstall]
CopyFiles = Scheme.Cur, Scheme.Txt
AddReg    = Scheme.Reg

[DestinationDirs]
Scheme.Cur = 10,"%CUR_DIR%"
Scheme.Txt = 10,"%CUR_DIR%"

[Scheme.Reg]
HKCU,"Control Panel\Cursors\Schemes","%SCHEME_NAME%",,"%10%\%CUR_DIR%\%pointer%,%10%\%CUR_DIR%\%help%,%10%\%CUR_DIR%\%work%,%10%\%CUR_DIR%\%busy%,%10%\%CUR_DIR%\%Cross%,%10%\%CUR_DIR%\%Text%,%10%\%CUR_DIR%\%Hand%,%10%\%CUR_DIR%\%Unavailiable%,%10%\%CUR_DIR%\%Vert%,%10%\%CUR_DIR%\%Horz%,%10%\%CUR_DIR%\%Dgn1%,%10%\%CUR_DIR%\%Dgn2%,%10%\%CUR_DIR%\%move%,%10%\%CUR_DIR%\%alternate%,%10%\%CUR_DIR%\%link%"

[Scheme.Cur]
"Normal Select.cur"
"Help Select.ani"
"Work in Background.ani"
"Wait.ani"
"Precision Select.cur"
"Text Select.cur"
"Handwriting.cur"
"Unavailable.ani"
"Vertical Resize.ani"
"Horizontal Resize.ani"
"Diagonal Resize 1.ani"
"Diagonal Resize 2.ani"
"Move.ani"
"Alternate Select.cur"
"Link Select.cur"

[Strings]
CUR_DIR         = "Cursors\pkuwyc\GANT_Blue_Colorful_24x24"
SCHEME_NAME     = "Gant Blue"
pointer		= "Normal Select.cur"
help		= "Help Select.ani"
work		= "Work in Background.ani"
busy		= "Wait.ani"
cross		= "Precision Select.cur"
text		= "Text Select.cur"
hand		= "Handwriting.cur"
unavailiable	= "Unavailable.ani"
vert		= "Vertical Resize.ani"
horz		= "Horizontal Resize.ani"
dgn1		= "Diagonal Resize 1.ani"
dgn2		= "Diagonal Resize 2.ani"
move		= "Move.ani"
alternate	= "Alternate Select.cur"
link		= "Link Select.cur"

I don't understand too much of driver installers, but as it is a .inf file it should work like a driver installer right? Or could it be that Windows does some special stuff again.

我不太了解驱动程序安装程序,但由于它是一个.inf文件,它应该像驱动程序安装程序一样工作,对吗?或者是Windows又做了一些特殊的事情。

英文:

I have a cursor installer file (installer.inf). Is it possible to install the cursor using C#?

I already tried the following code but it doesn't work and nothing happens when getting executed:

[DllImport("Setupapi.dll", EntryPoint = "InstallHinfSection", CallingConvention = CallingConvention.StdCall)]
public static extern void InstallHinfSection([In] IntPtr hwnd, [In] IntPtr ModuleHandle, [In, MarshalAs(UnmanagedType.LPWStr)] string CmdLineBuffer, int nCmdShow);

public static void InstallCursor(string installerFilePath)
{
    InstallHinfSection(IntPtr.Zero, IntPtr.Zero, installerFilePath, 0);
}

By cursor installer, I mean the following file that is present when installing a new cursor on Windows:

安装 C# 游标安装程序 (.inf) 文件

Here, the installer.inf file looks like this:

[Version]
signature="$CHICAGO$"

[DefaultInstall]
CopyFiles = Scheme.Cur, Scheme.Txt
AddReg    = Scheme.Reg

[DestinationDirs]
Scheme.Cur = 10,"%CUR_DIR%"
Scheme.Txt = 10,"%CUR_DIR%"

[Scheme.Reg]
HKCU,"Control Panel\Cursors\Schemes","%SCHEME_NAME%",,"%10%\%CUR_DIR%\%pointer%,%10%\%CUR_DIR%\%help%,%10%\%CUR_DIR%\%work%,%10%\%CUR_DIR%\%busy%,%10%\%CUR_DIR%\%Cross%,%10%\%CUR_DIR%\%Text%,%10%\%CUR_DIR%\%Hand%,%10%\%CUR_DIR%\%Unavailiable%,%10%\%CUR_DIR%\%Vert%,%10%\%CUR_DIR%\%Horz%,%10%\%CUR_DIR%\%Dgn1%,%10%\%CUR_DIR%\%Dgn2%,%10%\%CUR_DIR%\%move%,%10%\%CUR_DIR%\%alternate%,%10%\%CUR_DIR%\%link%"

[Scheme.Cur]
"Normal Select.cur"
"Help Select.ani"
"Work in Background.ani"
"Wait.ani"
"Precision Select.cur"
"Text Select.cur"
"Handwriting.cur"
"Unavailable.ani"
"Vertical Resize.ani"
"Horizontal Resize.ani"
"Diagonal Resize 1.ani"
"Diagonal Resize 2.ani"
"Move.ani"
"Alternate Select.cur"
"Link Select.cur"

[Strings]
CUR_DIR         = "Cursors\pkuwyc\GANT_Blue_Colorful_24x24"
SCHEME_NAME     = "Gant Blue"
pointer		= "Normal Select.cur"
help		= "Help Select.ani"
work		= "Work in Background.ani"
busy		= "Wait.ani"
cross		= "Precision Select.cur"
text		= "Text Select.cur"
hand		= "Handwriting.cur"
unavailiable	= "Unavailable.ani"
vert		= "Vertical Resize.ani"
horz		= "Horizontal Resize.ani"
dgn1		= "Diagonal Resize 1.ani"
dgn2		= "Diagonal Resize 2.ani"
move		= "Move.ani"
alternate	= "Alternate Select.cur"
link		= "Link Select.cur"

I don't understand too much of driver installers, but as it is a .inf file it should work like a driver installer right? Or could it be that Windows does some special stuff again.

答案1

得分: 0

以下的代码成功安装了光标:

public void InstallCursor(string installerFilePath)
{
    string command = @"C:\WINDOWS\System32\rundll32.exe";
    string arguments = "setupapi,InstallHinfSection DefaultInstall 132 " + installerFilePath;

    ProcessStartInfo startInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = $"/c \"{command} {arguments}\"",
        Verb = "runas",
        UseShellExecute = true,
        CreateNoWindow = true,
        WindowStyle = ProcessWindowStyle.Hidden
    };

    Process process = new Process
    {
        StartInfo = startInfo
    };

    process.Start();
    process.WaitForExit();
}

请注意,无论您打开的 cmd 还是打开它的应用程序,都需要以管理员权限运行,以安装 .inf 文件。

英文:

The following Code did successfully install the Cursor:

public void InstallCursor(string installerFilePath)
{
    string command = @"C:\WINDOWS\System32\rundll32.exe";
    string arguments = "setupapi,InstallHinfSection DefaultInstall 132 " + installerFilePath;

    ProcessStartInfo startInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = $"/c \"{command} {arguments}\"",
        Verb = "runas",
        UseShellExecute = true,
        CreateNoWindow = true,
        WindowStyle = ProcessWindowStyle.Hidden
    };

    Process process = new Process
    {
        StartInfo = startInfo
    };

    process.Start();
    process.WaitForExit();
}

Note that either the cmd you open or your Application that opens it, need to be run with Administrator priveleges to install the .inf file

huangapple
  • 本文由 发表于 2023年6月12日 13:50:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定