简体   繁体   中英

Run a command line with c++

I have read several posts about running a command line in c++, but none of them does what I want : no display of the console in an external application when I use the dllfile.

My problem : I want to use a process that picks files from a database. For example : to get a file XYZ.xyz and copy it to the directory MyDirXYZ, I would use the command line "MyDataGenerator XYZ.xyz C:\\MyDirXYZ".

I use Visual studio Let 's take the following example to clarify the issue, I am trying to create a directory with "mkdir" in C++ , without using CreateDirectory :

First method :

std::string lDirectory("c:\\MyDummyDir")
std::string lCmd("mkdir " + lDirectory);
system((lCmd).c_str());

It will work , however it displays the console when I run it with a dllfile on Excel .

Second Method ( convertToRightFormat() convert a char to a TChar)

    std::string lDirectory("c:\\MyDummyDir")
    std::string lCmd("mkdir " + lDirectory);
    BOOL _status = TRUE;
    DWORD _reply, _code;
    STARTUPINFO _si = { sizeof(_si) };
    PROCESS_INFORMATION _pi;
    TCHAR *_cmd = NULL;
   _cmd=convertToRightFormat(lCmd.c_str())
   _status &= CreateProcess(NULL, _cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &_si, &_pi);

This method does not work(not even creating the folder) , and obviously it does not work either for the process "MyDataGenerator".

I tried to use the executable MKDIR.EXE and put it in the commandline. In that case, it works , but still does not work for MyDataGenerator.

Is there a generic to make all command lines work, without showing the console?

Thanks you.

#include <Windows.h> // FreeConsole, system

int main(int argc, char* argv[])
{   
    system("mkdir newdirectoryname");
    FreeConsole();
}

This will result in cmd popping up for a split second though. It closes after that.

If thats not acceptable just target the windows subsystem instead of the console one and make sure to not draw the window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM