局域网中,如何编程实现共享目录?

tifaling 2002-07-21 12:04:51
对于Win98可以用NetShareAdd,可是在Win2000下怎么样实现?
...全文
61 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
siphonelee 2002-07-21
  • 打赏
  • 举报
回复
up
Lemon_2000 2002-07-21
  • 打赏
  • 举报
回复
up
wistaria 2002-07-21
  • 打赏
  • 举报
回复
LPWSTR DirectoryToShare;
LPWSTR Sharename;
LPWSTR Username;
LPWSTR Server;
PSID pSid = NULL;
DWORD cbSid;
WCHAR RefDomain[DNLEN + 1];
DWORD cchDomain = 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
if(argc < 4)
{
printf("Usage: %ls <directory> <sharename> <user/group> [\\\\Server]\n", argv[0]);
printf(" directory is fullpath of directory to share\n");
printf(" sharename is name of share on server\n");
printf(" user/group is an WinNT user/groupname (REDMOND\\sfield, Administrators, etc)\n");
printf(" optional Server is the name of the computer to create the share on\n");
printf("\nExample: %ls c:\\public public Everyone\n", argv[0]);
printf("c:\\public shared as public granting Everyone full access\n");
printf("\nExample: %ls c:\\private cool$ REDMOND\\sfield \\\\WINBASE\n", argv[0]);
printf("c:\\private on \\\\WINBASE shared as cool$ (hidden) granting REDMOND\\sfield access\n");
return RTN_USAGE;
}
// // since the commandline was Unicode, just provide pointers to
// the relevant items //
DirectoryToShare = argv[1];
Sharename = argv[2];
Username = argv[3];
if( argc > 4 )
{
Server = argv[4];
}
else
{
Server = NULL;
// local machine
}
// // initial allocation attempt for Sid //
#define SID_SIZE 96
cbSid = SID_SIZE;
pSid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid);
if(pSid == NULL)
{
printf("HeapAlloc error!\n");
return RTN_ERROR;
}
// // get the Sid associated with the supplied user/group name
// force Unicode API since we always pass Unicode string //
if(!LookupAccountNameW(NULL, // default lookup logic
Username, // user/group of interest from commandline
pSid, // Sid buffer
&cbSid, // size of Sid
RefDomain, // Domain account found on (unused)
&cchDomain, // 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)
{
printf("HeapReAlloc error!\n");
goto cleanup;
}
cchDomain = DNLEN + 1;
if(!LookupAccountNameW( NULL, // default lookup logic
Username, // user/group of interest from commandline
pSid, // Sid buffer
&cbSid, // size of Sid
RefDomain, // Domain account found on (unused)
&cchDomain, // size of domain in chars
&peUse ))
{
printf("LookupAccountName error! (rc=%lu)\n", GetLastError());
goto cleanup;
}
}
else
{
printf("LookupAccountName error! (rc=%lu)\n", GetLastError());
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 GENERIC_ALL (Full Control) access //
if(!AddAccessAllowedAce(pDacl,ACL_REVISION,GENERIC_ALL,pSid))
goto cleanup;
if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
goto cleanup;
if(!SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE))
{
fprintf(stderr, "SetSecurityDescriptorDacl error! (rc=%lu)\n",GetLastError());
goto cleanup;
}
// // setup share info structure //
si502.shi502_netname = (LPTSTR) Sharename;
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 = (LPTSTR) DirectoryToShare;
si502.shi502_passwd = NULL;
si502.shi502_reserved = 0;
si502.shi502_security_descriptor = &sd;
nas = NetShareAdd((LPTSTR) Server, // share is on local machine
502, // info-level
(LPBYTE)&si502, // info-buffer
NULL // don't bother with parm
);
if(nas != NO_ERROR)
{
printf("NetShareAdd error! (rc=%lu)\n", nas);
goto cleanup;
}
bSuccess = TRUE;
// indicate success
cleanup:
// // free allocated resources //
if(pDacl != NULL)
HeapFree(GetProcessHeap(), 0, pDacl);
if(pSid != NULL)
HeapFree(GetProcessHeap(), 0, pSid);
if(!bSuccess)
{
return RTN_ERROR;
}
return RTN_OK;

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧