大家看看这两段VB/VC的“匿名管道”代码...

SnowGrassland 2005-12-16 08:18:05
VB的可以正常跑:
Option Explicit

'The CreatePipe function creates an anonymous pipe,
'and returns handles to the read and write ends of the pipe.
Private Declare Function CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As Long

'Used to read the the pipe filled by the process create
'with the CretaProcessA function
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Any) As Long

'Structure used by the CreateProcessA function
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

'Structure used by the CreateProcessA function
Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

'Structure used by the CreateProcessA function
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

'This function launch the the commend and return the relative process
'into the PRECESS_INFORMATION structure
Private Declare Function CreateProcessA Lib "kernel32" ( _
ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
lpProcessAttributes As SECURITY_ATTRIBUTES, _
lpThreadAttributes As SECURITY_ATTRIBUTES, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long

'Close opened handle
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hHandle As Long) As Long

'Consts for the above functions
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1

Private Sub Command1_Click()
Dim proc As PROCESS_INFORMATION 'Process info filled by CreateProcessA
Dim ret As Long 'long variable for get the return value of the
'API functions
Dim start As STARTUPINFO 'StartUp Info passed to the CreateProceeeA
'function
Dim sa As SECURITY_ATTRIBUTES 'Security Attributes passeed to the
'CreateProcessA function
Dim hReadPipe As Long 'Read Pipe handle created by CreatePipe
Dim hWritePipe As Long 'Write Pite handle created by CreatePipe
Dim lngBytesread As Long 'Amount of byte read from the Read Pipe handle
Dim strBuff As String * 256 'String buffer reading the Pipe

'Create the Pipe
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)

If ret = 0 Then
'If an error occur during the Pipe creation exit
MsgBox "CreatePipe failed. Error"
Exit Sub
End If

'Launch the command line application
start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
'set the StdOutput and the StdError output to the same Write Pipe handle
start.hStdOutput = hWritePipe
start.hStdError = hWritePipe
'Execute the command
ret& = CreateProcessA(0&, "ipconfig.exe", sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

If ret <> 1 Then
'if the command is not found ....
MsgBox "File or command not found"
Exit Sub
End If

'Now We can ... must close the hWritePipe
ret = CloseHandle(hWritePipe)

'Read the ReadPipe handle
Do
DoEvents
ret = ReadFile(hReadPipe, strBuff, 256, lngBytesread, 0&)
Loop While ret <> 0

MsgBox strBuff
'Close the opened handles
ret = CloseHandle(proc.hProcess)
ret = CloseHandle(proc.hThread)
ret = CloseHandle(hReadPipe)
End Sub




写出VC的,报109错误(管道已关闭),faint.............................................
void CTestDlg::OnOK()
{
PROCESS_INFORMATION proc;
STARTUPINFO start;
int ret;

memset(&start,0,sizeof(start));
SECURITY_ATTRIBUTES sa;

HANDLE hReadPipe,hWritePipe;
unsigned long lngBytesread;
char strBuff[MAX_PATH]="\0";

sa.nLength = sizeof(sa);
sa.bInheritHandle = 1;
sa.lpSecurityDescriptor = 0;
ret = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);

if(ret==0)
{
AfxMessageBox("createpipe error");
return;
}

start.cb=sizeof(start);
start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
start.hStdInput = hWritePipe;
start.hStdError = hWritePipe;

ret = CreateProcess(0, "c:\\boot.exe", &sa, &sa, 1,
NORMAL_PRIORITY_CLASS, 0, 0, &start, &proc);
if(ret==0)
{
AfxMessageBox("createprocess");
return;
}
ret = CloseHandle(hWritePipe);

while(ret=ReadFile(hReadPipe, strBuff, sizeof(strBuff), &lngBytesread, 0));

AfxMessageBox(strBuff);
ret = CloseHandle(proc.hProcess);
ret = CloseHandle(proc.hThread);
ret = CloseHandle(hReadPipe);
}

请问大家,这是什么缘故......
...全文
130 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
SnowGrassland 2005-12-16
  • 打赏
  • 举报
回复
TO vcmute(横秋):
多谢啦,大哥!
我看看先.
winks 2005-12-16
  • 打赏
  • 举报
回复
mark 了
vcmute 2005-12-16
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=185751

void CDosWindowsDlg::OnButton1()
{
// TODO: Add your control notification handler code here
SECURITY_ATTRIBUTES sa;
HANDLE hRead,hWrite;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead,&hWrite,&sa,0)) {
MessageBox("Error On CreatePipe()");
return;
}

STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWrite;
si.hStdOutput = hWrite;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW ¦ STARTF_USESTDHANDLES;
if (!CreateProcess(NULL,"cmd.exe /c dir /?"
,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) {
MessageBox("Error on CreateProcess()");
return;
}
CloseHandle(hWrite);

char buffer[4096] = {0};
DWORD bytesRead;
while (true) {
if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
break;
m_Edit1 += buffer;//m_Edit1是CString
UpdateData(false);
Sleep(200);
}
}

SnowGrassland 2005-12-16
  • 打赏
  • 举报
回复
谁能给个VC写的可以跑的匿名管道的例子都行,只要不象网上"夸夸其谈"的那样,贴的代码复制下来总跑个109的错误码.
多谢!

16,551

社区成员

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

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

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