[問題] CreatePipe & 程式間傳送訊息的問題
Q: 我想自己寫一個程式 A, 用 CreateProcess 開啟 telnet
然後想用 CreatePipe建立管線 使得 A <-> telnet 可以溝通
目前我已經可以成功建立 telnet
CreatePipe也沒有出現 Error
但是我用 WriteFile卻沒辦法跟 telnet溝通
(我用來登入 ptt, 但是沒有出現任何反應)
所以我先建立 cmd.exe來測試
然後用 WriteFile出來之後 cmd.exe沒有如我預期般的出現 test字眼...
/* ================== 我的原始碼 ======================= */
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int main( int argc, TCHAR *argv[] )
{
int len;
HANDLE telnet_input, telnt_output;
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( !CreateProcess( NULL, // No module name (use command line)
"cmd.exe", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return 1;
}
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if( !CreatePipe( &pi.hProcess, &telnet_input, &sa, 0 ) )
{
printf("Create pipe failed (%d).\n", GetLastError() );
return 1;
}
WriteFile( telnet_input, "test", 4, &len, NULL );
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
CloseHandle( telnet_input );
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.112.45.253
推
10/17 22:08, , 1F
10/17 22:08, 1F
→
10/17 22:09, , 2F
10/17 22:09, 2F
→
10/17 22:10, , 3F
10/17 22:10, 3F
→
10/17 22:11, , 4F
10/17 22:11, 4F
→
10/17 22:11, , 5F
10/17 22:11, 5F
→
10/17 22:11, , 6F
10/17 22:11, 6F
推
10/18 00:02, , 7F
10/18 00:02, 7F
→
10/18 00:33, , 8F
10/18 00:33, 8F
→
10/18 00:33, , 9F
10/18 00:33, 9F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章