0

Trying to convert the network drive or mapped drive(Z:) to its UNC path( 192.168.x.x\Shared). Tried different ways like WNetGetUniversalName and WNetGetConnection, but getting error like ERROR_NOT_CONNECTED or ERROR_CONNECTION_UNAVAIL. then elevated its privileges and also tried with api LogonUser. but still getting same errors. Then tried using WMI query, it works fine when running in console mode. But it is not working in service mode. Even Function to convert UNC path from the network drive is not directly called under service running process. Suppose Service process name is "parentService.exe", it creates a child called "childService.exe". this "childService.exe" is running function of converting network drive to UNC path, this means "childService.exe" is actually a console application running without standard I/O's. this same "childService.exe" works fine if its run as individually in console mode. variable "vRet.bstrVal"(provider name) contains unc path. it doesn't give any error for service mode running, it just returns successfully .but while loop condition is not met. Both console and service runs on same user.

How to make it work also when it runs as part of service? Here is code the for it:

#include <windows.h>
#include <Winnetwk.h>
#include <iostream>
#pragma comment(lib, "wbemuuid.lib")
int wmi_call()
{
HRESULT hRes;
hRes = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hRes))
{
    cout << "Unable to launch COM: 0x" << std::hex << hRes << endl;
    return 1;
}

if ((FAILED(hRes = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0))))
{
    cout << "Unable to initialize security: 0x" << std::hex << hRes << endl;
    return 1;
}

IWbemLocator* pLocator = NULL;
if (FAILED(hRes = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pLocator))))
{
    cout << "Unable to create a WbemLocator: " << std::hex << hRes << endl;
    return 1;
}

IWbemServices* pService = NULL;
if (FAILED(hRes = pLocator->ConnectServer(L"root\\CIMV2", NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pService)))
{
    pLocator->Release();
    cout << "Unable to connect to \"CIMV2\": " << std::hex << hRes << endl;
    return 1;
}

IEnumWbemClassObject* pEnumerator = NULL;

if (FAILED(hRes = pService->ExecQuery(L"WQL", L"SELECT ProviderName FROM Win32_LogicalDisk where DeviceId = \"Z:\"", WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator)))
{
    pLocator->Release();
    pService->Release();
    cout << "Unable to retrive desktop monitors: " << std::hex << hRes << endl;
    return 1;
}

IWbemClassObject* clsObj = NULL;
int numElems;
while ((hRes = pEnumerator->Next(WBEM_INFINITE, 1, &clsObj, (ULONG*)&numElems)) != WBEM_S_FALSE)
{
    if (FAILED(hRes))
        break;

    VARIANT vRet;
    VariantInit(&vRet);
    hRes = clsObj->Get(L"ProviderName", 0, &vRet, NULL, NULL);
    if (SUCCEEDED(hRes) && vRet.vt == VT_BSTR)
    {
        std::wcout << L"Succesfull conversion to UNC path, ProviderName: " << vRet.bstrVal << endl;
        VariantClear(&vRet);
    }
    else
    {
        cout << "Unable clsObj " << std::hex << hRes << endl;
    }
    clsObj->Release();
}

pEnumerator->Release();
pService->Release();
pLocator->Release();
return 0;
}
  • Side note: I would highly suggest using a RAII library around COM pointers like the one provided in `` – Mgetz May 08 '18 at 15:36

1 Answers1

0

it worked fine, when used "SELECT ProviderName FROM Win32_MappedLogicalDisk where DeviceId = \"Z:\"" instead of "SELECT ProviderName FROM Win32_LogicalDisk where DeviceId = \"Z:\""