How to run external executable (.exe ) in C++
I'm not a developper. We want ( me and our team)to write a tool 开发者_C百科in C++ that can print a PDF ( Send a PDF to printer). We tried to use external tool, like a command line pdfprint.exe, using CreateProcessW and cmd /c, but we have a problem. With Windows XP we don't have problem with our tool, with Windows Vista we have problem and we cannot using the tool like pdfprint.exe.
There are differences between Windows XP and Vista to launch external tools within another program with CreateProcessW and "cmd /c"?
Thanks
Oronzo
There are many approaches to run external application from your C++ programm. I will count them below.
- Win32API CreateProcess function. Cédric Julien gave example of use.
- exec* C/C++ function family. They are defined in POSIX standard. So they are the same on Linux. But because they are deprecated in new version of MSVC, try to use analogical _exec* function.
- WiNT Native API Call - NtCreateProcess. It is called in CreateProcess also :-) And this function represents deeper level of API. With use of Native API calls you can write small and simple applications, because you will make lesser number of dependencies of your program with external libraries. But there are issues: native API is binded to OS version, so there aren't any garanties that Native API will be the same in next OS versions.
You may try each. Please, note that it is good idea to point in each call to these functions full path to executable file. Also you must have read/execute rights (permissions) on pdfprint.exe. If you don't have such only way to execute external tool is to run your C++ program with administrative rights. You can run it such way if you right click on exe-file of your program and select "Run As Administrator" menu element.
As described here, you should use something like this :
LPTSTR szCmdline = _tcsdup(TEXT("C:\\Program Files\\....\\pdfprint.exe -args-you-need"));
CreateProcess(NULL, szCmdline, /* ... */);
精彩评论