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

CreatePipe及CreateProcess 無法建立...

缺席
haman
中階會員


發表:46
回覆:137
積分:56
註冊:2005-03-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-08-24 15:25:37 IP:60.248.xxx.xxx 未訂閱

我參考

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

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

http://www.hkitn.com/article.php/2752

http://www.programmer-club.com/pc2020v5/forum/showsametitleN.asp?board_pc2020=vc&id=26050

的文章,嘗試寫一個重導console的stdout、stdin的function(在bcb及vc中)

可是在CreatePipe 時就false了,用GetLastError得到的error code 是998,

用msdn去查是

998 Invalid access to memory location. ERROR_NOACCESS

想請問名位高手,這個問題要如何去解決呢?

以下是我的code..

[code]

String cstrTmp,cstrPasswd;
char chTmp[100];

SECURITY_ATTRIBUTES Security;
PHANDLE ReadPipe,WritePipe;
STARTUPINFO start;
PROCESS_INFORMATION ProcessInfo;
char Buffer[2400];
DWORD BytesRead;
DWORD Apprunning;
int ReadBuffer = 2400;

Security.nLength = sizeof(Security);
Security.bInheritHandle = true;

bool isTest = CreatePipe(ReadPipe, WritePipe, &Security , 0);//就是這裡建不起來
if(isTest)
{
memset(&start,0,sizeof(start));
start.cb = sizeof(start);
start.hStdOutput = WritePipe;
start.hStdInput = ReadPipe;
start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
start.wShowWindow = SW_HIDE;
}
else
{ //這裡取得的errorcode是998
ReadBuffer = GetLastError();
}

cstrTmp = "E:\\VC_Source\\TEST\\TestReDirStdOut\\Debug\\jut listall";
strcpy(chTmp,cstrTmp.c_str());

isTest = CreateProcess(NULL,chTmp,&Security,&Security,true,NORMAL_PRIORITY_CLASS,
NULL,NULL,&start,&ProcessInfo);
if(isTest)
{
Apprunning = WaitForSingleObject(ProcessInfo.hProcess,5000);
}

do{
BytesRead = 0;
ReadFile(ReadPipe,(void *)Buffer,ReadBuffer,&BytesRead,NULL);
Buffer[BytesRead] = 0;
OemToChar(Buffer,Buffer);
Memo->Lines->Add(Buffer);
}while(BytesRead < ReadBuffer);

CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ReadPipe);
CloseHandle(WritePipe);

[/code]

haman
中階會員


發表:46
回覆:137
積分:56
註冊:2005-03-10

發送簡訊給我
#2 引用回覆 回覆 發表時間:2006-08-24 18:51:27 IP:60.248.xxx.xxx 未訂閱

自已問自已答...

原因出在HANDLE的宣告上

CreatePipe用的是HANDLE的位址,但我傳入的位址卻沒有先配址,

最後改為isTest = CreatePipe (&ReadPipe, &WritePipe, &Security , 2400);

且宣告為HANDLE ReadPipe, WritePipe;

就解決了^^

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