全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2021
推到 Plurk!
推到 Facebook!

console和gui雙向溝通

尚未結案
dylan_hsieh
一般會員


發表:1
回覆:1
積分:0
註冊:2010-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-04-18 19:59:27 IP:220.133.xxx.xxx 訂閱
各位先進大家好,小弟想要利用gdb這套debugger做個小型IDE除錯環境
但由於對console和GUI之間溝通不是很熟,也是win32 api新手,希望先進們可以給小弟一些方向跟指引!!
(note:已拜讀過版上有關cmd跟console相關程式的輸出)

目前遇到的幾個問題如下:

Q1. 小弟目前已學會如何利用ShellExecute,CreateProcess背景執行cmd並讀到我要的輸出


/* example */
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShellExecute(NULL,NULL,"cmd.exe","/k gcc -g -o GDB32 D:\\GDB.cpp -L. D:\\DCT.dll 2> error.txt",NULL, SW_HIDE);
ShellExecute(NULL,NULL,"cmd.exe","/k COPY 1.rtf 2.rtf", NULL, SW_SHOW); // /k只是確認是否開啟兩個cmd
}

我第一次已用ShellExecute將cmd開啟,若想要繼續在ide上執行第二個動作COPY 1.rtf 2.rtf ,甚至更多動作!!
再用ShellExecute會變成再開啟一個cmd視窗, 如果我要在已經開啟cmd內繼續執行,
除了用system("COPY 1.rtf 2.rtf")這個function來做還有什麼方法可以一直再背景執行?

Q2. gdb也是一個command line的程式,如果我要在IDE上對它傳入指令跟得到gdb輸出, 是否用pipes就可以達到?
如果pipe可以的話,是不是全部console程式我都可以用這種方法來達到背景執行?

(小弟有參考下列幾篇,看得不是很懂也沒有很確定是否可以達到我要的功能, 且有時間上壓力很怕弄錯方向 )


http://delphi.ktop.com.tw/board.php?cid=30&fid=66&tid=47504

Creating a Child Process with Redirected Input and Output
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=23904

Using anonymous pipes to redirect standard input/output of a child process.
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=23905

編輯記錄
dylan_hsieh 重新編輯於 2010-04-18 20:00:33, 註解 無‧
dylan_hsieh
一般會員


發表:1
回覆:1
積分:0
註冊:2010-04-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-04-19 14:46:45 IP:218.210.xxx.xxx 訂閱
小弟來自問自答一下好了= =

剛好看到這位施主的問題與我Q1一樣,所以我也用pipe解決
http://delphi.ktop.com.tw/board.php?cid=168&fid=914&tid=97460

研究了stallion版主程式後自己動手做了一下
http://delphi.ktop.com.tw/board.php?cid=168&fid=914&tid=91684

程式碼與stallion版主一樣
我在Edit輸入 ipconfg
得到下列結果:

Ethernet adapter 區域連線 2:
Media State . . . . . . . . . . . : Media disconnected
Ethernet adapter 無線網路連線:
. . . . . . . . . . (略)


接著輸入
copy 1.txt 2.txt
cmd copy 1.txt 2.txt
均得到下列結果:

Bad command or filename

再輸入cmd /c copy 1.txt 2.txt =>成功複製檔案

小弟問題是如果前面不加cmd , 直接輸入copy 1.txt 2.txt就沒辦法執行
而在版主發表的CreateProcess code內也完全找不到啟動cmd.exe這個字
ret =::CreateProcess(0, sBuffer.c_str(), &sa, &sa, TRUE, NORMAL_PRIORITY_CLASS, 0, 0, &start, &proc);

於是小弟去找了CreateProcess的說明
文中有提到如果CreateProcess內第一個參數是null的話
那麼便要在第二個參數內啟動時第一個元素就是application name
也就是我在上面填的cmd

可是小弟不解的是為啥我輸入ipconfig卻又可以動作?這似乎互相矛盾~希望有經驗的先進們指點一下
而且為什麼一定要加/c才會動作?


BOOL CreateProcess(

LPCTSTR lpApplicationName, // pointer to name of executable module
LPTSTR lpCommandLine, // pointer to command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // pointer to process security attributes
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes
BOOL bInheritHandles, // handle inheritance flag
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // pointer to new environment block
LPCTSTR lpCurrentDirectory, // pointer to current directory name
LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION
);

lpCommandLine(第二個參數說明)

Pointer to a null-terminated string that specifies the command line to execute.
The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line.
If both lpApplicationName and lpCommandLine are non-NULL, *lpApplicationName specifies the module to execute, and *lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. C runtime processes can use the argc and argv arguments.

If lpApplicationName is NULL, the first white space-delimited token of the command line specifies the module name. If the filename does not contain an extension, .EXE is assumed. If the filename ends in a period (.) with no extension, or the filename contains a path, .EXE is not appended. If the filename does not contain a directory path, Windows searches for the executable file in the following sequence:

1. The directory from which the application loaded.
2. The current directory for the parent process.
3. Windows 95: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.

Windows NT: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is SYSTEM32.
4. Windows NT: The 16-bit Windows system directory. There is no Win32 function that obtains the path of this directory, but it is searched. The name of this directory is SYSTEM.
5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6. The directories that are listed in the PATH environment variable.

If the process to be created is an MS-DOS - based or Windows-based application, lpCommandLine should be a full command line in which the first element is the application name. Because this also works well for Win32-based applications, it is the most robust way to set lpCommandLine.


『補充』這篇CreateProcess也有明確指定cmd
Using anonymous pipes to redirect standard input/output of a child process.
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=23905


系統時間:2017-10-24 2:55:31
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!