none
Proplem with c# and pnputil RRS feed

  • Question

  • Hello

    I have the problem that i cant call the pnputil function over c#.

    My code:

    static void ExecuteCommand(string command)
            {
                int exitCode;
                ProcessStartInfo processInfo;
                Process process;
    
                processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
                processInfo.CreateNoWindow = true;
                processInfo.UseShellExecute = false;
                // *** Redirect the output ***
                processInfo.RedirectStandardError = true;
                processInfo.RedirectStandardOutput = true;
    
                process = Process.Start(processInfo);
                process.WaitForExit();
    
                // *** Read the streams ***
                // Warning: This approach can lead to deadlocks, see Edit #2
                string output = process.StandardOutput.ReadToEnd();
                string error = process.StandardError.ReadToEnd();
    
                exitCode = process.ExitCode;
    
                Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
                Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
                Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
                process.Close();
            }
    ExecuteCommand("pnputil /add-driver " + inffilepath);

    Result:

    output>>(none)
    error>>Der Befehl "pnputil" ist entweder falsch geschrieben oder
    konnte nicht gefunden werden.

    ExitCode: 1

    Error in eglish: 

    The command "pnputil" is either misspelled or
    could not be found.

    I use Windows 10 and .NET Framework 4.6.1

    I i start cmd and type the command it is working.

    I dont find the problem.

    Can someone help me?

    Thanks


    • Edited by Marti95 Wednesday, February 20, 2019 1:28 PM
    Wednesday, February 20, 2019 1:28 PM

Answers

  • Try to add before :

    bool bWow64 = false;
    IsWow64Process(Process.GetCurrentProcess().Handle, out bWow64);
    if (bWow64)
    {
        IntPtr OldValue = IntPtr.Zero;
        bool bRet = Wow64DisableWow64FsRedirection(out OldValue);
    }

    with declarations :

            [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern bool IsWow64Process(IntPtr hProcess, out bool Wow64Process);
    
            [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern bool Wow64DisableWow64FsRedirection(out IntPtr OldValue);

    • Marked as answer by Marti95 Wednesday, February 20, 2019 2:59 PM
    Wednesday, February 20, 2019 2:11 PM

All replies

  • Hello,

    Try the following where WorkingDirectory is where the program pnputil resides.

    var startInfo = new ProcessStartInfo();
    
      startInfo.WorkingDirectory = // working directory
      // set additional properties 
    
    Process proc = Process.Start(startInfo);


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, February 20, 2019 1:47 PM
    Moderator
  • Try to add before :

    bool bWow64 = false;
    IsWow64Process(Process.GetCurrentProcess().Handle, out bWow64);
    if (bWow64)
    {
        IntPtr OldValue = IntPtr.Zero;
        bool bRet = Wow64DisableWow64FsRedirection(out OldValue);
    }

    with declarations :

            [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern bool IsWow64Process(IntPtr hProcess, out bool Wow64Process);
    
            [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern bool Wow64DisableWow64FsRedirection(out IntPtr OldValue);

    • Marked as answer by Marti95 Wednesday, February 20, 2019 2:59 PM
    Wednesday, February 20, 2019 2:11 PM
  • Try to add before :

    bool bWow64 = false;
    IsWow64Process(Process.GetCurrentProcess().Handle, out bWow64);
    if (bWow64)
    {
        IntPtr OldValue = IntPtr.Zero;
        bool bRet = Wow64DisableWow64FsRedirection(out OldValue);
    }

    with declarations :

            [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern bool IsWow64Process(IntPtr hProcess, out bool Wow64Process);
    
            [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            private static extern bool Wow64DisableWow64FsRedirection(out IntPtr OldValue);

    Where can i add this?
    Wednesday, February 20, 2019 2:15 PM
  • Where can i add this?

    The code before your code ExecuteCommand("pnputil /add-driver " +inffilepath);

    The declarations in your class

    with, at beginning of the program, :

    using System.Runtime.InteropServices;



    • Edited by Castorix31 Wednesday, February 20, 2019 2:20 PM
    Wednesday, February 20, 2019 2:19 PM
  • it works! Thank you so much
    Wednesday, February 20, 2019 2:59 PM