3,245
社区成员
发帖
与我相关
我的任务
分享
Set oIIS = GetObject("winmgmts:root\WebAdministration")
' Create a binding for the site
' 这里不知道对应的VC代码应该怎么样
Set oBinding = oIIS.Get("BindingElement").SpawnInstance_ '
oBinding.BindingInformation = "*:80:www.newsite.com"
oBinding.Protocol = "http"
' These are the parameters we pass to the Create method
physicalPath = "C:\inetpub\wwwroot"
arrBindings = array(oBinding)
' Get the Site object definition
Set oSiteDefn = oIIS.Get("Site")
' Create site!!
oSiteDefn.Create name, arrBindings, physicalPath
WScript.Echo "Site created successfully!"
Set oIIS = GetObject("winmgmts:root\WebAdministration")
Set oSites = oIIS.InstancesOf("Site")
For Each oSite In oSites
WScript.Echo oSite.Name & " (" & oSite.Id & ")"
Next
的VC翻译。可以正确运行。
翻译内容如下:
int EnumerateAllSiteInstances()
{
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. " << "Error code = 0x" << hex << hres << endl;
return 1; // Program has failed.
}
// 设置进程安全级别
hres = CoInitializeSecurity( NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. " << "Error code = 0x" << hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
//创建一个CLSID_WbemLocator对象
IWbemLocator *pLoc = 0;
hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object. " << "Error code = 0x" << hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
IWbemServices *pSvc = 0;
//使用pLoc连接到” root\cimv2” 并把pSvc的指针也搞定了
hres = pLoc->ConnectServer(
_bstr_t(L"root\\WebAdministration"), // WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&pSvc // IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x" << hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
//已经连接到WMI了
cout << "Connected to root\\WebAdministration WMI namespace" << endl;
hres = CoSetProxyBlanket(
pSvc,
// the proxy to set
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
//这里是列出正在运行的进程的例子
//为了接收结果,你必须定义一个枚举对象
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
_bstr_t(L"WQL"),
_bstr_t(L"SELECT * FROM Site"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for processes failed. " << "Error code = 0x" << hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
else
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
unsigned int nSum = 0;
while (pEnumerator)
{
// 推出下一个对象
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
//没有东西了就跳出去吧
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hres = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << "Website Name[" << nSum ++ << "]: " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
}
}
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0;
}