can not run sfc.exe in c++

Oct 15, 2012 at 8:57am
hi
I want to run sfc.exe command in c++.
I use this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
Run Command with user permission
 */
HINSTANCE SFCSevenRun() {
	return ShellExecute(NULL, "runas", "C:\\WINDOWS\\system32\\cmd.exe",
		"/k C:\\WINDOWS\\system32\\sfc.exe /scannow", 0, SW_SHOWNORMAL);
}
 
/*
Run Command without user permission
 */
HINSTANCE SFCXpRun() {
	return ShellExecute(NULL, "open", "C:\\WINDOWS\\system32\\cmd.exe",
		"/k C:\\WINDOWS\\system32\\sfc.exe /scannow", 0, SW_SHOWNORMAL);
}


If I run cmd in usual mode (use open instead of runas ) I get this message:
"You must be an administrator running a console session in order to use the sfc utility."
In xp it is OK, but in windows 7 I receive this message in cmd when I run as admin:
"windows resource protection could not start the repair service"
I create a batch file with these commands
1
2
3
C:\WINDOWS\system32\sfc.exe /scannow
pause
exit

and I send that into my code:
1
2
ShellExecute(NULL, "runas", "C:\\WINDOWS\\system32\\cmd.exe",
        "/k mybatchfile.bat", 0, SW_SHOWNORMAL);

I have that problem as the above.
But When I run this batch file directly by right click and choose "run as admin" or run cmd.exe as admin and write my batch file address in cmd.exe console everything is OK.
Please Help me. How can I fix this problem?
Oct 15, 2012 at 10:29am
Look at this:

http://www.codeproject.com/Articles/19165/Vista-UAC-The-Definitive-Guide

it might work for windows 7, too.

Basically it states that your program has to run as admin, then it can run another program as admin
Oct 16, 2012 at 4:48am
I try my program run as admin in the past, but it did not work;
Oct 16, 2012 at 5:03am
Both running the app as admin and using runas verb work for me in Windows 7 Ultimate, UAC default settings, Administrator account.
Oct 16, 2012 at 2:32pm
I found the answer myself:
I created a project that compiled by 32bit compiler but my windows7 platform was 64bit and this did not work.
After that, I test this project in windows7 32bit and was OK. and I created project with 64bit compiler in windows7 64bit and that was OK too.
Topic archived. No new replies allowed.