请教一个WMI远程执行命令,获取命令输出结果的问题,万分感谢!

重阳学习微博 2011-09-24 03:25:40
以下代码是利用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};
mbstowcs(pszName, Userstr, CREDUI_MAX_USERNAME_LENGTH+1);
wchar_t pszPwd[CREDUI_MAX_PASSWORD_LENGTH+1];//= L"test!@#$1234";//{0};
mbstowcs(pszPwd, PWstr, CREDUI_MAX_PASSWORD_LENGTH+1);

wchar_t pszDomain[CREDUI_MAX_USERNAME_LENGTH+1];
wchar_t pszUserName[CREDUI_MAX_USERNAME_LENGTH+1];
wchar_t pszAuthority[CREDUI_MAX_USERNAME_LENGTH+1];
BOOL fSave;
DWORD dwErr;

char Remote_WMI[MAX_PATH] = "\\\\";
strcat(Remote_WMI, IPStr);
strcat(Remote_WMI, "\\root\\cimv2");
wchar_t psz_Remote_WMI[MAX_PATH];
mbstowcs(psz_Remote_WMI, Remote_WMI, MAX_PATH);
// Connect to the remote root\cimv2 namespace
// and obtain pointer pSvc to make IWbemServices calls.
//---------------------------------------------------------

hres = pLoc->ConnectServer(
psz_Remote_WMI,
pszName, //useToken?NULL:pszName), // User name
pszPwd, //useToken?NULL:pszPwd), // User password
NULL, // Locale
NULL, // Security flags
_bstr_t(L""), //useNTLM?NULL:pszAuthority),// Authority
NULL, // Context object
&pSvc // IWbemServices proxy
);

if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 0; // Program has failed.
}

cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;


// step 7: --------------------------------------------------
// Create COAUTHIDENTITY that can be used for setting security on proxy

COAUTHIDENTITY *userAcct = NULL ;
COAUTHIDENTITY authIdent;

memset(&authIdent, 0, sizeof(COAUTHIDENTITY));
authIdent.PasswordLength = wcslen (pszPwd);
authIdent.Password = (USHORT*)pszPwd;

StringCchCopy(pszUserName, CREDUI_MAX_USERNAME_LENGTH+1, pszName);
authIdent.User = (USHORT*)pszUserName;
authIdent.UserLength = wcslen(pszUserName);

StringCchCopyN(pszDomain, CREDUI_MAX_USERNAME_LENGTH+1, pszName, 0);
authIdent.Domain = (USHORT*)pszDomain;
authIdent.DomainLength = 0;
authIdent.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;

userAcct = &authIdent;

// Step 8: --------------------------------------------------
// Set security levels on a WMI connection ------------------

hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_DEFAULT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_DEFAULT, // RPC_C_AUTHZ_xxx
COLE_DEFAULT_PRINCIPAL, // Server principal name
RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
userAcct, // 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 0; // Program has failed.
}

BSTR MethodName = SysAllocString(L"Create");
BSTR ClassName = SysAllocString(L"Win32_Process");

IWbemClassObject* pClass = NULL;
hres = pSvc->GetObject(ClassName, 0, NULL, &pClass, NULL);

if (FAILED(hres))
{
cout<<"GetObject failed. Error code = 0x"<<hex<<hres<<endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0;
}

IWbemClassObject* pInParamsDefinition = NULL;
hres = pClass->GetMethod(MethodName, 0,
&pInParamsDefinition, NULL);

IWbemClassObject* pClassInstance = NULL;
hres = pInParamsDefinition->SpawnInstance(0, &pClassInstance);

// Create the values for the in parameters
char Remote_CMD[200] = "net user";

VARIANT varCommand;
varCommand.vt = VT_BSTR;
varCommand.bstrVal = A2W(Remote_CMD);//L"cmd /c echo test>>C:\\test.bat";

// Store the value for the in parameters
hres = pClassInstance->Put(L"CommandLine", 0, &varCommand, 0);
wprintf(L"The command is: %s\n", V_BSTR(&varCommand));

// Execute Method
IWbemClassObject* pOutParams = NULL;
hres = pSvc->ExecMethod(ClassName, MethodName, 0,
NULL, pClassInstance, &pOutParams, NULL);

if (FAILED(hres))
{
cout << "Could not execute method. Error code = 0x"
<< hex << hres << endl;
VariantClear(&varCommand);
SysFreeString(ClassName);
SysFreeString(MethodName);
pClass->Release();
pInParamsDefinition->Release();
pOutParams->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0; // Program has failed.
}

CComVariant varReturnValue;
hres=pOutParams->Get(_bstr_t(L"ReturnValue"),0,&varReturnValue, NULL, 0);
printf( "the return is %s\n\r ",hres);

return 0;
}
...全文
727 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
su_guolong 2013-03-25
  • 打赏
  • 举报
回复
问下,这个问题解决了么,远程执行net user ,他的执行结果能通过pOutParams->Get(_bstr_t(L"ReturnValue"), 0,&var, NULL, 0);获取么?
wyx100 2011-09-30
  • 打赏
  • 举报
回复
取出这些输出参数的值,看看是不是有回显信息
重阳学习微博 2011-09-29
  • 打赏
  • 举报
回复
楼上麻烦说的详细一点,万分感谢!
枫桦沐阳 2011-09-29
  • 打赏
  • 举报
回复
我帮着解释一下。
就是pOutParams->GetNames看看都有哪些输出参数名。
再取出这些输出参数的值,看看是不是有回显信息。
有就是有,没有就是实现不了。
oyljerry 2011-09-24
  • 打赏
  • 举报
回复
这个取决于WMI返回值是否包含它的回显数据.
W1nds 2011-09-24
  • 打赏
  • 举报
回复
创建管道接收回显?

3,245

社区成员

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

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