在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(老妖) 信箱:[email protected]
// 為防止不負責任的轉載者遺漏作者信息,故在此插入此信息,請見諒.
// 歡迎訪問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", "", "共享目錄的注釋"));
}
//---------------------------------------------------------------------------