1

I have written a simple custom shell application for Windows 8.1 systems in WPF. It functions fine, but I need to start some of the applications in the Run section of the registry. Fine and good, however, no matter what I try, they don't launch, and I receive an error: "The system cannot find the file specified."

This is designed for 64 bit systems, so I've heard that using C:\Windows\Sysnative\ for the path rather than C:\Windows\System32\ is a fix, but it didn't work. My code is as follows:

Process processToStart = new Process 
{ 
    StartInfo = 
    { 
        FileName = @"C:\Windows\Sysnative\hkcmd.exe", 
        WorkingDirectory = @"C:\Windows\Sysnative\") 
    }
};
processToStart.Start();
Giallo
  • 785
  • 2
  • 10
  • 26

2 Answers2

4

The way I found to get this to work was to disable WOW64 file system redirection. Nothing else seemed to work.
From this link: http://tcamilli.blogspot.co.uk/2005/07/disabling-wow64-file-system.html

[DllImport("Kernel32.Dll", EntryPoint="Wow64EnableWow64FsRedirection")]
public static extern bool EnableWow64FSRedirection(bool enable);

Wow64Interop.EnableWow64FSRedirection(false)
Process processToStart = new Process 
{ 
    StartInfo = 
    { 
        FileName = @"C:\Windows\Sysnative\hkcmd.exe", 
        WorkingDirectory = @"C:\Windows\Sysnative\") 
    }
};
processToStart.Start();
Wow64Interop.EnableWow64FSRedirection(true)
Giallo
  • 785
  • 2
  • 10
  • 26
  • 1
    Note that the 'Wow64EnableWow64FsRedirection' method may not work properly when there are nested calls, so you should rely on 'Wow64DisableWow64FsRedirection' instead. Just wrote an article about how to run any app from the System32 directory with C# using this approach: https://ourcodeworld.com/articles/read/909/how-to-run-any-executable-inside-the-system32-directory-of-windows-with-c-sharp-in-winforms – Carlos Delgado Apr 16 '19 at 17:12
0

Not sure if these might be the cause of your issue but from your example posted in the question, note these few points:

  1. StartInfo.FileName should only contain the fileName, not the path.
  2. If you're trying to execute hkcmd.exe, you wrote it as hkcmnd.exe (extra N).

In the example above I believe it effectively looks like specifying the filename and workingdirectory repeatedly causes a file to not be found. See this link I used to check and this link as well

Sysnative was not present on my machine (Win 7 x64), it might be in Windows 8.

I couldn't get hkcmd.exe to execute, it threw an error you had as well, however, executing cmd.exe and notepad.exe is fine.

Sample code:

System.Diagnostics.Process processToStart = new System.Diagnostics.Process();
processToStart.StartInfo.FileName = "cmd.exe"; //or notepad.exe
processToStart.StartInfo.WorkingDirectory = @"C:\Windows\System32\";
processToStart.Start();
Community
  • 1
  • 1
matrixanomaly
  • 6,627
  • 2
  • 35
  • 58
  • Yeah, just edited out the extra n. That was a mistake, sorry about that. I just tried Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\hkcmd.exe"), which works out to C:\Windows\system32\hkcmd.exe, but when I do a file.exists on the path, it comes up negative even though the file is present in that directory. Super odd. – Giallo Mar 19 '15 at 16:45
  • @Giallo no worries. :D could you try the fix specified here? : http://stackoverflow.com/questions/29149512/process-start-for-system32-applications-the-system-cannot-find-the-file-specif/29150270#29150270 This also working for other programs other than hkcmd: System.IO.Directory.SetCurrentDirectory(@"C:\Windows\System32\"); System.Diagnostics.Process.Start("cmd.exe"); – matrixanomaly Mar 19 '15 at 16:51
  • I just tried your exact code, replacing cmd.exe with hkcmd.exe, and I still get the exception. cmd.exe does work, though. It's specifically hkcmd.exe that cannot be started for some reason. – Giallo Mar 19 '15 at 16:55
  • My suspicion (which I can't test on my set up to confirm) is that the issue is similar to what the SO link I attached in my comment. I'm not sure though. (edit: just realized I was being dumb and pasted the wrong link) this one: http://stackoverflow.com/questions/13974653/system-componentmodel-win32exception-when-starting-process-file-not-found-but – matrixanomaly Mar 19 '15 at 16:58
  • This is beyond me since I can't test much from here, good luck! Seems to be a permissions issue. An alternative would be finding a way to launch an admin hidden cmd.exe that would call and run hkcmd. I've managed to start hkcmd process (shows up on task Manager) manually through an admin cmd. – matrixanomaly Mar 19 '15 at 18:42