我先用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;
}
uj5u.com熱心網友回復:
編譯環境是 vs2010 win10uj5u.com熱心網友回復:
設定需要管理員權限?uj5u.com熱心網友回復:
瀏覽器用的么?以管理員運行uj5u.com熱心網友回復:
你指的是ie用管理員權限打開? 還是ocx用管理員權限生成運行?
uj5u.com熱心網友回復:
對,在加載在html網頁中呼叫。 您說的用管理員運行是ie用管理員運行?還是ocx生成的時候。權限用管理員生成?
uj5u.com熱心網友回復:
管理員運行IE,我前段時間剛做過類似,控制元件無法生成日志,管理員運行IE后就有日志了。(WIN10uj5u.com熱心網友回復:
我也是win10 ,的確我用打開ie用管理員權限就能打開管道。 但是這樣不方便呀。 ie很少用管理員打開,要到ie程式的路徑去打開ie。 這么說來就是權限不夠, 能不能通過提高ocx權限來解決這個問題? 要如何提權? 我之前你的幾個提權方式都不行的。
uj5u.com熱心網友回復:
用代理行程打開管道。管道的DACL設定為低整合級別的行程也能訪問參考
https://blogs.msdn.microsoft.com/ieinternals/2009/11/30/understanding-the-protected-mode-elevation-dialog/
https://stackoverflow.com/questions/18982221/is-there-a-way-to-create-a-named-pipe-from-an-appcontainer-bho-on-ie11
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/68663.html
