Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
I want to execute one command using batch file but non of below code is working where Please tell me where I am going wrong,
Following is code for executing batch file,
First trial:-
C++
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( !CreateProcess( NULL,
    "cmd /C  sa.bat",
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL,
    &si,
    &pi )
    )
{
    printf( "CreateProcess failed (%d)\n", GetLastError() );
    return FALSE;
}
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return TRUE;


Second trial :-
C++
system("cmd.exe /C sa.bat");


Third Trial :-
C++
_execlpe("cmd", "/c", "sa.bat", static_cast<char*>(0));


Fourth Trial :-
C++
WinExec("sa.bat",SW_SHOWNORMAL);


Fifth Trial :-
C++
ShellExecute(GetDesktopWindow(), "sa.bat","", NULL, NULL, SW_SHOWNORMAL);


Non of above code is working for me but when open same batch file using double click it work like I expected.

Following is content of the file,
C:\\windows\\system32\\wusa /uninstall /kb:2718695 /quiet /forcerestart
Command use to uninstall internet explorer 10 an install internet explorer9.
Any help ,link ,block of code are most welcome.
Posted
Updated 18-May-13 1:28am
v2
Comments
[no name] 18-May-13 8:15am    
http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx
Coder Block 18-May-13 8:18am    
Dear ThePhatomUpvoter I already go thought its. I known how to browse MSDN.
[no name] 18-May-13 8:26am    
Then I would suggest the you read it again and actually use the function as stated in the documentation. Pay particular attention to the second parameter that you are setting to null.
enhzflep 19-May-13 5:46am    
ShellExecute(NULL, "open", "sa.bat", "", ".", SW_SHOW);
AlphaDeltaTheta 19-May-13 21:06pm    
sometimes the old ways are best... try spawn or something in process.h

1 solution

you better use a complete path in your command line. Not only for your batch but also for the working directory.

More love to the details is needed ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900