Execute 'ksetup.exe' commandline command programmatically

335 views Asked by At

'Ksetup.exe' tool is used to configure a KDC server in a windows machine; Link1, Link2;

This tool is present in 'C:\Windows\System32\' location; Find the image;

This can be executed in a command prompt directly to get the result of installed KDC in the machine; (Check the image)![KDC executed]

I need to execute this command programmatically via C#; I tried using ProcessInfo class with below code; But I couldn't get the result only for this command; I mean I can able to get result of all other command I tried (like ipconfig, hostname,.)

ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c ksetup")
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true,
            WorkingDirectory = @"C:\Windows\System32"
        };
Process p = Process.Start(startInfo);
p.Start();
p.WaitForExit();

When I execute the same I get output message like this,

"'ksetup' is not recognized as an internal or external command,\r\noperable program or batch file."

What is the change I need in my code to execute the command?

Edit - With a workaround solution

This issue occurs because we try to access a file belonging to "C:\Windows\System32" directory;

Later I just copied the file and placed in "C:\Windows\ksetup.exe" location and it worked;

Other references: Why do 64-bit DLLs go to System32 and 32-bit DLLs to SysWoW64 on 64-bit Windows?

Another solution is, unchecking "Prefer 32 bit option" in build tab of project's properties window;

So my question is can't we access files/command present in "C:\Windows\System32" location programatically?

1

There are 1 answers

2
David W On BEST ANSWER

Although the OP has indicated a resolution by moving the app to a different folder, and inferred from that the issue was permissions, I would like to offer a different answer based on some specific testing of this issue.

I strongly suspect the firing executable in this case was either x86, or AnyCPU/X64 with "Prefer 32-bit code" enabled.

ksetup.exe is a 64-bit executable. If the app attempting to "spawn" ksetup is 32-bit, and tries to start an application from the c:\Windows\System32 folder, Windows will transparently try to run that application from the C:\Windows\Syswow64 folder instead. This is why the 'ksetup is not recognized...' error - the subsystem tried to locate the file in c:\Windows\SysWow64, and it wasn't there.

Moving ksetup to SysWow64 is a non-starter, because it's natively a 64-bit app, and the dependencies from that folder will not be correct to support the application.

The resolution, I respectfully suggest, is to either mark the spawning executable as AnyCPU or x64, with "Prefer 32-bit" explicitly unchecked.

I have tried the solution myself with the ksetup executable and have reproduced these results by changing the executable between 32- and 64-bits as noted. Setting the spawning executable to 64-bit or AnyCPU works.