4

'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?

Community
  • 1
  • 1
Dinesh Kumar P
  • 1,128
  • 2
  • 18
  • 32
  • _"This tool is present in 'C:\Windows\System32'"_ - is it really, or is it in the SysWOW64 redirected folder? – CodeCaster Jun 09 '15 at 12:46
  • 1
    Feels like the path is incorrect. Did you try using an absolute path to ksetup instead? – AlG Jun 09 '15 at 12:47
  • Agree with @AI G, works fine for me. – PrinceT Jun 09 '15 at 12:50
  • try to add .exe at the end of command "/c ksetup.exe" – Senad Meškin Jun 09 '15 at 12:53
  • @AlG I have added the screenshot; – Dinesh Kumar P Jun 09 '15 at 12:55
  • @SenadMeškin Tried, same result – Dinesh Kumar P Jun 09 '15 at 12:56
  • @CodeCaster, if you just try to search 'ksetup' in the windows search box, it will result with many locations; Yet I could find it clearly in C:\Windows\System32 in my machine; Other files I have no idea why they are present – Dinesh Kumar P Jun 09 '15 at 12:59
  • I would fire up a copy of Process Monitor (ProcMon) and have it running when you start this program. It will tell you the physical location it is using as the default when it tries to start ksetup. When invoking a CMD shell like this, there are some other runtime things going on that *might* affect the working directory, so ProcMon would be my suggestion for a next-best investigative step. – David W Jun 09 '15 at 13:02
  • 2
    @Dinesh I mean `ksetup.exe` is **not** present in `C:\Windows\SysWOW64\ `. If your process runs as 32 bit, it will use that folder redirected to `C:\Windows\System32`. – CodeCaster Jun 09 '15 at 13:05
  • 1
    @Senad that makes little sense. "Windows8_OS" is the volume label for the C: partition, and a permission issue would return another error. – CodeCaster Jun 09 '15 at 13:08
  • @SenadMeškin In that scenario, you would not have received the message being reported. You would have seen something more along the lines of "Access Denied." – David W Jun 09 '15 at 13:12
  • @SenadMeškin You are right; I just copied 'ksetup.exe' file to another drive and set that as Working Directory; NOW WORKING; Earlier I run the sample application with VisualStudio opened as Administrator; Still I faced same error; So I didn't mind about "Permission" issue; Does "Ksetup" wont be present in windows by default? To make use of this, only way is to use 'Ksetup.exe' file explicilty? – Dinesh Kumar P Jun 09 '15 at 13:13
  • @DineshKumarP This isn't a permissions issue, Dinesh. The resolution simply verified that you placed the target executable in a location the caller could find it. The name of your target folder is NOT "WINDOWS8_OS"; that's the *volume label* of the C:\ drive!!! – David W Jun 09 '15 at 13:23
  • Nice that you resolved. – Senad Meškin Jun 09 '15 at 13:33
  • @DavidW, It seems to be permission issue only; Please see my edited answer – Dinesh Kumar P Jun 10 '15 at 07:19
  • @Dinesh No disrespect at all, but having developed in Windows for a looong time I just recognize that a security/access issue generates a different kind of message. If Windows says it can't find a file, it can't find it. If a user is explicitly denied access, you get "Access Denied." For my own education, I'm going to explore this issue further on my own. – David W Jun 10 '15 at 11:46
  • @DineshKumarP Is your startup executable - the one firing ksetup - an AnyCPU, X64, or x86 executable? And if AnyCPU or X64, is "Prefer 32-bit enabled" checked in the Project Options? I am working some local experiments which are confirming my suspicions about this issue... – David W Jun 10 '15 at 14:03
  • ksetup.exe is a 64-bit executable. The app spawning ksetup.exe needs to be 64-bit as well; attempting to start a 64-bit process from a 32-bit process in c:\windows\system32 will redirect to the SysWow64 folder (which is why you correctly received the "file not found" error, because ksetup doesn't live there), but the dependencies for ksetup will not be correct even if copied/executed from that folder. Ensure the spawning app is a 64-bit or AnyCPU executable. – David W Jun 10 '15 at 14:38

1 Answers1

1

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.

David W
  • 10,062
  • 34
  • 60