我是新人,说话之前先多谢大家看我的问题
比如吧,ForTest.exe就是这么一个简单的函数,向Log.txt中写了一句话
#include <fstream>using namespace std;
int main(){
ofstream ofs("Log.txt", ios::out|ios::binary);
ofs<<"hello CSDN!"<<endl;
return 1;
}点击button1的代码
#include <windows.h>
#include <shellapi.h>
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
WinExec("ForTest\\ForTest.exe", SW_HIDE); 
ShellExecute(0,L"open",L"ForTest\\ForTest.exe",L"",L"", SW_SHOW );         string   strCmdLine="ForTest\\ForTest.exe";   
      PROCESS_INFORMATION           piProcInfo;   
      STARTUPINFO                           siStartInfo;   
    
      siStartInfo.cb                     =   sizeof(STARTUPINFO);   
      siStartInfo.lpReserved     =   NULL;   
      siStartInfo.lpReserved2   =   NULL;   
      siStartInfo.cbReserved2   =   0;   
      siStartInfo.lpDesktop       =   NULL;   
      siStartInfo.dwFlags           =   0;   
    
      CreateProcess(   
                          NULL,   
                          LPWSTR(strCmdLine.c_str()),   
                          NULL,                       //   process   security   attributes   
                          NULL,                       //   primary   thread   security   attributes   
                          0,                             //   handles   are   inherited   
                          0,                             //   creation   flags   
                          NULL,                       //   use   parent's   environment   
                          NULL,                       //   use   parent's   current   directory   
                          &siStartInfo,       //   STARTUPINFO   pointer   
                          &piProcInfo);       //   receives   PROCESS_INFORMATION   
    
      //   Wait   for   the   processs   to   finish   
      DWORD   rc   =   WaitForSingleObject(   
                          piProcInfo.hProcess,   //   process   handle   
                          INFINITE);      }WinExec那个运行没效果
ShellExecute Link的时候出错误
CreateProcess的方法是参照一个帖子贴的,运行了也没效果这个问题很简单但是我被困住了,大家帮看看

解决方案 »

  1.   

    但是调用外部的不是命令行的程序就没问题比如 WinExec("D:\\Program Files\\Foxmail\\Foxmail.exe", SW_HIDE);  
    就没问题,顺利的就打开了调用命令行的,执行就没结果:(
      

  2.   

    补充一下,确切的说是执行了,但是就是没有写到文件里去大家不信可以试一下,改成
    int main() { 
    ofstream ofs("Log.txt", ios::out ¦ios::binary); 
    ofs < <"hello CSDN!" < <endl; 
    cout<<"ok"<<endl;
    system("pause");
    return 1; 
    } 就可以看到,点button以后,输出了ok,但就是写到文件那句就是没有效果
      

  3.   

    没有回答你的题目问题,不过如果你要写log的话, 下面代码就可以了,而且线程安全:
            static void Log(string msg)
            {
                const string filename = "log.txt";
                lock (filename)
                {
                    System.IO.File.AppendAllText(filename, msg);
                }
            }
      

  4.   

    谢谢,但我的目的不是为了写log,是在调用外部console exe的时候遇到了问题ForTest只是做实验的小程序,证明确实有问题
      

  5.   

    System.Diagnostics.Process.Start("notepad", txtFile); 
      

  6.   

    大家用我那个ForTest小程序试验一下好吗,用form也行, mfc也行现在不是不会调用,而是调用外部console exe出现奇怪问题ok都输出了,就是没写到文件而且我主要目的也不是为了写log,而是发现出了问题,用这个简单的小程序试验一下,缩小解决问题的范围
      

  7.   

    说明调用外部exe本身没有问题。你看看是不是文件操作问题,比如是不是没有ofs.flush(),是不是权限问题等。
      

  8.   

    呵呵,确实是调用exe以外的问题太毛躁了,也太不相信ms了