WinExec()与system函数区别!

standout 2002-11-29 10:33:16
我想创建一个目录列表文件。
比如:dir >dir.txt
我想通过程序实现。如果用WinExec("dir >dir.txt",SW_HIDE);
结果不能生成dir.txt,不知为什么?
而用system("dir >dir.txt")去直接可以生成了dir.txt文件。请问各位大虾这是为什么啊。
我想用第一种方式实现它,因为它可以不显示那个dos窗口。怎么实现,或怎么用类似的不显示dos窗口的方式实现它。
分不够再给加。多谢!
...全文
1142 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuii 2002-12-24
  • 打赏
  • 举报
回复
FindFirstFile FindNextFile 难道不能完成这个任务么? 呵呵,迟早要用到的,不必偷懒阿!
gotoyangjm 2002-12-24
  • 打赏
  • 举报
回复
UP
chenybin 2002-12-24
  • 打赏
  • 举报
回复
学习中

其实SHELLEXECUTE也可以的

一共有3个函数

用途和参数不同
crystal_heart 2002-11-30
  • 打赏
  • 举报
回复
up
qing_li73 2002-11-29
  • 打赏
  • 举报
回复
so u must designate a specific command for lpCmdLine....

qing_li73 2002-11-29
  • 打赏
  • 举报
回复
WinExec
The WinExec function runs the specified application.

This function is provided for compatibility with 16-bit Windows. Win32-based applications should use the CreateProcess function.

UINT WinExec(
LPCSTR lpCmdLine, // address of command line
UINT uCmdShow // window style for new application
);

approach 2002-11-29
  • 打赏
  • 举报
回复
因此,正确读取c:的目录写法应为:
WinExec("Command.com /c dir c:\\ >dir.txt",SW_HIDE);
approach 2002-11-29
  • 打赏
  • 举报
回复
要在程式執行時啟動其他的應用程式有幾個做法,這裡先來看看最簡單使用的兩個 API 。
1: UINT WinExec ( LPCSTR lpCmdLine , // address of command line
       UINT uCmdShow   // window style for new application
      );
Header File : winbase.h
詳細說明請參考 Win32 SDK Reference



第一個參數是程式名及參數;第二個參數用來指定目的程式被執行起來後如何顯示。

EX1: WinExec("Notepad.exe c:\\autoexec.bat",SW_SHOW); 執行 notepad.exe 並正常顯示其程式視窗


EX2: WinExec("Notepad.exe",SW_SHOWMINIMIZED); 執行 notepad.exe 但最小化其程式視窗


EX3: WinExec("Command.com /c dir c:\\",SW_SHOW); 執行 dir C:\ ,完成後關閉 MSDOS 視窗


EX4: WinExec("Command.com /k dir c:\\",SW_SHOW); 執行 dir C:\ ,完成後不關閉 MSDOS 視窗


 注意:第一個參數雖然可用長檔名,但長檔名中如果有空格的話,有時候目的程式執行起來會發生錯誤,發生錯誤的原因是目的程式本身判斷輸入參數時疏忽了長檔名的關係,如果遇到這個情況時,要記得將程式名改為短檔名喔。

 

2: ShellExecute(HWND hwnd, // handle to parent window
     LPCTSTR lpOperation, // pointer to string that specifies operation to perform
     LPCTSTR lpFile, // pointer to filename or folder name string
     LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters
     LPCTSTR lpDirectory, // pointer to string that specifies default directory
     INT nShowCmd // whether file is shown when opened
     );
Head File : ShellApi.h
詳細說明請參考 Win32 SDK Reference



paul2002 2002-11-29
  • 打赏
  • 举报
回复
WinExec是一个WIN32 API,它的第一个参数必须包含一个可执行文件名,
SYSTEM是C库函数,它接受一个DOS命令。
你可以这样:
WinExec("command /C dir C:\ > dir.txt",SW_HIDE);

16,472

社区成员

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

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

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