注册 登录
编程论坛 VC++/MFC

如何解决WinExec函数不能处理超长字符串命令的问题

vfdff 发布于 2010-09-28 02:21, 670 次点击
程序代码:
#include <windows.h>
#include <iostream.h>

#include "string"
using namespace std;

std::string longcmd = "notepad ";

std::string longfilename = "zhongyundezhongyundezhongyundezhongyundezhongyundezhongyunde";

void initcmd()
{
    int i = 0;
    for (i=0;i<1000;i++)
    {
        longcmd += longfilename;

    }
    longcmd += ".txt";
}


void main(int argc,char *argv[])
{
    cout <<"Opening with WinExec\n";
    initcmd();
    if (WinExec(longcmd.c_str(),SW_SHOW) <32)
        MessageBox(NULL,"Can't WinExec",NULL,MB_OK);
}
以上程序的initcmd()中,如果修改for (i=0;i<1000;i++)为for (i=0;i<1;i++)能正常执行,但是现在由于longcmd.c_str()命令所对应的字符串超出了WinExec函数的允许范围,导致WinExec(longcmd.c_str(),SW_SHOW)执行出错,有什么办法能解决这个问题吗?或者用别的API函数可以突破这个限制呢 ?
2 回复
#2
红色警戒2010-09-28 09:06
CString str = "notepad zhongyundezhongyundezhongyundezhongyundezhongyundezhongyunde";
WinExec(str, SW_SHOW);
我这样执行没有问题
#3
vfdff2010-09-28 23:48
回复 2楼 红色警戒
恩,后来发现是notepad这个命令的问题,用ls就没有问题了
1