Click here to Skip to main content
15,867,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying to run a code that I found on this site, "Getting the File System Image and Deleted Data Recovery." But when I run the project, a cpp file called "RemovebleDeviceInfo.cpp" has an error: "no matching function for call to 'std::__cxx11::basic_string<wchar_t>::basic_string(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type*)'".


Below is the code:

C++
<pre>std::wstring GetDevInterfaceVolume(PSP_DEVINFO_DATA pdevInfoData)
{

    ULONG   ulSize = 0;
    DEVINST childDevInst;
    bool isDisk = false;

    if ( NULL == pdevInfoData )
    {
        throw std::runtime_error("Wrong PSP_DEVINFO_DATA parameter");
    }

    DWORD res = CM_Get_Child_Ex(&childDevInst, pdevInfoData->DevInst, 0, NULL);
    isDisk = (CR_NO_SUCH_DEVNODE == res);
    if(isDisk)
    {
        childDevInst = pdevInfoData->DevInst;
    }
    else if ( CR_SUCCESS != res)
    {
        throw std::runtime_error("Can't get Child Device Instance");
    }

    TCHAR bDeviceID[MAX_DEVICE_ID_LEN] = {0};
    if ( CR_SUCCESS != CM_Get_Device_ID_Ex(childDevInst, bDeviceID, sizeof(bDeviceID)/sizeof(TCHAR), 0, NULL) ) 
    {
        throw std::runtime_error("Can't get Device ID");
    }

    LPGUID InterfaceClassGuid = isDisk ? (LPGUID)&DiskClassGuid : (LPGUID)&VolumeClassGuid;

    if ( CR_SUCCESS != CM_Get_Device_Interface_List_Size(&ulSize, InterfaceClassGuid, bDeviceID, 0) )
    {
        throw std::runtime_error("Can't get Device Instance List Size");
    }

    std::vector<TCHAR> sResult(ulSize, 0x00);
    if ( sResult.empty() )
    {
        throw std::runtime_error("Device Interface Volume is empty");
    }

    InterfaceClassGuid = isDisk ? (LPGUID)&GUID_DEVINTERFACE_DISK : (LPGUID)&GUID_DEVINTERFACE_VOLUME;

    if ( CR_SUCCESS != CM_Get_Device_Interface_List(InterfaceClassGuid, bDeviceID, &sResult.front(), ulSize, 0) )
    {
        throw std::runtime_error("Can't get Device Instance List");
    }

    sResult.resize(ulSize);

    if ( sResult.empty() )
    {
        throw std::runtime_error("Device Interface Volume is empty");
    }

    return std::wstring(&sResult.front());
}


The error comes from
return std::wstring(&sResult.front());

The link to the project : https://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=138888

What I have tried:

I attempted to find the answer on the internet but was unsuccessful. 
Posted
Updated 28-Dec-22 21:57pm

When you try to compile such (ill formed) code, there is a mismatch between TCHAR (resolved to char) and wstring.
You may easily verify that trying to compile the following
C++
#include <iostream>
#include <vector>
using namespace std;

int main()
{
  vector < wchar_t> w { L'h', L'e', L'l', L'l', L'o'};
  vector < char > v{ 'h', 'e', 'l', 'l', 'o'};

  wstring sw{&w.front()}; // compiles fine (wchar_t vector, wstring)
  
  wstring sv{&v.front()}; // OOOPS... (char vector, wstring)
}

The code is ill formed because it freely mixes TCHAR (which can be resolved either to char or to wchar_t), with wstring (which always use wchar_t).
As a workaround to fix your original code, you can set (assuming you are using Visual Studio) "Use Unicode Character Set" in project build options.
 
Share this answer
 
Don't post this under Quick Answers - if you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
 
Share this answer
 
It looks like you are building this code in the ASCII version, but the return statement expects that sResult contains Unicode data. Check the actual settings for the project and ensure they match what is used in the article.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900