Main Topics
Browse All TopicsHow do i run a (dos) command from a peice of c++ code in Visual c++?
I am trying to run this command "pdftk A=test2.pdf cat A4 A7 output test.pdf" which works well from the command prompt, but I need to run this from a peice of C++ code. It must be from c++.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can use the system command (http://msdn.microsoft.com
int err = system("pdftk A=test2.pdf cat A4 A7 output test.pdf");
The combination of err (local variable receiving return value of system call) and errno (global variable defined in stdlib.h) tell you the outcome of the command.
There are several methods for launching another program from one program.
You can use _spawn(), _exec(), system(), WinExec(), ShellExecute(), ShellExecuteEx(), and CreateProcess().
The system() function is a more portable function.
WinExec(), ShellExecute(), ShellExecuteEx(), and CreateProcess() are unique to Windows programming. All four windows functions allow for launching an application in the background (window-hidden).
The WinExec() is the easiest of the four to use. It only takes two arguments, however MS recommends using the CreateProcess() instead of WinExec on all 32-bit programs.
The ShellExecute() is easier to setup then CreateProcess, and it has more options then WinExec. However, it's also a legacy function. MS recommends using ShellExecuteEx() for Win32 applications.
HINSTANCE ShellExecute(
HWND hwnd,
LPCTSTR lpVerb,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);
The ShellExecuteEx() only takes one parameter, but the parameter is a structure with many members, and it's almost as compicate as the CreateProcess() function.
BOOL ShellExecuteEx(
LPSHELLEXECUTEINFO lpExecInfo
);
The CreateProcess() function takes 10 parameters, and can be tricky to use.
int system( const char *command );
UINT WinExec(
LPCSTR lpCmdLine, // address of command line
UINT uCmdShow // window style for new application
);
BOOL CreateProcess(
LPCTSTR lpApplicationName,
// pointer to name of executable module
LPTSTR lpCommandLine, // pointer to command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // process security attributes
LPSECURITY_ATTRIBUTES lpThreadAttributes, // thread security attributes
BOOL bInheritHandles, // handle inheritance flag
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // pointer to new environment block
LPCTSTR lpCurrentDirectory, // pointer to current directory name
LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION
);
Or if you need some advanced output processing - check this samples
http://codeproject.com/sys
http://www.codeproject.com
Business Accounts
Answer for Membership
by: AlexFMPosted on 2005-01-13 at 23:29:10ID: 13042057
Use CreateProcess API:
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
CreateProcess(NULL, _T("pdftk A=test2.pdf cat A4 A7 output test.pdf",
NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);