VC操作IIS的问题

欢乐马_19e9 2013-09-10 03:42:39
最近需要做一个操作IIS的程序,在网了上了好久感觉资料很少。在某个网站上看到一个VBS文件。
请问大牛这个VBS怎么翻译成VC代码啊。代码如下:
  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 oBinding = oIIS.Get("BindingElement").SpawnInstance_ '
这一句不知道用VC怎么写,还请大神指点。
...全文
656 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
欢乐马_19e9 2013-09-27
  • 打赏
  • 举报
回复
这样就没有人了啊。
欢乐马_19e9 2013-09-10
  • 打赏
  • 举报
回复
引用 10 楼 zhangyihu321 的回复:
用C#写做成COM组建。。。VC++再调用
我这是用在windows server 2003上的。C#不好使啊。
zhangyihu321 2013-09-10
  • 打赏
  • 举报
回复
用C#写做成COM组建。。。VC++再调用
欢乐马_19e9 2013-09-10
  • 打赏
  • 举报
回复
人呢,在线等呢。
欢乐马_19e9 2013-09-10
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
system("xxx.vbs");
不想这样写,这样不好控制啊。
欢乐马_19e9 2013-09-10
  • 打赏
  • 举报
回复
引用 5 楼 oyljerry 的回复:
对应的就是这个 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);
可是这个“SELECT * FROM Site”应该怎么写啊? 他这里是Set oBinding = oIIS.Get("BindingElement")。 查询语句应该写:“SELECT * FROM BindingElement” ?这样啊?
赵4老师 2013-09-10
  • 打赏
  • 举报
回复
system("xxx.vbs");
oyljerry 2013-09-10
  • 打赏
  • 举报
回复
引用 3 楼 kingsollyu 的回复:
[quote=引用 2 楼 oyljerry 的回复:] WMI的方式,如果可以成功,那就应该差不多了
回版主大神,就是用WMI做的,可惜小弟不才,不知道 Set oBinding = oIIS.Get("BindingElement").SpawnInstance_ ' 这一句用VC怎么写。还请指点。[/quote] 对应的就是这个 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);
欢乐马_19e9 2013-09-10
  • 打赏
  • 举报
回复
主要就是这个oIIS.Get。知道这个就好写了。
欢乐马_19e9 2013-09-10
  • 打赏
  • 举报
回复
引用 2 楼 oyljerry 的回复:
WMI的方式,如果可以成功,那就应该差不多了
回版主大神,就是用WMI做的,可惜小弟不才,不知道 Set oBinding = oIIS.Get("BindingElement").SpawnInstance_ ' 这一句用VC怎么写。还请指点。
oyljerry 2013-09-10
  • 打赏
  • 举报
回复
WMI的方式,如果可以成功,那就应该差不多了
欢乐马_19e9 2013-09-10
  • 打赏
  • 举报
回复
另外我做了一份
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;
}

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧