大佬們,我在用NetShareAdd共享列印機,代碼如下
wchar_t szPrinterName[64] = L"Fujitsu DPK2680,LocalsplOnly";
wchar_t szPrinterNetName[64] = L"Fujitsu DPK2680";
wchar_t szPrinterRemark[64] = L"";
SHARE_INFO_502 shareInfo;
shareInfo.shi502_type = STYPE_PRINTQ;
shareInfo.shi502_netname = szPrinterNetName;
shareInfo.shi502_permissions = ACCESS_ALL;
shareInfo.shi502_max_uses = SHI_USES_UNLIMITED;
shareInfo.shi502_current_uses = 1;
shareInfo.shi502_path = szPrinterName;
shareInfo.shi502_passwd = NULL;
shareInfo.shi502_remark = szPrinterRemark;
shareInfo.shi502_reserved = 0;
shareInfo.shi502_security_descriptor = NULL;
NetShareAdd(NULL, 502, (LPBYTE)&shareInfo, &dwErr);
列印機可以正常共享,但只有自己電腦可以訪問,其他電腦訪問會報 “操作無法完成 錯誤0x00000005 拒絕訪問”,但手動共享列印機的話其他電腦是可以訪問的,我嘗試自定義SHARE_INFO_502的shi502_security_descriptor引數,無論怎么修改,都是一樣的結果。
我再貼一下修改shi502_security_descriptor的代碼,用戶名是Everyone,有沒有大佬能告訴我是不是哪里錯了。
TCHAR RefDomain[DNLEN + 1];
DWORD cchDomain = DNLEN + 1;
SID_NAME_USE peUse;
TCHAR szUser[64] = L"Everyone";
PSID pSid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid); // 為SID分配空間
if(!LookupAccountName(NULL, // [in] 這個引數指明查找的用戶或組在哪個系統上,為NULL表示本地系統
szUser, // [in] 欲授予訪問權限的用戶或組
pSid, // [out] 存放回傳的SID值
&cbSid, // [in,out] 進去的是你設定的緩沖區長度,出來的是實際SID的長度
RefDomain, // [out] 域名
&cchDomain, // [in,out] 長度
&peUse )) // [out] 結構,用來指示用戶的型別
{
if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) // 如果緩沖區不足, try again
{
pSid = (PSID)HeapReAlloc(GetProcessHeap(), 0, pSid, cbSid);
printf("LookupAccountName() failed1\n");
}
else
{
printf("LookupAccountName() failed2\n");
return 0;
}
}
printf("LookupAccountName() success RefDomain=%ls\n", RefDomain);
printf("LookupAccountName() success peUse=%d\n", peUse);
DWORD dwAclSize = sizeof(ACL) +1 * ( sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) ) + GetLengthSid(pSid);
PACL pDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);
BOOL bRet = InitializeAcl(pDacl, dwAclSize, ACL_REVISION2);
printf("InitializeAcl() = %d\n", bRet);
bRet = AddAccessAllowedAce(pDacl,ACL_REVISION2,GENERIC_ALL,pSid);
printf("AddAccessAllowedAce() = %d\n", bRet);
if(!bRet)
{
printf("AddAccessAllowedAce() GetLastError() %d\n", GetLastError());
}
SECURITY_DESCRIPTOR sd;
bRet = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
printf("InitializeSecurityDescriptor() = %d\n", bRet);
bRet = SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE);
uj5u.com熱心網友回復:
http://blog.sina.com.cn/s/blog_963e25db01013r01.html轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/10457.html
標籤:網絡編程
上一篇:非模態對話框內繪圖問題
