VC 操作 IIS

VC/MFC 专栏收录该内容
5 篇文章 0 订阅

CIISAdminBase::CIISAdminBase()
{

HRESULT hRes = 0;
::CoInitialize(NULL);
hRes = CoCreateInstance(CLSID_MSAdminBase, NULL, CLSCTX_ALL, IID_IMSAdminBase, &m_pIMetabase);

if(FAILED(hRes)) 
{
  WriteLogFile("Can't create instance - CIISAdminBase create");
  m_pIMetabase = NULL;
}
}

CIISAdminBase::~CIISAdminBase()
{
// Destroy the object
if(m_pIMetabase) 
{
  ((IUnknown *) m_pIMetabase)->Release();
  m_pIMetabase = NULL;
}
::CoUninitialize();
}

//
// WWW Site operations

BOOL CIISAdminBase::StartWebSite()
{
BOOL bRet = SetServerStatus();
if(!bRet)
  WriteLogFile("Can't start web site");
return bRet;
}

BOOL CIISAdminBase::StopWebSite()
{
BOOL bRet = SetServerStatus(FALSE);
if(!bRet)
  WriteLogFile("Can't stop web site");
return bRet;
}

BOOL CIISAdminBase::AddWebSite(CString strSiteName, CString strSitePath, DWORD dwAccessFlag)
{
BOOL bRet = CreateSite(strSiteName, "/LM/W3SVC/1/ROOT", strSitePath, dwAccessFlag);
if(!bRet)
  WriteLogFile("Can't create web site: " + strSiteName);
return bRet;
}

BOOL CIISAdminBase::KillWebSite(const CString &strSiteName)
{
if(!m_pIMetabase) return FALSE;

HRESULT hRes = 0;
METADATA_HANDLE hRoot;
CIISPtr ptrIIS((IMSAdminBase *)m_pIMetabase);

    // Prepare BSTRs
CComBSTR bstrSiteName = "/1/ROOT/" + strSiteName;
CComBSTR bstrSiteRoot = "/LM/W3SVC";

// Open metabase key
    hRes = ptrIIS->OpenKey(METADATA_MASTER_ROOT_HANDLE, bstrSiteRoot, 
       METADATA_PERMISSION_WRITE, 20, &hRoot);
if(FAILED(hRes)) return FALSE;

// Delete a key
hRes = ptrIIS->DeleteKey(hRoot, bstrSiteName);
if(FAILED(hRes) && HRESULT_CODE(hRes) != ERROR_PATH_NOT_FOUND) return FALSE;

ptrIIS->CloseKey(hRoot);
return TRUE;
}


BOOL CIISAdminBase::CreateSite(CString strSiteName, CString strSiteVDir, 
          CString strSiteDir, DWORD dwAccessFlag)
{
if(!m_pIMetabase) return FALSE;

HRESULT hRes = 0;
METADATA_HANDLE hRoot;
METADATA_RECORD metaRecord;
CIISPtr ptrIIS((IMSAdminBase *)m_pIMetabase);

    // Prepare BSTRs
CComBSTR bstrSiteName = strSiteName;
CComBSTR bstrSiteVDir = strSiteVDir;
CComBSTR bstrSiteVName = "" + strSiteName;
CComBSTR bstrSiteDir = strSiteDir;
// Open metabase key
    hRes = ptrIIS->OpenKey(METADATA_MASTER_ROOT_HANDLE, bstrSiteVDir, 
       METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE, 20, &hRoot);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't open metabase key: " + strSiteVDir);
  return FALSE;
}

// Add a key
hRes = ptrIIS->AddKey(hRoot, bstrSiteVName);
if(FAILED(hRes) && HRESULT_CODE(hRes) != ERROR_ALREADY_EXISTS) 
{
  WriteLogFile("Can't add key to metabase: /" + strSiteName);
  return FALSE;
}

///
// Set Site Properties
// Key type
CComBSTR bstrKeyType("IIsWebVirtualDir");
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDDataType = STRING_METADATA;
metaRecord.dwMDIdentifier = MD_KEY_TYPE;
metaRecord.dwMDUserType = IIS_MD_UT_SERVER;
metaRecord.pbMDData = (unsigned char *)(unsigned short*)bstrKeyType;
metaRecord.dwMDDataLen = bstrKeyType.Length() * 2 + 3;
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Key type:IIsWebVirtualDir");
  return FALSE;
}

// Friendly name
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDDataType = STRING_METADATA;
metaRecord.dwMDIdentifier = MD_APP_FRIENDLY_NAME;
metaRecord.dwMDUserType = IIS_MD_UT_WAM;
metaRecord.pbMDData = (unsigned char *)(unsigned short*)bstrSiteName;
metaRecord.dwMDDataLen = bstrSiteName.Length() * 2 + 3;
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Friendly name");
  return FALSE;
}

// App Root Path
CComBSTR bstrSiteRoot = strSiteVDir + "/" + strSiteName;
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = STRING_METADATA;
metaRecord.dwMDIdentifier = MD_APP_ROOT;
metaRecord.dwMDUserType = IIS_MD_UT_FILE;
metaRecord.pbMDData = (unsigned char *)(unsigned short*)bstrSiteRoot;
metaRecord.dwMDDataLen = bstrSiteRoot.Length() * 2 + 3;
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for App Root Path");
  return FALSE;
}

// Path
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = STRING_METADATA;
metaRecord.dwMDIdentifier = MD_VR_PATH;
metaRecord.dwMDUserType = IIS_MD_UT_FILE;
metaRecord.pbMDData = (unsigned char *)(unsigned short*)bstrSiteDir;
metaRecord.dwMDDataLen = bstrSiteDir.Length() * 2 + 3;
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Path");
  return FALSE;
}

// Access Flag
DWORD dwFlag = dwAccessFlag; //MD_ACCESS_READ | MD_ACCESS_EXECUTE | MD_ACCESS_SCRIPT;
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = DWORD_METADATA;
metaRecord.dwMDIdentifier = MD_ACCESS_PERM;
metaRecord.dwMDUserType = IIS_MD_UT_FILE;
metaRecord.pbMDData = (unsigned char *)&dwFlag;
metaRecord.dwMDDataLen = sizeof(DWORD);
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Access Flag");
  return FALSE;
}

// Dir Browse Flag
dwFlag = 1073741886;
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = DWORD_METADATA;
metaRecord.dwMDIdentifier = MD_DIRECTORY_BROWSING;
metaRecord.dwMDUserType = IIS_MD_UT_FILE;
metaRecord.pbMDData = (unsigned char *)&dwFlag;
metaRecord.dwMDDataLen = sizeof(DWORD);
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Dir Browse Flag");
  return FALSE;
}

// App Isolate
dwFlag = 2;
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = DWORD_METADATA;
metaRecord.dwMDIdentifier = MD_APP_ISOLATED;
metaRecord.dwMDUserType = IIS_MD_UT_WAM;
metaRecord.pbMDData = (unsigned char *)&dwFlag;
metaRecord.dwMDDataLen = sizeof(DWORD);
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for App Isolate");
  return FALSE;
}

// Site Properties complete, close the metabase key
ptrIIS->CloseKey(hRoot);

return TRUE;
}


BOOL CIISAdminBase::SetServerStatus(BOOL bStart, BOOL bW3Site)
{
if(!m_pIMetabase) return FALSE;

HRESULT hRes = 0;
METADATA_HANDLE hRootW3;
CIISPtr ptrIIS((IMSAdminBase *)m_pIMetabase);

    // Open metabase key
CComBSTR bstrSiteName;
if(bW3Site) bstrSiteName = "/LM/W3SVC";
else bstrSiteName = "/LM/MSFTPSVC/";

hRes = ptrIIS->OpenKey(METADATA_MASTER_ROOT_HANDLE, bstrSiteName, 
       METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE, 20, &hRootW3); 
if(FAILED(hRes)) return FALSE;

// Get metabase key data
METADATA_RECORD metaRecord;
DWORD dwBuffer;

if(bStart) dwBuffer = MD_SERVER_COMMAND_START;
else dwBuffer = MD_SERVER_COMMAND_STOP;

ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDIdentifier = MD_SERVER_COMMAND;
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDUserType = IIS_MD_UT_SERVER;
metaRecord.dwMDDataType = DWORD_METADATA;
metaRecord.dwMDDataLen = sizeof(DWORD);
metaRecord.pbMDData = (unsigned char*)&dwBuffer;

hRes = ptrIIS->SetData(hRootW3, CComBSTR("/1"), &metaRecord); 

if(FAILED(hRes)) return FALSE;

ptrIIS->CloseKey(hRootW3);

return TRUE;
}

BOOL CIISAdminBase::CreateSite(CString strSiteName, CString strSiteVDir, 
          CString strSiteDir, DWORD dwAccessFlag)
{
if(!m_pIMetabase) return FALSE;

HRESULT hRes = 0;
METADATA_HANDLE hRoot;
METADATA_RECORD metaRecord;
CIISPtr ptrIIS((IMSAdminBase *)m_pIMetabase);

    // Prepare BSTRs
CComBSTR bstrSiteName = strSiteName;
CComBSTR bstrSiteVDir = strSiteVDir;
CComBSTR bstrSiteVName = "" + strSiteName;
CComBSTR bstrSiteDir = strSiteDir;
// Open metabase key
    hRes = ptrIIS->OpenKey(METADATA_MASTER_ROOT_HANDLE, bstrSiteVDir, 
       METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE, 20, &hRoot);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't open metabase key: " + strSiteVDir);
  return FALSE;
}

// Add a key
hRes = ptrIIS->AddKey(hRoot, bstrSiteVName);
if(FAILED(hRes) && HRESULT_CODE(hRes) != ERROR_ALREADY_EXISTS) 
{
  WriteLogFile("Can't add key to metabase: /" + strSiteName);
  return FALSE;
}

///
// Set Site Properties
// Key type
CComBSTR bstrKeyType("IIsWebVirtualDir");
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDDataType = STRING_METADATA;
metaRecord.dwMDIdentifier = MD_KEY_TYPE;
metaRecord.dwMDUserType = IIS_MD_UT_SERVER;
metaRecord.pbMDData = (unsigned char *)(unsigned short*)bstrKeyType;
metaRecord.dwMDDataLen = bstrKeyType.Length() * 2 + 3;
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Key type:IIsWebVirtualDir");
  return FALSE;
}

// Friendly name
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDDataType = STRING_METADATA;
metaRecord.dwMDIdentifier = MD_APP_FRIENDLY_NAME;
metaRecord.dwMDUserType = IIS_MD_UT_WAM;
metaRecord.pbMDData = (unsigned char *)(unsigned short*)bstrSiteName;
metaRecord.dwMDDataLen = bstrSiteName.Length() * 2 + 3;
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Friendly name");
  return FALSE;
}

// App Root Path
CComBSTR bstrSiteRoot = strSiteVDir + "/" + strSiteName;
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = STRING_METADATA;
metaRecord.dwMDIdentifier = MD_APP_ROOT;
metaRecord.dwMDUserType = IIS_MD_UT_FILE;
metaRecord.pbMDData = (unsigned char *)(unsigned short*)bstrSiteRoot;
metaRecord.dwMDDataLen = bstrSiteRoot.Length() * 2 + 3;
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for App Root Path");
  return FALSE;
}

// Path
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = STRING_METADATA;
metaRecord.dwMDIdentifier = MD_VR_PATH;
metaRecord.dwMDUserType = IIS_MD_UT_FILE;
metaRecord.pbMDData = (unsigned char *)(unsigned short*)bstrSiteDir;
metaRecord.dwMDDataLen = bstrSiteDir.Length() * 2 + 3;
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Path");
  return FALSE;
}

// Access Flag
DWORD dwFlag = dwAccessFlag; //MD_ACCESS_READ | MD_ACCESS_EXECUTE | MD_ACCESS_SCRIPT;
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = DWORD_METADATA;
metaRecord.dwMDIdentifier = MD_ACCESS_PERM;
metaRecord.dwMDUserType = IIS_MD_UT_FILE;
metaRecord.pbMDData = (unsigned char *)&dwFlag;
metaRecord.dwMDDataLen = sizeof(DWORD);
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Access Flag");
  return FALSE;
}

// Dir Browse Flag
dwFlag = 1073741886;
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = DWORD_METADATA;
metaRecord.dwMDIdentifier = MD_DIRECTORY_BROWSING;
metaRecord.dwMDUserType = IIS_MD_UT_FILE;
metaRecord.pbMDData = (unsigned char *)&dwFlag;
metaRecord.dwMDDataLen = sizeof(DWORD);
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for Dir Browse Flag");
  return FALSE;
}

// App Isolate
dwFlag = 2;
ZeroMemory(&metaRecord, sizeof(metaRecord));
metaRecord.dwMDAttributes = METADATA_INHERIT;
metaRecord.dwMDDataType = DWORD_METADATA;
metaRecord.dwMDIdentifier = MD_APP_ISOLATED;
metaRecord.dwMDUserType = IIS_MD_UT_WAM;
metaRecord.pbMDData = (unsigned char *)&dwFlag;
metaRecord.dwMDDataLen = sizeof(DWORD);
hRes = ptrIIS->SetData(hRoot, bstrSiteVName, &metaRecord);
if(FAILED(hRes)) 
{
  WriteLogFile("Can't set data for App Isolate");
  return FALSE;
}

// Site Properties complete, close the metabase key
ptrIIS->CloseKey(hRoot);

return TRUE;
}

//VC6.0+SP5,   SDK200210,   windows   2000   server  
  #include   <windows.h>  
  #include   <Iads.h>  
  #include   <comdef.h>  
  #include   <tchar.h>  
  #include   <stdio.h>  
  #include   <Adshlp.h>  
  #pragma   comment(lib,"ActiveDS")  
  #pragma   comment(lib,"adsiid")  
  BOOL   CreateWebServer(LPCTSTR   bindaddress,LPCTSTR   domain,LPCTSTR   DiskPath);  
  void   main()  
  {  
  CoInitialize(NULL);  
  if(TRUE==CreateWebServer(_T("192.168.0.224:80"),_T("")))  
  printf("create   site   ok\n");  
  else  
  printf("create   site   failed\n");  
  CoUninitialize();  
  }  
  BOOL   CreateWebServer(LPCTSTR   bindaddress,LPCTSTR   domain,LPCTSTR   pathname)  
  {  
  if(bindaddress==NULL||NULL==domain||NULL==pathname)  
  return   FALSE;  
  IADsContainer   *pCont=NULL;  
  IADs*   pAds=NULL;  
  IADs*   pVrAds=NULL;  
  IADsServiceOperations   *pSrvOp=NULL;  
  IDispatch   *pDisp   =   NULL;  
  IDispatch   *pVrDisp   =   NULL;  
  _bstr_t   WNumer="123";  
  _bstr_t   newBindings=_bstr_t(bindaddress)+":"+domain;  
  HRESULT   hr;  
  if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&pCont)==S_OK)  
  {        
  if(pCont->Create(L"IIsWebServer",WNumer,&pDisp)==S_OK)  
  {  
  hr=pDisp->QueryInterface(IID_IADs,   (void**)&pAds);  
  hr=pDisp->QueryInterface(IID_IADsServiceOperations,   (void**)&pSrvOp);  
  pAds->Put(L"ServerSize",_variant_t(long(1)));  
  pAds->Put(L"ServerComment",_variant_t(_bstr_t("masterz")));  
  pAds->Put(L"ServerBindings",_variant_t(newBindings));  
  pAds->SetInfo();  
   
  hr=pCont->GetObject(L"IIsWebServer",(WNumer),&pDisp);  
  if(pDisp->QueryInterface(IID_IADsContainer,(void**)&pCont)==S_OK)  
  {  
  if(pCont->Create(L"IIsWebVirtualDir",L"Root",&pVrDisp)==S_OK)  
  {  
  hr=pVrDisp->QueryInterface(IID_IADs,   (void**)&pVrAds);  
  pVrAds->Put(L"AccessRead",_variant_t(true));  
  pVrAds->Put(L"AccessWrite",_variant_t(true));  
  pVrAds->Put(L"AccessScript",_variant_t(true));  
  pVrAds->Put(L"EnableDirBrowsing",_variant_t(true));  
  pVrAds->Put(L"Path",_variant_t(pathname));  
  pVrAds->Put(L"AppRoot",_variant_t(pathname));  
  pVrAds->SetInfo();  
  pVrAds->Release();  
  pAds->Release();  
  pCont->Release();  
  }  
  hr=pSrvOp->Start();  
  hr=pSrvOp->Release();  
  }  
  }  
  }  
  return   true;  
  }  

Top

2 楼masterz()回复于 2003-02-24 16:16:53 得分 0 在Visual   C++中使用ADSI,需要#include   activeds.h,连接activeds.lib和adsiid.lib库文件,并要使用Unicode字符串。  
  这个例子是使用VC在默认Web站点建立的虚拟目录。  
  */  
  ///  
  //   CreateVirtualDirection参数说明  
  //   lpszVirtualDirName需要建立的虚拟目录的目录名字  
  //   lpszDiskPath   需要建立虚拟目录的本地磁盘目录  
  //  
   
  BOOL   CreateVirtualDirection(LPCTSTR   lpszVirtualDirName,LPCTSTR   lpszDiskPath)  
  {  
  IADsContainer*   iContainer;  
  IADs*   iAds;  
    
  if(ADsGetObject(L"IIS://localhost/w3svc",IID_IADsContainer,(void**)&iContainer)==S_OK)  
  {  
  //等到默认站点  
  iContainer->GetObject(_bstr_t("IIsWebServer"),   _bstr_t("1"),(IDispatch**)&iAds);  
  if(iAds->QueryInterface(IID_IADsContainer,(void**)&iContainer)==S_OK)  
  {  
  //得到默认站点的根目录  
  iContainer->GetObject(_bstr_t("IIsWebVirtualDir"),_bstr_t("Root"),(IDispatch**)&iAds);  
  //获得访问虚拟目录  
  if(iAds->QueryInterface(IID_IADsContainer,(void**)&iContainer)==S_OK)  
  {  
  //先删除了虚拟目录  
  iContainer->Delete(_bstr_t("IIsWebVirtualDir"),   _bstr_t(lpszVirtualDirName));  
  //建立虚拟目录  
  if(iContainer->Create(_bstr_t("IIsWebVirtualDir"),   _bstr_t(lpszVirtualDirName),(IDispatch**)&iAds)==S_OK)  
  {  
  //设置虚拟目录的属性  
  iAds->Put(_bstr_t("AccessRead"),_variant_t("True"));//注意跟VB中的设置属性比较  
  iAds->Put(_bstr_t("AccessWrite"),_variant_t("True"));  
  iAds->Put(_bstr_t("Path"),_variant_t(lpszDiskPath));  
  iAds->SetInfo();  
  iAds->Release();  
  iAds->Release();  
  iContainer->Release();  
  iContainer->Release();  
  return   TRUE;  
  }  
  else  
  {  
  iAds->Release();  
  iAds->Release();  
  iContainer->Release();  
  iContainer->Release();  
  return   FALSE;  
  }  
  }  
  else  
  {  
  iAds->Release();  
  iContainer->Release();  
  }  
  }  
  else  
  {  
  iAds->Release();  
  }  
  iContainer->Release();  
  }

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值