How can I keep open the command prompt in my windows form application in c++?

孤者浪人 提交于 2019-12-24 01:19:51

问题


I wrote a project in windows form application in C++ by Visual Studio 2010. I need to open the cmd and then type special command and run other program.

I use this function :

system("cmd.exe /c dir c:\\");

but by this function I just saw cmd for a second and then it was disappeared. then I add this line :

cin.get();

but it did not work.

also I use this function :

char program[] = "C:\Windows\System32\cmd.exe";
WinExec((LPCSTR)program, SW_SHOWMINIMIZED);

but it did not work either! Can you help me please?


回答1:


Have you tried the following?

system("cmd /k dir c:\\");

/k keeps the cmd prompt window open after the executing process has terminated.

But, to be honest, it may be better to use the Windows Terminal Services API for finer control, if you so desire. But, depending on what you want to do - that might be overkill.

And, regarding your second question: don't forget to escape your backslashes in:

const char program[] = "C:\\Windows\\System32\\cmd.exe";
WinExec((LPCSTR)program, SW_SHOWMINIMIZED);

References: https://superuser.com/questions/306167/how-to-prevent-the-command-prompt-from-closing-after-execution




回答2:


You could try

cin.ignore();

maybe also in combination with cin.get()

cin.get();
cin.ignore();

I think normally cin.get() worked fine in my programs.



来源:https://stackoverflow.com/questions/18160012/how-can-i-keep-open-the-command-prompt-in-my-windows-form-application-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!