CreateDC returns an error 1801
<p>I need to receive screenshots from MS Mirror driver, but unexpectedly I noticed, that <strong>CreateDC()</strong> function returns <strong>NULL</strong> and <strong>GetLastError</strong> gives me Error#1801 ("The printer name is invalid")</p>

<pre><code>extern "C" __declspec(dllexport) HDC InitDC() {
    DWORD dwAttach = 0;
    DEVMODE devmode;

    // Make sure we have a display on this thread.
    BOOL change = EnumDisplaySettings(NULL,
        ENUM_CURRENT_SETTINGS,
        &amp;devmode);
    LPCWSTR driverName = L"Microsoft Mirror Driver";

    devmode.dmFields = DM_BITSPERPEL |
        DM_PELSWIDTH | 
        DM_PELSHEIGHT |
        DM_POSITION ;
    devmode.dmSize = sizeof(DEVMODE);
    devmode.dmDriverExtra = 0;

    if (change) 
    {
        // query all display devices in the system until we hit a primary
        // display device. Using it get the width and height of the primary
        // so we can use that for the mirror driver. Also enumerate the
        // display devices installed on this machine untill we hit
        // our favourate mirrored driver, then extract the device name string
        // of the format '\\.\DISPLAY#'

        DISPLAY_DEVICE dispDevice;

        FillMemory(&amp;dispDevice, sizeof(DISPLAY_DEVICE), 0);

        dispDevice.cb = sizeof(DISPLAY_DEVICE);

        LPCWSTR deviceName = NULL;

        devmode.dmDeviceName[0] = '\0';

        INT devNum = 0;
        BOOL result;
        DWORD cxPrimary = 0xFFFFFFFF;
        DWORD cyPrimary = 0xFFFFFFFF;

        // First enumerate for Primary display device:
        while ((result = EnumDisplayDevices(NULL,
            devNum,
            &amp;dispDevice,
            0)) == TRUE)
        {
            wcout &lt;&lt; "DriverName: " &lt;&lt; dispDevice.DeviceString &lt;&lt; endl;
            wcout &lt;&lt; "DevName: " &lt;&lt; dispDevice.DeviceName &lt;&lt; endl;

            if (dispDevice.StateFlags &amp; DISPLAY_DEVICE_PRIMARY_DEVICE)
            {
                // Primary device. Find out its dmPelsWidht and dmPelsHeight.
                EnumDisplaySettings(dispDevice.DeviceName,
                    ENUM_CURRENT_SETTINGS,
                    &amp;devmode);      
                cxPrimary = devmode.dmPelsWidth;
                cyPrimary = devmode.dmPelsHeight;

                wcout &lt;&lt; "Selected Primary DevName (width, height): " &lt;&lt; dispDevice.DeviceName &lt;&lt; " (" &lt;&lt; cxPrimary &lt;&lt; "," &lt;&lt; cyPrimary &lt;&lt; ")" &lt;&lt; endl &lt;&lt; endl;
                break;
            }
            devNum++;
        }

        // error check
        if (!result)
        {
            wcout &lt;&lt; "No " &lt;&lt; driverName &lt;&lt; " found " &lt;&lt; endl;
            exit(0);
        }

        if (cxPrimary == 0xffffffff || cyPrimary == 0xffffffff)
        {
            wcout &lt;&lt; "cxPrimary or cyPrimary not valid" &lt;&lt; endl;
            exit(0);
        }

        // Enumerate again for the mirror driver:
        devNum = 0;
        while ((result = EnumDisplayDevices(NULL,
            devNum,
            &amp;dispDevice,
            0)) == TRUE)
        {
            LPCWSTR devString = dispDevice.DeviceString;

            wcout &lt;&lt; " devString: "&lt;&lt; devString &lt;&lt; endl; 

            if (wcscmp(devString, driverName ) == 0) {
                wcout &lt;&lt; " Driver selected: "&lt;&lt; driverName &lt;&lt; endl;             
                break;
            }

            devNum++;
        }

        // error check       
        if (!result)
        {
            wcout &lt;&lt; "No " &lt;&lt; driverName &lt;&lt; " found " &lt;&lt; endl;
            exit(0);
        }

        wcout &lt;&lt; "DevNum " &lt;&lt; devNum &lt;&lt; endl &lt;&lt;
            "DeviceName: " &lt;&lt; dispDevice.DeviceName &lt;&lt; endl &lt;&lt;
            "DeviceString: " &lt;&lt; dispDevice.DeviceString &lt;&lt; endl &lt;&lt;
            "DeviceID: " &lt;&lt; dispDevice.DeviceID &lt;&lt; endl &lt;&lt;
            "DeviceKey: " &lt;&lt; dispDevice.DeviceKey &lt;&lt; endl;

        CHAR *deviceNum = new CHAR[MAX_PATH];
        LPSTR deviceSub;

        // Simply extract 'DEVICE#' from registry key.  This will depend
        // on how many mirrored devices your driver has and which ones
        // you intend to use.
        _wcsupr(&amp;dispDevice.DeviceKey[0]);

        deviceSub = (LPSTR)(wcsstr(&amp;dispDevice.DeviceKey[0],
                           L"\\DEVICE"));

        if (!deviceSub) {
            printf("deviceSub - yes \n"); 
            deviceNum[0] = (CHAR)(((string)("DEVICE0")).c_str());
        }
        else {
            printf("!deviceSub \n"); 
            deviceNum[0] = (CHAR)(((string)("DEVICE1")).c_str());
        }
        // Reset the devmode for mirror driver use:
        FillMemory(&amp;devmode, sizeof(DEVMODE), 0);
        devmode.dmSize = sizeof(DEVMODE);
        devmode.dmDriverExtra = 0;

        devmode.dmFields = DM_BITSPERPEL |
                           DM_PELSWIDTH | 
                           DM_PELSHEIGHT |
                           DM_POSITION;

        //wcscpy( devmode.dmDeviceName, sizeof(devmode.dmDeviceName, L"mirror" );
        deviceName = dispDevice.DeviceName;
        StringCbCopy(devmode.dmDeviceName, sizeof(devmode.dmDeviceName), dispDevice.DeviceName);

        wcout &lt;&lt; "dmDeviceName: " &lt;&lt; devmode.dmDeviceName &lt;&lt; endl;
        wcout &lt;&lt; "deviceName: " &lt;&lt; deviceName &lt;&lt; endl;
        wcout &lt;&lt; "driverName: " &lt;&lt; driverName &lt;&lt; endl;

        /* !!! THE PROBLEM IS HERE !!!*/
        HDC hdc = CreateDC(driverName, deviceName, NULL, &amp;devmode );

        if ( hdc == NULL ) {
            wcout &lt;&lt; "CreateDC error: " &lt;&lt; GetLastError() &lt;&lt; endl;
            return NULL;
        }

        return hdc;
    }

    return NULL;
}
</code></pre>

<p>Thanks for an assistance!</p>

<p>UPD1:</p>

<pre><code>deviceName = "\\.\DISPLAYV1"
driverName = "Microsoft Mirror Driver"
devmode.dmDeviceName = "\\.\DISPLAYV1"
</code></pre>