netshareadd
来源:互联网 发布:微信支付html源码 编辑:程序博客网 时间:2024/05/14 08:39
API_FUNCTION NetShareAdd(
const char FAR * pszServer,
short sLevel,
const char FAR * pbBuffer,
unsigned short cbBuffer
);
这个函数在netapi32.dll之中。
具体说明太多,自己去找MSDN,下面有个VC例子。
#define UNICODE
#include <windows.h>
#include <stdio.h>
#include <lm.h>
void wmain( int argc, TCHAR *argv[ ])
{
NET_API_STATUS res;
SHARE_INFO_2 p;
DWORD parm_err = 0;
if(argc<2)
printf("Usage: NetShareAdd server\n");
else
{
//
// Fill in the SHARE_INFO_2 structure.
//
p.shi2_netname = TEXT("TESTSHARE");
p.shi2_type = STYPE_DISKTREE; // disk drive
p.shi2_remark = TEXT("TESTSHARE to test NetShareAdd");
p.shi2_permissions = 0;
p.shi2_max_uses = 4;
p.shi2_current_uses = 0;
p.shi2_path = TEXT("C:\\");
p.shi2_passwd = NULL; // no password
//
// Call the NetShareAdd function,
// specifying level 2.
//
res=NetShareAdd(argv[1], 2, (LPBYTE) &p, &parm_err);
//
// If the call succeeds, inform the user.
//
if(res==0)
printf("Share created.\n");
// Otherwise, print an error,
// and identify the parameter in error.
//
else
printf("Error: %u\tparmerr=%u\n", res, parm_err);
}
return;
}
PSID pSid = NULL; // 定义一个指向SID结构的指针
DWORD cbSid; // 存放SID的长度 (in bytes)TCHAR RefDomain[DNLEN + 1]; // 一个缓冲区用来存放所在域的名称
DWORD cchDomain = DNLEN + 1; // 可以存入缓冲区的字符的个数 (in TCHARs)
SID_NAME_USE peUse; // 一个枚举类型,用来指出SID的类型
SECURITY_DESCRIPTOR sd; // 用来描述安全属性的结构
PACL pDacl = NULL; // 指向权限控制列表的指针
DWORD dwAclSize ; // 列表的长度;
LPTSTR Username = _T("everyone"); // 设定可以访问到此共享资源的用户或组
SHARE_INFO_502 si502; // 这是一个结构,用来描述共享资源的共享属性
NET_API_STATUS res; // 用来显示调用相关函数后的结果,指示调用是否成功
WCHAR* szShareName=L"ShareDatabase";// 显示在网络上的共享名
WCHAR* szSharePath =L"f:\\"; // 欲共享的目录
cbSid = 96; // 设定SID的长度为96
pSid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid); // 为SID分配空间
if(pSid == NULL) // 错误处理
{
AfxMessageBox(_T("HeapAlloc error!\n"));
}
// 得到可以访问此资源的用户或组的SID.
if(!LookupAccountName(NULL, // [in] 这个参数指明查找的用户或组在哪个系统上,为NULL表示本地系统
Username, // [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);
if(pSid == NULL)
{
AfxMessageBox(_T("HeapReAlloc error!\n"));
}
cchDomain = DNLEN + 1;
if(!LookupAccountName(NULL,
Username,
pSid,
&cbSid,
RefDomain,
&cchDomain,
&peUse ))
{
AfxMessageBox(_T("LookupAccountName error! (rc=%lu)\n"), GetLastError());
}
}
else
{
AfxMessageBox(_T("LookupAccountName error! (rc=%lu)\n"), GetLastError());
}
}
// 计算权限控制列表所需长度
dwAclSize = sizeof(ACL) +1 * ( sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) ) + GetLengthSid(pSid) ;
// 为pDacl分配所需的空间
pDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);
if(pDacl == NULL) return;
// 将pDacl初始化
InitializeAcl(pDacl, dwAclSize, ACL_REVISION);
// 授予GENERIC_ALL 权限授于得到的SID,并加入到列表中
AddAccessAllowedAce(pDacl,ACL_REVISION,GENERIC_ALL,pSid);
// 初始化 SECURITY_DESCRIPTOR 结构的实例sd
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION) ;
// 将pDacl加入到sd中
SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE);
// 填充SHARE_INFO_502 结构
si502.shi502_netname =(char*) szShareName; // 共享名
si502.shi502_type = STYPE_DISKTREE; // 资源类型--文件
si502.shi502_remark = NULL; // 别名
si502.shi502_max_uses = SHI_USES_UNLIMITED; // 最大连接数
si502.shi502_permissions = ACCESS_ALL; // 访问权限
si502.shi502_current_uses = 0; // 当前连接
si502.shi502_path = (char*) szSharePath; // 共享的目录
si502.shi502_passwd = NULL; // 访问密码
si502.shi502_reserved = 0; // 保留字段
si502.shi502_security_descriptor = &sd; // 安全描述符
// 将资源共享
res = NetShareAdd(
NULL, // NULL表示共享资源在本地系统上
502, // 表示共享信息描述用SHARE_INFO_502结构
(LPBYTE)&si502, // 存放SHARE_INFO_502结构的缓冲区
NULL // 存放错误信息,可以不用
);
// 根据返回信息,判断共享操作是否成功
if(res==NERR_Success)
AfxMessageBox("Share created");
else if(res==ERROR_ACCESS_DENIED)
AfxMessageBox("The user does not have access to the requested information");
else if(res==ERROR_INVALID_LEVEL)
AfxMessageBox("The value specified for the level parameter is invalid. ");
else if(res==ERROR_INVALID_NAME)
AfxMessageBox("The character or file system name is invalid.");
else if(res==ERROR_INVALID_PARAMETER)
AfxMessageBox("The specified parameter is invalid.");
else if(NERR_DuplicateShare==res)
AfxMessageBox("The share name is already in use on this server.");
else if(NERR_RedirectedPath==res)
AfxMessageBox("The operation is invalid for a redirected resource. The specified device name is assigned to a shared resource.");
else if(NERR_UnknownDevDir==res)
AfxMessageBox("The device or directory does not exist. ");
else
AfxMessageBox("dfdfd");
- netshareadd
- 关于NetShareCheck和NetShareAdd的使用方法
- zend studio 5.5 快捷键
- js数组依据下标删除元素
- sed 暂存空间详解
- jdk1.5 后 的新特性—枚举
- powerDesigner学习一
- netshareadd
- hive UDF
- 关于OleDB连接Excel的Extended Properties(扩展属性)HDR=YES; IMEX=2个人理解心得
- 二分查找
- Java - 线程基础
- StrToInteger
- SP2010开发和VS2010专家"食谱"--第六章节--Web Services和REST(3)--使用Fiddler
- 【操作系统】可变分区存储管理,模拟实现
- 黑马程序员_HTML5新特性总结