Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A specific exe file can not be called programmatically

Windows 8.1 comes with a feature called "SlideToShutdown". I am trying to call that executable file programmatically. I tried Process.Start(); in C#, Shell() in VB and (void)system(); in C.

It says error as 'C:\Windows\System32\SlideToShutdown.exe' is not recognized as an internal or external command, operable program or batch file.

But in command prompt when I execute start C:\windows\system32\slidetoshutdown.exe it works perfectly.

enter image description here

This is my C program (named a.c) to call it

#include <stdlib.h>
#include <stdio.h>
int main()
{
    (void)system("C:\\Windows\\System32\\SlideToShutDown.exe");
    return(0);
}

Please help me.

like image 866
Isham Mohamed Avatar asked Nov 12 '22 01:11

Isham Mohamed


1 Answers

You are probably using the 64-bit version of Windows. Your program is however a 32-bit process. It is subjected to file system redirection, it will actually look in the c:\windows\syswow64 directory for the program. The home directory for 32-bit executables. Where it doesn't exist.

The workaround is to use c:\windows\sysnative\slidetoshutdown.exe. The "sysnative" part of the directory name will be mapped to system32 for a 32-bit process. You should technically also lookup the home directory, it isn't necessarily c:\windows. GetWindowsDirectory() function.

If you do this in a managed project then simply change the Project + Properties, Build tab, Platform target setting. Favor AnyCPU, turn off the "Prefer 32-bit" option for VS2012 and up. Which will make your program run as a 64-bit process and thus won't get redirected. Now simply Process.Start("slidetoshutdown.exe") will work. Creating a 64-bit C program isn't hard either, just change the target platform to x64.

like image 109
Hans Passant Avatar answered Nov 14 '22 23:11

Hans Passant