蝙蝠:
START notepad.exe
使用WMI winapi查詢命令列:
HRESULT hr = 0;
hr = wbemLocator_.CoCreateInstance(CLSID_WbemLocator);
hr = wbemLocator_->ConnectServer(CComBSTR(L"ROOT\\CIMV2"), NULL, NULL, NULL, 0, NULL, NULL, &wbemServices_);
hr = ::CoSetProxyBlanket(wbemServices_, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
CComPtr<IEnumWbemClassObject> enumWbemClassObject;
hr = wbemServices_->ExecQuery(CComBSTR(L"WQL"), CComBSTR(WStringUtils::Format(L"SELECT ProcessId, CommandLine, ExecutablePath FROM Win32_Process WHERE ProcessId = %d", processId).c_str()), WBEM_FLAG_FORWARD_ONLY, NULL, &enumWbemClassObject);
CComPtr<IWbemClassObject> wbemClassObject;
ULONG count = 0;
hr = enumWbemClassObject->Next(WBEM_INFINITE, 1, &wbemClassObject, &count);
CComVariant commandLineVar;
wbemClassObject->Get(L"CommandLine", 0, &commandLineVar, 0, 0);
在commandLineVar將一個空白字符結束。
這是 procexp.exe 的螢屏截圖:

這是怎么發生的?如何避免這種情況?
uj5u.com熱心網友回復:
我可以確認這種行為,但我不知道為什么會發生這種情況。任何使用“標準”命令列引數決議的應用程式可能不會注意到。
愚蠢的解決方法:
call notepad.exe&REM if you don't mind the batch waiting
start /B cmd.exe /C call notepad.exe
uj5u.com熱心網友回復:
僅使用 WMIC.exe 即可正確回傳命令列(我將結果括在括號中以表明沒有尾隨空格字符):
批處理檔案:
Start %SystemRoot%\System32\notepad.exe
For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Process Where "CommandLine Like '%%notepad.exe%%' And Not CommandLine Like '%%[%%]notepad.exe[%%]%%'" Get CommandLine /Format:MOF 2^>NUL') Do @Echo [%%G]
輸出:
C:\Users\user1633272>Start C:\Windows\System32\notepad.exe
C:\Users\user1633272>For /F Tokens^=6^ Delims^=^" %G In ('C:\Windows\System32\wbem\WMIC.exe Process Where "CommandLine Like '%notepad.exe%' And Not CommandLine Like '%[%]notepad.exe[%]%'" Get CommandLine /Format:MOF 2^>NUL') Do @Echo [%G]
[C:\Windows\System32\notepad.exe]
即使您在start命令列中包含額外的尾隨空格,決議器也會洗掉這些空格并且不會作為命令列字串的一部分傳遞,因此它們也不會作為 CommandLine 屬性的一部分回傳。
請注意,您應該知道我的 WMIC 命令還使用 NOT LIKE 字串進行過濾,否則將回傳 WMIC 命令本身,因為它notepad.exe在其內容中還包含該字串。
因此,我假設在您的特定情況下,從 COM 物件檢索時 pType 未定義為字串,而我上面的 MOF 格式表明 COM 到 MOF 正在正確請求/翻譯它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/360782.html
