VC操纵IIS
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;
}