如安在Win9x/2k下成立同享目录[VC/C++编程]
本文“如安在Win9x/2k下成立同享目录[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
在Windows中成立一个同享目录,普通是利用NetShareAdd函数.但是在Win2k和Win9x下,这个函数辨别是包含在Netapi32.dll和Svrapi.dll中.
那么假如直接在程序中利用Netapi32.lib或Svrapi.lib,在差别的操作系统下运行时会发生找不到xxx函数的输出错误. 办理的办法就是通过GetProcAddress动态获得NetShareAdd.恰好ccrun在csdn答复一位网友的问题,趁便写了些代码,在这里贴出来.
注:
1. 以下代码已在Win2k ADV Server和Win9x SE中通过测试.编译时不需求包含lm.h或Svrapi.h,假如包含的话,会惹起数据范例重名的错误.
2. 请利用Borland C++ Builder编译器.假如您用VC,个体参数需求窜改
代码以下:
typedef DWORD (WINAPI *NETSHAREADD2K)(LPWSTR, DWORD, LPBYTE, LPDWORD);
typedef DWORD (WINAPI *NETSHAREADD9X)(const char FAR*, short, const char FAR*, unsigned short);
NETSHAREADD2K NetShareAdd2k;
NETSHAREADD9X NetShareAdd9x;
bool bWinNT; // 操作系统标志
HINSTANCE hDll; // DLL文件句柄
#define NNLEN 80 // 同享名的长度
#define LM20_NNLEN 12 // LM 2.0 同享名的长度
#define SHPWLEN 8 // 同享密码的长度
#define STYPE_DISKTREE 0 // 磁盘同享
#define SHI50F_FULL 0x0002
#define NERR_Success 0 // 操作成功
#define NERR_BASE 2100
#define NERR_DuplicateShare (NERR_BASE+18) // 同享名称已经存在
#define NERR_RedirectedPath (NERR_BASE+17) // 重定向资源的操作不法
#define NERR_UnknownDevDir (NERR_BASE+16) // 设备或目录不存在
typedef struct _share_info_50 // 用于Win9x
{
char shi50_netname[LM20_NNLEN+1]; // 同享名
unsigned char shi50_type; //
unsigned short shi50_flags; //
char FAR * shi50_remark; // ANSI 备注
char FAR * shi50_path; // 同享的资源,也就是同享的目录
char shi50_rw_password[SHPWLEN+1]; // 可读写的密码
char shi50_ro_password[SHPWLEN+1]; // 只读密码
}share_info_50;
typedef struct _SHARE_INFO_2 // 用于Win2k
{
LPWSTR shi2_netname;
DWORD shi2_type;
LPWSTR shi2_remark;
DWORD shi2_permissions;
DWORD shi2_max_uses;
DWORD shi2_current_uses;
LPWSTR shi2_path;
LPWSTR shi2_passwd;
}SHARE_INFO_2, *PSHARE_INFO_2, *LPSHARE_INFO_2;
//---------------------------------------------------------------------------
bool __fastcall InitApi() // 按照操作系统的差别,从Dll中获得API
{
// 判断系统平台
OSVERSIONINFO info;
info.dwOSVersionInfoSize = sizeof(info);
GetVersionEx(&info);
bWinNT = (info.dwPlatformId == VER_PLATFORM_WIN32_NT)? true: false;
if(bWinNT)
{
hDll = LoadLibrary("Netapi32.dll"); // win2k
if(hDll)
{
NetShareAdd2k = (NETSHAREADD2K)GetProcAddress(hDll, "NetShareAdd");
return (NetShareAdd2k != NULL);
}
else
return false;
}
else
{
hDll = LoadLibrary("Svrapi.dll"); // win9x
if(hDll)
{
NetShareAdd9x = (NETSHAREADD9X)GetProcAddress(hDll, "NetShareAdd");
return NetShareAdd9x != NULL;
}
else
return false;
}
}
//---------------------------------------------------------------------------
String __fastcall MyPathToNetShare(
String strPath, // 同享目录
String strNetName, // 同享名
String strPassword, // 密码
String strRemark) // 同享目录的注释
{
if(!InitApi())
return "函数获得错误!";
// 文档标题:如安在Win9x/2k下成立同享目录
// 作者:ccrun(老妖) 信箱:info@ccrun.com
// 为避免不负责任的转载者遗漏作者信息,故在此插入此信息,请包涵.
// 欢送拜候C++Builder 研究 http://www.ccrun.com
if(!bWinNT) // 操作系统是win9x
{
char FAR *pszServerName = NULL;
short nLevel = 50;
struct share_info_50* pBuf = NULL;
unsigned short cbBuffer;
DWORD nStatus;
cbBuffer = sizeof(struct share_info_50);
pBuf = (share_info_50 *)malloc(cbBuffer);
if(pBuf == NULL)
return "内存分配失利!";
// Assign values to the share_info_50 structure.
strcpy(pBuf->shi50_netname, strNetName.c_str());
pBuf->shi50_type = STYPE_DISKTREE;
pBuf->shi50_flags = SHI50F_FULL;
pBuf->shi50_remark = strRemark.c_str();
pBuf->shi50_path = strPath.c_str();
strcpy(pBuf->shi50_rw_password, strPassword.c_str()); // No password
strcpy(pBuf->shi50_ro_password, strPassword.c_str()); // No password
// Call the NetShareAdd function specifying information level 50.
nStatus = NetShareAdd9x(pszServerName,
nLevel,
(char FAR *)pBuf,
cbBuffer);
// 释放分配的内存
if(pBuf != NULL)
free(pBuf);
//
FreeLibrary(hDll);
if(nStatus == NERR_Success)
return("增添成功!");
else
return String("有错误发生,返回码: " + String(nStatus));
}
else // 操作系统是Win2k
{
HANDLE hToken;
TOKEN_PRIVILEGES tk;
// 首先晋升本进程的调试级别
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tk.Privileges[0].Luid);
tk.PrivilegeCount = 1;
tk.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tk, 0, (PTOKEN_PRIVILEGES)NULL, 0);
DWORD nas;
SHARE_INFO_2 p;
DWORD dwParamErr = NULL;
p.shi2_path = StringToOleStr(strPath);
p.shi2_remark = StringToOleStr(strRemark);
p.shi2_netname = StringToOleStr(strNetName);
p.shi2_passwd = StringToOleStr(strPassword);
p.shi2_type = STYPE_DISKTREE; // disk drive
p.shi2_permissions = 0;
p.shi2_max_uses = -1;
p.shi2_current_uses = 0;
nas = NetShareAdd2k(NULL, 2, (LPBYTE)&p, &dwParamErr);
FreeLibrary(hDll);
switch(nas)
{
case NERR_Success: return "设置成功!";
case ERROR_ACCESS_DENIED: return "回绝拜候!";
case ERROR_INVALID_LEVEL: return "不法的级别!";
case ERROR_INVALID_NAME: return "不法的名称!";
case ERROR_INVALID_PARAMETER: return "参数不法!";
case NERR_DuplicateShare: return "同享重名!";
case NERR_RedirectedPath: return "重定向途径!";
case NERR_UnknownDevDir: return "未知的驱动途径!";
default: return "未知错误!";
}
}
}
// 调用举例
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage(MyPathToNetShare("C:\\ccrun\\123", "MyShare", "", "同享目录的注释"));
}
//---------------------------------------------------------------------------
以上是“如安在Win9x/2k下成立同享目录[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |