首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

怎么用代码在2K操作系统下只读共享一个文件夹

2012-02-15 
如何用代码在2K操作系统下只读共享一个文件夹?完全共享一个文件夹的方法我已经有了用代码和netshare方法都

如何用代码在2K操作系统下只读共享一个文件夹?
完全共享一个文件夹的方法我已经有了用代码和net   share方法都可以,就是找不到如何只读共享一个文件夹

[解决办法]
CopyFile 试试看
[解决办法]
You can use NetShareGetInfo/NetShareSetInfo. Call the get function, requesting a SHARE_INFO_502 structure. Change the shi502_security_descriptor to what you want it to be (you 'll have to munge the ACL), then call the Set function.
see also
http://msdn.microsoft.com/msdnmag/issues/05/03/SecurityBriefs/default.aspx
[解决办法]
#include <lm.h>

void CrnOutErrMsg(LPSTR lpMsg)
{
ShowMessage(lpMsg);
}
//---------------------------------------
bool CrnAddReadOnlyShare(LPWSTR lpwDirToShare, LPWSTR lpwShareName,
LPSTR lpwUserName, LPWSTR lpwServer = NULL);
bool CrnAddReadOnlyShare(LPWSTR lpwDirToShare, LPWSTR lpwShareName,
LPSTR lpwUserName, LPWSTR lpwServer)
{
PSID pSid = NULL;
DWORD cbSid;
WCHAR wszRefDomain[DNLEN + 1];
DWORD dwDomainLen = DNLEN + 1;
SID_NAME_USE peUse;
SECURITY_DESCRIPTOR sd;
PACL pDacl = NULL;
DWORD dwAclSize;
SHARE_INFO_502 si502;
NET_API_STATUS nas;
BOOL bSuccess = FALSE; // assume this function fails

// initial allocation attempt for Sid
#define SID_SIZE 96
cbSid = SID_SIZE;
pSid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid);
if(pSid == NULL)
{
CrnOutErrMsg( "HeapAlloc error! ");
return false;
}
//
// get the Sid associated with the supplied user/group name
// force Unicode API since we always pass Unicode string
//
if(!LookupAccountName(
NULL,// default lookup logic
(LPSTR)lpwUserName,// user/group of interest from commandline
pSid, // Sid buffer
&cbSid, // size of Sid
(LPSTR)wszRefDomain, // Domain account found on (unused)
&dwDomainLen, // size of domain in chars
&peUse
))
{
// if the buffer wasn 't large enough, try again
if(GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
pSid = (PSID)HeapReAlloc(GetProcessHeap(), 0, pSid, cbSid);
if(pSid == NULL)
{
CrnOutErrMsg( "HeapReAlloc error!\n ");
goto cleanup;
}
dwDomainLen = DNLEN + 1;
if(!LookupAccountName(
NULL, // default lookup logic
(LPSTR)lpwUserName, // user/group of interest from commandline
pSid,// Sid buffer
&cbSid, // size of Sid
(LPSTR)wszRefDomain, // Domain account found on (unused)
&dwDomainLen, // size of domain in chars
&peUse
))
{
CrnOutErrMsg( "LookupAccountName error! ");
goto cleanup;
}
}
else
{
CrnOutErrMsg( "LookupAccountName error! ");
goto cleanup;
}
}
// compute size of new acl
dwAclSize = sizeof(ACL) + 1 * ( sizeof(ACCESS_ALLOWED_ACE)
- sizeof(DWORD) ) + GetLengthSid(pSid) ;
// allocate storage for Acl
pDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);


if(pDacl == NULL) goto cleanup;
if(!InitializeAcl(pDacl, dwAclSize, ACL_REVISION))
goto cleanup;
// grant access
if(!AddAccessAllowedAce(
pDacl,
ACL_REVISION,
0xA0000000L,
pSid
)) goto cleanup;
if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
goto cleanup;
if(!SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE))
{
CrnOutErrMsg( "SetSecurityDescriptorDacl error! ");
goto cleanup;
}
// 63 63 72 75 6E 2E 63 6F 6D
// setup share info structure
si502.shi502_netname = lpwShareName;
si502.shi502_type = STYPE_DISKTREE;
si502.shi502_remark = NULL;
si502.shi502_permissions = 0;
si502.shi502_max_uses = SHI_USES_UNLIMITED;
si502.shi502_current_uses = 0;
si502.shi502_path = (LPWSTR) lpwDirToShare;
si502.shi502_passwd = NULL;
si502.shi502_reserved = 0;
si502.shi502_security_descriptor = &sd;
nas = NetShareAdd(
(LPWSTR) lpwServer, // share is on local machine
502,// info-level
(LPBYTE)&si502, // info-buffer
NULL// don 't bother with parm
);
if(nas != NO_ERROR)
{
CrnOutErrMsg( "NetShareAdd error! ");
goto cleanup;
}
bSuccess = TRUE; // indicate success
cleanup:
//
// free allocated resources
//
if(pDacl != NULL)
HeapFree(GetProcessHeap(), 0, pDacl);
if(pSid != NULL)
HeapFree(GetProcessHeap(), 0, pSid);
//
return bSuccess;
}
//---------------------------------------
// 测试:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CrnAddReadOnlyShare(L "C:\\ccrun ", L "MyShareRD ", "ccrun ");
}
[解决办法]
老妖强.

热点排行