WinExec()与system函数区别!
我想创建一个目录列表文件。
比如:dir >dir.txt
我想通过程序实现。如果用WinExec("dir >dir.txt",SW_HIDE);
结果不能生成dir.txt,不知为什么?
而用system("dir >dir.txt")去直接可以生成了dir.txt文件。请问各位大虾这是为什么啊。
我想用第一种方式实现它,因为它可以不显示那个dos窗口。怎么实现,或怎么用类似的不显示dos窗口的方式实现它。
分不够再给加。多谢!
问题点数:100、回复次数:9Top
1 楼paul2002(Now 2006!)回复于 2002-11-29 22:48:57 得分 10
WinExec是一个WIN32 API,它的第一个参数必须包含一个可执行文件名,
SYSTEM是C库函数,它接受一个DOS命令。
你可以这样:
WinExec("command /C dir C:\ > dir.txt",SW_HIDE);Top
2 楼approach()回复于 2002-11-29 23:02:27 得分 90
要在程式執行時啟動其他的應用程式有幾個做法,這裡先來看看最簡單使用的兩個 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
Top
3 楼approach()回复于 2002-11-29 23:04:41 得分 0
因此,正确读取c:的目录写法应为:
WinExec("Command.com /c dir c:\\ >dir.txt",SW_HIDE);Top
4 楼qing_li73(Vincent Lee)回复于 2002-11-29 23:14:11 得分 0
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
);
Top
5 楼qing_li73(Vincent Lee)回复于 2002-11-29 23:16:15 得分 0
so u must designate a specific command for lpCmdLine....
Top
6 楼crystal_heart(笑看风云)回复于 2002-11-30 07:21:41 得分 0
upTop
7 楼gotoyangjm(Seven)回复于 2002-12-24 09:38:28 得分 0
UPTop
8 楼xuii(xuchi)回复于 2002-12-24 10:05:46 得分 0
FindFirstFile FindNextFile 难道不能完成这个任务么? 呵呵,迟早要用到的,不必偷懒阿!Top
9 楼chenybin(小马)回复于 2002-12-24 13:05:52 得分 0
学习中
其实SHELLEXECUTE也可以的
一共有3个函数
用途和参数不同Top




