|
※ 以下、TestShare.cpp
#include "stdafx.h"
#include "TestShare.h"
namespace TestShare2 {
static void MarshalString(String^ s, wstring& os ){
using namespace Runtime::InteropServices;
const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
static void MarshalString(wstring s, String^ %os){
os = gcnew String(s.c_str ());
}
static System::Boolean EnumShareFolder(wstring wszShareName, wstring& wszPath){
System::Boolean bRet;
PSHARE_INFO_502 BufPtr,p;
NET_API_STATUS res;
DWORD er=0,tr=0,resume=0, i;
do
{
res = NetShareEnum (NULL, 502, (LPBYTE *) &BufPtr, -1, &er, &tr, &resume);
if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA){
p=BufPtr;
bRet=false;
for(i=1;i<=er;i++){
if (p->shi502_netname == wszShareName){
wszPath.assign (p->shi502_path );
bRet=true;
break;
}
p++;
}
NetApiBufferFree(BufPtr);
}
}while (res==ERROR_MORE_DATA);
return bRet;
}
System::Boolean SharePrinter::IsSharedPrinter(String^ szPrinterName){
System::Boolean bRet;
HANDLE hPrinter;
PRINTER_DEFAULTS pd;
DWORD dwNeeded;
PRINTER_INFO_2 *pi2;
wstring wszPrinterName;
MarshalString(szPrinterName, wszPrinterName);
ZeroMemory(&pd, sizeof(PRINTER_DEFAULTS) );
pd.DesiredAccess = PRINTER_ALL_ACCESS;
if(!OpenPrinter((LPWSTR)wszPrinterName.c_str () , &hPrinter, &pd ) ){
return false;
}
if(!GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded ) ){
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER ){
ClosePrinter(hPrinter );
return false;
}
}
pi2 = (PRINTER_INFO_2 *)malloc(dwNeeded );
if(pi2 == NULL ){
ClosePrinter(hPrinter );
return false;
}
if(!GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded ) ){
free(pi2 );
ClosePrinter(hPrinter );
return FALSE;
}
if ( (pi2->Attributes & PRINTER_ATTRIBUTE_SHARED) != PRINTER_ATTRIBUTE_SHARED)
bRet = false;
else
bRet = true;
free(pi2 );
ClosePrinter(hPrinter );
return bRet;
}
System::Boolean SharePrinter::DoSharePrinterNT(String^ szPrinterName, String^ szShareName, System::Boolean bShare ){
HANDLE hPrinter;
PRINTER_DEFAULTS pd;
DWORD dwNeeded;
PRINTER_INFO_2 *pi2;
wstring wszPrinterName;
wstring wszShareName;
MarshalString(szPrinterName, wszPrinterName);
MarshalString(szShareName, wszShareName);
ZeroMemory(&pd, sizeof(PRINTER_DEFAULTS) );
pd.DesiredAccess = PRINTER_ALL_ACCESS;
if(!OpenPrinter((LPWSTR)wszPrinterName.c_str () , &hPrinter, &pd ) ){
return false;
}
if(! GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded ) ){
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER ){
ClosePrinter(hPrinter );
return false;
}
}
pi2 = (PRINTER_INFO_2 *)malloc(dwNeeded );
if(pi2 == NULL ){
ClosePrinter(hPrinter );
return false;
}
if(! GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded ) ){
free(pi2 );
ClosePrinter(hPrinter );
return false;
}
if(bShare ){
pi2->pShareName = (LPWSTR)wszShareName.c_str ();
pi2->Attributes |= PRINTER_ATTRIBUTE_SHARED;
} else {
pi2->Attributes = pi2->Attributes & (~PRINTER_ATTRIBUTE_SHARED);
}
if(! SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0 ) ){
free(pi2 );
ClosePrinter(hPrinter );
return false;
}
free(pi2 );
ClosePrinter(hPrinter );
return true;
}
System::Boolean ShareDiskFolder::DoShareDiskFolder(String^ szDiskFolderName, String^ szShareName, System::Boolean bShare){
System::Boolean bRet;
SHARE_INFO_2 share;
DWORD dwErr;
NET_API_STATUS res;
wstring wszShareName;
wstring wszFolderName;
MarshalString(szShareName, wszShareName);
if (bShare){
MarshalString(szDiskFolderName, wszFolderName);
ZeroMemory((void*)&share, sizeof(SHARE_INFO_2));
share.shi2_netname = (LPWSTR)wszShareName.c_str (); // 共有名
share.shi2_type = STYPE_DISKTREE; // ディスクドライブ
share.shi2_remark = (LPTSTR)L""; // コメント
share.shi2_permissions = 0;
share.shi2_max_uses = SHI_USES_UNLIMITED; // 無制限
share.shi2_current_uses = 0;
share.shi2_path = (LPWSTR)wszFolderName.c_str (); // フォルダのパス
share.shi2_passwd = NULL; // パスワードなし
dwErr = 0;
bRet = IsShareDiskFolder(szShareName, szDiskFolderName);
if (bRet)
res = NetShareSetInfo(NULL, (LPWSTR)wszShareName.c_str () , 2, (LPBYTE)&share, &dwErr);
else
res = NetShareAdd(NULL, 2, (LPBYTE)&share, &dwErr);
} else
res = NetShareDel(NULL, (LPWSTR)wszShareName.c_str (), 0);
if (res == NERR_Success )
bRet = true;
else
bRet = false;
return bRet;
}
System::Boolean ShareDiskFolder::IsShareDiskFolder(String^ szShareName, String^ %szDiskFolderName){
System::Boolean bRet;
NET_API_STATUS res;
SHARE_INFO_502 share;
DWORD dwType;
wstring wszShareName;
wstring wszFolderName;
MarshalString(szShareName, wszShareName);
ZeroMemory((void*)&share, sizeof(SHARE_INFO_502));
dwType = 0;
res = NetShareGetInfo(NULL, (LPWSTR)wszShareName.c_str (), 502, (LPBYTE*)&share);
if ( res == NERR_NetNameNotFound || res == ERROR_INVALID_PARAMETER )
bRet = false;
else {
bRet = EnumShareFolder(wszShareName, wszFolderName);
if (bRet)
MarshalString(wszFolderName, szDiskFolderName);
bRet = true;
}
return bRet;
}
}
|