0
我們已經寫在C#(其中P /從WINSPOOL.DRV調用函數)的一些打印服務器軟件。一些「打印機」實際上是虛擬打印機的print to a directory。安裝這樣的打印機的代碼是:誤差在64位增加一個虛擬打印機的Windows 7
public override void AddLocalDirectoryPrinter(string printerName, string directory)
{
IntPtr hPrinter = IntPtr.Zero;
try
{
PRINTER_DEFAULTS pd;
PRINTER_INFO_2 pi2;
pd.datatype = null;
pd.pDevMode = IntPtr.Zero;
// Access = PRINTER_ALL_ACCESS
// http://msdn.microsoft.com/en-us/library/cc244650%28v=prot.10%29.aspx
pd.desiredAccess = PRINTER_ALL_ACCESS;
// Attributes = PUBLISHED | DO_COMPLETE_FIRST | SHARED
// http://msdn.microsoft.com/en-us/library/aa394363%28v=vs.85%29.aspx
pi2.attributes = 0x2208;
pi2.averagePpm = 0;
pi2.jobs = 0;
pi2.defaultPriority = 0;
pi2.comment = "Auto-Generated by [name of program]";
pi2.datatype = "RAW";
pi2.pDevMode = IntPtr.Zero;
pi2.driverName = "Generic/Text Only";
pi2.location = string.Empty;
pi2.parameters = string.Empty;
pi2.portName = directory;
pi2.printerName = printerName.ToUpperInvariant();
pi2.printProcessor = "WinPrint";
pi2.priority = 0;
pi2.pSecurityDescriptor = IntPtr.Zero;
pi2.sepFile = string.Empty;
pi2.serverName = string.Empty;
pi2.shareName = printerName.ToUpperInvariant();
pi2.startTime = 0;
pi2.status = 0;
pi2.untilTime = 0;
hPrinter = AddPrinter(null, 2, ref pi2);
if (hPrinter == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
catch (Exception ex)
{
throw new PrintingException("Failed to add printer", ex);
}
finally
{
if (hPrinter != IntPtr.Zero)
{
ClosePrinter(hPrinter);
}
}
}
在32位Windows XP上,上述代碼運行時沒有錯誤。但在64位Windows 7,調用AddPrinter失敗,Windows錯誤1796(「指定的端口是未知」)。
爲什麼它在一個版本的Windows而不是另一個工作?