兄弟有编写过“模仿控制台程序”的吗?

xstarwish 2004-12-09 08:42:00
模仿出 cmd.exe 的样子,我在一个控件里输入命令,在另一个控件里显示出相应的结果

例如 dir

可以实现这种吗`?
...全文
63 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
pleasehelpme 2004-12-09
  • 打赏
  • 举报
回复
// 这个是看的别人的,显示dir用法
void CDosOutput2WinDlg::OnDir()
{
SECURITY_ATTRIBUTES sa;
HANDLE hRead,hWrite;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead,&hWrite,&sa,0)) {
MessageBox("Error On CreatePipe()");
return;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWrite;
si.hStdOutput = hWrite;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if (!CreateProcess(NULL,TEXT("C:\\WINNT\\system32\\cmd.exe /C dir.exe /?")
,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) {
MessageBox("Error on CreateProcess()");
return;
}
CloseHandle(hWrite);

char buffer[4096] = {0};
DWORD bytesRead;
while (true) {
if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
break;
m_DosOutputContent += buffer;
UpdateData(false);
Sleep(200);
}
}
pleasehelpme 2004-12-09
  • 打赏
  • 举报
回复
原来写着玩的,没有实现用cd转换目录的功能
#include <iostream>
#include <string>
#include <ctime>
#include <direct.h>
using namespace std;

void PrintTime()
{
time_t aclock;
time(&aclock);
struct tm *newtime = localtime(&aclock);
cout << "Current time is: " << newtime->tm_hour
<< ':' << newtime->tm_min
<< ':' <<newtime->tm_sec;

}

void PrintDate()
{
time_t aclock;
time(&aclock);
struct tm *newtime = localtime(&aclock);
cout << "Current date is: " << newtime->tm_year + 1900
<< '-' << newtime->tm_mon + 1
<< '-' << newtime->tm_mday;
}

void Hello()
{
cout << "Welcome to my toy shell :)" << endl;
PrintDate();
cout << endl;
PrintTime();
cout << endl;
}

char *GetWorkDir( void )
{
static char buffer[_MAX_PATH] = {0};

/* Get the current working directory: */
if (NULL == _getcwd( buffer, _MAX_PATH)) {
return "Oops, can't get current working directory";
}else {
return buffer;
}
}


int main()
{
Hello();
while (true) {
cout << "$>" << GetWorkDir() << ">";

string cmd;
getline(cin, cmd, '\n'); // Get command

if (0 == strcmp("date", cmd.c_str())) { // CMD: date
PrintDate();

}else if (0 == strcmp("time", cmd.c_str())) { // CMD: time
PrintTime();
}else if (0 == strcmp("exit", cmd.c_str())) { // CMD: exit
return 0;
}else if (strstr(cmd.c_str(), "cd") != NULL){ // CMD: cd
_chdir(GetDirFrom(cmd));
}else { // Other CMD
system(cmd.c_str());
}
cout << endl;
}
return 0;
}

shootingstars 2004-12-09
  • 打赏
  • 举报
回复
stdout重定向
http://contextfree.net/wangyg/b/tech/myide.html

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧