在64位Windows 10上运行32位应用程序时,在模拟后调用DocumentProperties时访问被拒绝

时间:2016-04-14 23:40:59

标签: c++ windows x86-64 impersonation print-spooler-api

在我的应用程序模拟用户之后,我需要获得打印机DEVMODE结构。只要我的架构与32位操作系统上运行的Windows 32位应用程序相匹配,反之亦然,这样就可以正常工作。但是,在对64位操作系统上运行的32位版本的应用程序进行模拟后,我对DocumentProperties的任何调用都会失败,并且在Windows 10上出现错误代码:5(访问被拒绝),或者在Windows 7上出现RPC错误。遗憾的是,由于需要与其进行交互的其他遗留应用程序,客户无法运行我的应用程序的64位版本。

有没有人知道这个问题的解决方法?

这是一个展示问题的小示例代码。您需要将其构建为x86应用程序并在64位操作系统上运行以查看问题。

// DocumentPropertiesTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include "Winspool.h"
#include "DocumentPropertiesTest.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(NULL);

    if (hModule != NULL)
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            _tprintf(_T("Fatal Error: MFC initialization failed\n"));
            nRetCode = 1;
        }
        else
        {
         wstring username;
         wstring domainName;
         wstring password;
         wstring printername;
         int lastError;

         cout << "Please specify a valid username: ";
         wcin >> username;
         cout << "Please specify the computer or domain name for the user. Use \".\" for this computer: ";
         wcin >> domainName;
         cout << "Please specify the users password: ";
         wcin >> password;
         cout << "Please give the printer name: ";
         wcin.ignore();
         getline (wcin, printername);

         HANDLE pHandle;
         HANDLE pPrinter;
         if (LogonUser(username.c_str(), domainName.c_str(), password.c_str(), LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &pHandle) != 0)
         {
            if (ImpersonateLoggedOnUser(pHandle) != 0)
            {
               PRINTER_DEFAULTS printerDefaults;

               printerDefaults.pDatatype     = NULL;
               printerDefaults.pDevMode      = NULL;
               printerDefaults.DesiredAccess = PRINTER_ALL_ACCESS;

               if (::OpenPrinter((LPWSTR)(printername.c_str()), &pPrinter, NULL))
               {
                  int dSize = ::DocumentPropertiesW(NULL, pPrinter, (LPWSTR)(printername.c_str()), NULL, NULL, 0);
                  if (dSize > 0)
                  {
                  }
                  else
                  {
                     lastError = ::GetLastError();
                     cout << "Failed DocumentProperties with Error code: " << lastError << endl;
                  }
                  ::ClosePrinter(pPrinter);
               }
               else
               {
                  lastError = ::GetLastError();
                  cout << "Failed OpenPrinter with Error code: " << lastError << endl;
               }
               RevertToSelf();
            }
            else
            {
               lastError = ::GetLastError();
               cout << "Failed ImpersonateLogonUser with Error code: " << lastError << endl;
            }
         }
         else
         {
            lastError = ::GetLastError();
            cout << "Failed LogonUser with Error code: " << lastError << endl;
         }

         system("pause");
        }
    }
    else
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
        nRetCode = 1;
    }

    return nRetCode;
}

0 个答案:

没有答案