Home > Software engineering >  NetShareAdd Shared printer problem
NetShareAdd Shared printer problem

Time:11-08

Bosses, I am using NetShareAdd Shared printer, the following code

DPK2680 would szPrinterName [64]=L "Fujitsu, LocalsplOnly";
Would be szPrinterNetName [64]=L "Fujitsu DPK2680";
Would be 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) & amp; ShareInfo, & amp; DwErr);

Printer can normal share, but only your computer can access, and other computer access will be submitted to the "operation cannot be completed error 0 x00000005 access denied", but the manual is can access other computers to share printers, I try to customize SHARE_INFO_502 shi502_security_descriptor parameters, no matter how modified, are the same as a result,

I'll post the modified shi502_security_descriptor code, user name is Everyone, have who can tell me where is wrong,

TCHAR RefDomain [DNLEN + 1];
DWORD cchDomain=DNLEN + 1;
SID_NAME_USE peUse;
TCHAR szUser [64]=L "Everyone";
PSID PSID=(PSID) HeapAlloc (GetProcessHeap (), 0, cbSid);//space for SID distribution
if(! LookupAccountName (NULL,//[in] the parameters specified for the user or group on which system, NULL said local system
SzUser,//[in] to grant access user or group
PSid,//[out] deposit return values of SID
& CbSid,///in and out in is you set the length of the buffer, it is the length of the actual SID
RefDomain,//[out] of the domain name
& CchDomain,//[in, out] length
& PeUse))//[out] structure, used to indicate the type of user
{
If (GetLastError ()==ERROR_INSUFFICIENT_BUFFER)//if the buffer is not enough, try again
{
PSid=(pSid) HeapReAlloc (GetProcessHeap (), 0, pSid, cbSid);
Printf (" LookupAccountName () failed1 \ n ");
}
The 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 (& amp; Sd, SECURITY_DESCRIPTOR_REVISION);
Printf (" InitializeSecurityDescriptor ()=% d \ n ", bRet);
BRet=SetSecurityDescriptorDacl (& amp; Sd, TRUE, pDacl, FALSE);

CodePudding user response:

http://blog.sina.com.cn/s/blog_963e25db01013r01.html

  • Related