Search code examples
c++windowswinapi

How to use DeviceCapabilities() DC_BINNAMES in C++/CLI


How to use DeviceCapabilities() DC_BINNAMES in C++/CLI ?

LPTSTR sizeBuf = ...
DeviceCapabilities(printerName.c_str(), NULL, DC_BINNAMES, sizeBuf, NULL);
for (int i = 0; i < nCount; i++) std::wcout << sizeBuf[i] << << "\n";

Thanks


Solution

  • You need to allocate an array of 24-char arrays, and then pass in the address of that outer array so DeviceCapabilities() can fill in the inner arrays, eg:

    nCount = DeviceCapabilitiesW(printerName.c_str(), NULL, DC_BINNAMES, NULL, NULL);
    if (nCount < 0) ... // error handling
    
    std::vector<WCHAR[24]> binNames(nCount);
    
    nCount = DeviceCapabilitiesW(printerName.c_str(), NULL, DC_BINNAMES, reinterpret_cast<LPWSTR>(binNames.data()), NULL);
    if (nCount < 0) ... // error handling
    

    Just watch out that if any given name is exactly 24 characters then it will not be null-terminated, so you have to account for that, eg:

    for (int i = 0; i < nCount; ++i) {
        WCHAR binName[25] = {0}; // +1 for null terminator
        std::copy_n(binNames[i], 24, binName);
        std::wcout << binName << << L'\n';
    }