如何在 Windows XP、2003、7、2008 下以编程方式添加额外的打印机驱动程序 (x86/x64)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3158575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:44:44  来源:igfitidea点击:

How to add additional printer drivers (x86/x64) programmatically under Windows XP,2003,7,2008

windowsprinting

提问by Thomas Woischnig

I've written an printer driver which works under x86 and x64 MS Windows systems. I also have written an setup which installs the printer driver via AddPrinterDriver and AddPrinter functions.

我编写了一个在 x86 和 x64 MS Windows 系统下工作的打印机驱动程序。我还编写了一个通过 AddPrinterDriver 和 AddPrinter 函数安装打印机驱动程序的设置。

So far, so good. The thing is, if I want to share this printer from an x64 system to an x86 system, the x64 print server needs to have the additional drivers for x86 systems installed.

到现在为止还挺好。问题是,如果我想将这台打印机从 x64 系统共享到 x86 系统,x64 打印服务器需要安装用于 x86 系统的附加驱动程序。

Now I could just install the additional drivers via "Printer Properties" -> "Sharing" -> "Additional Drivers" and select the proper .inf file, but I want to do it via my setup program.

现在我可以通过“打印机属性”->“共享”->“附加驱动程序”安装附加驱动程序并选择正确的 .inf 文件,但我想通过我的安装程序来完成。

Does anybody know how to accomplish this?

有谁知道如何做到这一点?

Thanks for the help!

谢谢您的帮助!

回答by Thomas Woischnig

So I found an solution myself. There must be a better and simpler way, but well it works.

所以我自己找到了解决方案。一定有更好更简单的方法,但效果很好。

When installing the driver, also copy the right architecture files to the other architectures. x86 driver to x86 spooler driver dir, x64 to x64 spooler driver dir etc.

安装驱动程序时,还要将正确的架构文件复制到其他架构中。x86 驱动程序到 x86 spooler 驱动程序目录,x64 到 x64 spooler 驱动程序目录等。

Then before calling AddPrinterDriver and AddPrinter you must add a few registry values for each other architectures you want to support. Then call AddPrinterDriver and AddPrinter only for the base architecture of the os you install your driver on.

然后在调用 AddPrinterDriver 和 AddPrinter 之前,您必须为要支持的其他体系结构添加一些注册表值。然后仅为您安装驱动程序的操作系统的基本架构调用 AddPrinterDriver 和 AddPrinter。

The registry values must be added to

必须将注册表值添加到

SYSTEM\CurrentControlSet\Control\Print\Environments\{TARGET_ENVIROMENT}\Drivers\Version-3\{NAME_OF_YOUR_DRIVER}

SYSTEM\CurrentControlSet\Control\Print\Environments\{TARGET_ENVIROMENT}\Drivers\Version-3\{NAME_OF_YOUR_DRIVER}

whereby {TARGET_ENVIROMENT} is the other enviroment you want to support, e.g. "Windows NT x86"; "Windows x64"; "Windows IA64" and {NAME_OF_YOUR_DRIVER} must match the drivername you are using for AddPrinterDriver. The values you have to add there are a copy of the ones AddPrinterDriver will add to your systems enviroment. You can see them in detail in the code example below. After that just call AddPrinterDriver and AddPrinter for the system architecture and the additional drivers will also be present.

{TARGET_ENVIROMENT} 是您想要支持的其他环境,例如“Windows NT x86”;"Windows x64"; “Windows IA64”和 {NAME_OF_YOUR_DRIVER} 必须与您用于 AddPrinterDriver 的驱动程序名称匹配。您必须添加的值是 AddPrinterDriver 将添加到您的系统环境中的值的副本。您可以在下面的代码示例中详细了解它们。之后,只需为系统架构调用 AddPrinterDriver 和 AddPrinter,其他驱动程序也将出现。

In Win32 you could use the following to install the drivers for x86 and x64 on an x64 system (this example does no error checks):

在 Win32 中,您可以使用以下内容在 x64 系统上安装 x86 和 x64 驱动程序(此示例不进行错误检查):

    DWORD uSize;
    BYTE driver_dir_x86[MAX_PATH];
    BYTE driver_dir_x64[MAX_PATH];

    GetPrinterDriverDirectory(NULL,"Windows NT x86",1,driver_dir_x86,MAX_PATH,&uSize);
    GetPrinterDriverDirectory(NULL,"Windows x64",1,driver_dir_x64,MAX_PATH,&uSize);


    CopyFile(".\x86\printer_driver.dll",driver_dir_x86);
    CopyFile(".\x86\PRINTER.PPD",driver_dir_x86);
    CopyFile(".\x86\PRINTERUI.DLL",driver_dir_x86);
    CopyFile(".\x86\PRINTER.HLP",driver_dir_x86);

    CopyFile(".\x64\printer_driver.dll",driver_dir_x64);
    CopyFile(".\x86\PRINTER.PPD",driver_dir_x64);
    CopyFile(".\x86\PRINTERUI.DLL",driver_dir_x64);
    CopyFile(".\x86\PRINTER.HLP",driver_dir_x64);

    //Insert x86 additional drivers to registry
   RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3\", 0, KEY_ALL_ACCESS, &hkey);
   RegCreateKey(hkey, "My_Printer_Drivername", &hsubkey);       {
   RegSetValueEx(hsubkey, "Dependent Files", 0, REG_MULTI_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "Previous Names", 0, REG_MULTI_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "ColorProfiles", 0, REG_MULTI_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "CoreDependencies", 0, REG_MULTI_SZ, (BYTE*)"", lstrlen("")+1);
   DWORD Val = 3;
   RegSetValueEx(hsubkey, "Version", 0, REG_DWORD, (BYTE*)&Val, sizeof(DWORD));
   Val = 0;
   RegSetValueEx(hsubkey, "TempDir", 0, REG_DWORD, (BYTE*)&Val, sizeof(DWORD));
   Val = 2;
   RegSetValueEx(hsubkey, "Attributes", 0, REG_DWORD, (BYTE*)&Val, sizeof(DWORD));
   Val = 0;
   RegSetValueEx(hsubkey, "PrinterDriverAttributes", 0, REG_DWORD, (BYTE*)&Val, sizeof(DWORD));
   RegSetValueEx(hsubkey, "Configuration File", 0, REG_SZ, (BYTE*)"PRINTERUI.DLL", lstrlen("PRINTERUI.DLL")+1);
   RegSetValueEx(hsubkey, "Data File", 0, REG_SZ, (BYTE*)"PRINTER.PPD", lstrlen("PRINTER.PPD")+1);
   RegSetValueEx(hsubkey, "Driver", 0, REG_SZ, (BYTE*)"printer_driver.dll", lstrlen("printer_driver.dll")+1);
   RegSetValueEx(hsubkey, "Help File", 0, REG_SZ, (BYTE*)"PRINTER.HLP", lstrlen("PRINTER.HLP")+1);
   RegSetValueEx(hsubkey, "Monitor", 0, REG_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "Datatype", 0, REG_SZ, (BYTE*)"RAW", lstrlen("RAW")+1);
   RegSetValueEx(hsubkey, "Manufacturer", 0, REG_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "OEM URL", 0, REG_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "HardwareID", 0, REG_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "Provider", 0, REG_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "Print Processor", 0, REG_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "VendorSetup", 0, REG_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "InfPath", 0, REG_SZ, (BYTE*)"", lstrlen("")+1);
   RegSetValueEx(hsubkey, "DriverDate", 0, REG_SZ, (BYTE*)"01/01/2010", lstrlen("01/01/2010")+1);
   RegSetValueEx(hsubkey, "DriverVersion", 0, REG_SZ, (BYTE*)"1.0.0.0", lstrlen("1.0.0.0")+1);
   RegSetValueEx(hsubkey, "MinInboxDriverVerDate", 0, REG_SZ, (BYTE*)"01/01/2010", lstrlen("01/01/2010")+1);
   RegSetValueEx(hsubkey, "MinInboxDriverVerVersion", 0, REG_SZ, (BYTE*)"1.0.0.0", lstrlen("1.0.0.0")+1);
   RegCloseKey(hsubkey);
   RegCloseKey(hkey);

   //Add x64 printer driver
   DRIVER_INFO_3 di3;
   ZeroMemory(&di3, sizeof(DRIVER_INFO_3)); 
   di3.cVersion = 0x03; 
   di3.pConfigFile = "PRINTERUI.DLL"; 
   di3.pDataFile = "PRINTER.PPD";       
   di3.pDependentFiles = "";
   di3.pDriverPath = "printer_driver.dll"; 
   di3.pEnvironment = "Windows x64";
   di3.pHelpFile = "PRINTER.HLP";
   di3.pMonitorName = NULL; 
   di3.pName = "My_Printer_Drivername";
   di3.pDefaultDataType = TEXT("RAW");
   AddPrinterDriver(NULL, 3, (LPBYTE)&di3);