请问一个WMI远程执行命令,获取命令输出结果的有关问题,万分感谢
请教一个WMI远程执行命令,获取命令输出结果的问题,万分感谢!
以下代码是利用WMI在远程计算机上执行一条命令,我使用“net user”做测试,但“net user”的输出不知道怎么获取,请问各位高手,该如何处理?万分感谢!
<------------>
// Execute Method
MethodName为“net user”;
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(ClassName, MethodName, 0,
NULL, pClassInstance, &pOutParams, NULL);
if (FAILED(hres))
{ // Program has failed.
}
CComVariant varReturnValue;
hres=pOutParams->Get(_bstr_t(L"ReturnValue"),0,&varReturnValue, NULL, 0);
printf( "the return is %s\n\r ",hres);
<------------>
以下是完整代码:
#define _WIN32_DCOM
#define UNICODE
#include <iostream>
#include <fstream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <Winnetwk.h>
#pragma comment(lib, "mpr.lib")
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "credui.lib")
#pragma comment(lib, "comsuppw.lib")
//#pragma comment(lib, "comsupp.lib")
#include <wincred.h>
#include <AtlBase.h>
#include <strsafe.h>
int main()
{
char* IPStr = "192.168.0.111";
char* Userstr = "Administrator";
char* PWstr = "111111";
char RemoteIPstr[50] = "\\\\";
strcat(RemoteIPstr, IPStr);
USES_CONVERSION;
LPWSTR pszIP = A2W(RemoteIPstr);
LPWSTR pszUser = A2W(Userstr);
LPWSTR pszPW = A2W(PWstr);
HRESULT hres;
// Step 3: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 0;
}
// Step 4: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IDENTIFY, // Default 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 0; // Program has failed.
}
// Step 5: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 0; // Program has failed.
}
// Step 6: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Get the user name and password for the remote computer
CREDUI_INFO cui;
wchar_t pszName[CREDUI_MAX_USERNAME_LENGTH+1];//= pszUser;//L"Administrator";//{0};
以下代码是利用WMI在远程计算机上执行一条命令,我使用“net user”做测试,但“net user”的输出不知道怎么获取,请问各位高手,该如何处理?万分感谢!
<------------>
// Execute Method
MethodName为“net user”;
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(ClassName, MethodName, 0,
NULL, pClassInstance, &pOutParams, NULL);
if (FAILED(hres))
{ // Program has failed.
}
CComVariant varReturnValue;
hres=pOutParams->Get(_bstr_t(L"ReturnValue"),0,&varReturnValue, NULL, 0);
printf( "the return is %s\n\r ",hres);
<------------>
以下是完整代码:
#define _WIN32_DCOM
#define UNICODE
#include <iostream>
#include <fstream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <comdef.h>
#include <Wbemidl.h>
#include <Winnetwk.h>
#pragma comment(lib, "mpr.lib")
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "credui.lib")
#pragma comment(lib, "comsuppw.lib")
//#pragma comment(lib, "comsupp.lib")
#include <wincred.h>
#include <AtlBase.h>
#include <strsafe.h>
int main()
{
char* IPStr = "192.168.0.111";
char* Userstr = "Administrator";
char* PWstr = "111111";
char RemoteIPstr[50] = "\\\\";
strcat(RemoteIPstr, IPStr);
USES_CONVERSION;
LPWSTR pszIP = A2W(RemoteIPstr);
LPWSTR pszUser = A2W(Userstr);
LPWSTR pszPW = A2W(PWstr);
HRESULT hres;
// Step 3: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 0;
}
// Step 4: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IDENTIFY, // Default 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 0; // Program has failed.
}
// Step 5: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 0; // Program has failed.
}
// Step 6: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Get the user name and password for the remote computer
CREDUI_INFO cui;
wchar_t pszName[CREDUI_MAX_USERNAME_LENGTH+1];//= pszUser;//L"Administrator";//{0};