在ocx中创建管道失败,creatfile返回值为5,访问失败。如何解决?
我先用mfc做了一个demo创建 命名管道是可以的。 代码移植到ActiveX 里面做成ocx就出问题了。
查了一下是creatfile失败。 返回值为5.
以下是代码。有经验的朋友帮我看看。
ActiveXCtrl.cpp
HANDLE hPipe;
LPTSTR lpszPipename = _T("\\\\.\\pipe\\MyPipe");
while (1)
{
hPipe = CreateFile(
lpszPipename, // pipe name
GENERIC_READ | GENERIC_WRITE, // read and write access
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
// 查看错误返回值
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL
);
//返回值
CString ii;
ii.Format(_T("%d"),GetLastError());
AfxMessageBox(ii);
//返回值代码
MessageBox( (LPCTSTR)lpMsgBuf);
LocalFree( lpMsgBuf );
if (hPipe != INVALID_HANDLE_VALUE)
break;
// Exit if an error other than ERROR_PIPE_BUSY occurs.
if (GetLastError() != ERROR_PIPE_BUSY)
{
if (!WaitNamedPipe(lpszPipename, 20000))
{
//printf("Could not open pipe");
//system("pause");
strResult=_T("数据管道拥挤打开失败");
return strResult;
}
}
}
// The pipe connected; change to message-read mode.
dwMode = PIPE_READMODE_MESSAGE;
fSuccess = SetNamedPipeHandleState(
hPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL); // don't set maximum time
if (!fSuccess)
{
//printf("SetNamedPipeHandleState failed");
//system("pause");
strResult=_T("数据参数传输失败");
return strResult;
}