Vb.net ick(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击 如果是真的话 '允许用户选择一个文件。 Dim ofd作为新的OpenFileDialog() 如果DialogResult.OK=ofd.ShowDialog(Me),则 '允许用户选择打印机。 将pd设置为新的打印对话框() pd.PrinterSettings=新的PrinterSettings() 如果DialogResult.OK=pd.ShowDialog(Me),则 '将文件打印到打印机。 SendFileToPrinter(pd.PrinterSettings.PrinterName,ofd.FileName) 如果结束 如果结束 如果结束 端接头 末级
我有一些OpenPrinter的C代码,应该很容易翻译Vb.net ick(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击 如果是真的话 '允许用户选择一个文件。 Dim ofd作为新的OpenFileDialog() 如果DialogResult.OK=ofd.ShowDialog(Me),则 '允许用户选择打印机。 将pd设置为新的打印对话框() pd.PrinterSettings=新的PrinterSettings() 如果DialogResult.OK=pd.ShowDialog(Me),则 '将文件打印到打印机。 SendFileToPrinter(pd.PrinterSettings.PrinterName,ofd.FileName) 如果结束 如果结束 如果结束 端接头 末级,vb.net,Vb.net,我有一些OpenPrinter的C代码,应该很容易翻译 [DllImport("winspool.drv", EntryPoint = "OpenPrinter", SetLastError = true)] internal static extern bool OpenPrinter(string pPrinterName, ref IntPtr phPrinter, PRINTER_DEFAULTS pDefault); [DllImport("winspool.drv", EntryP
[DllImport("winspool.drv", EntryPoint = "OpenPrinter", SetLastError = true)]
internal static extern bool OpenPrinter(string pPrinterName, ref IntPtr phPrinter, PRINTER_DEFAULTS pDefault);
[DllImport("winspool.drv", EntryPoint = "ClosePrinter", SetLastError = true)]
internal static extern int ClosePrinter(IntPtr hPrinter);
[StructLayout(LayoutKind.Sequential)]
public class PRINTER_DEFAULTS
{
public string pDatatype;
public IntPtr pDevMode;
public int DesiredAccess;
}
public struct OpenPrinterAccessCodes
{
public const int DELETE = 0x10000; // DELETE - Allowed to delete printers
public const int READ_CONTROL = 0x20000; // READ_CONTROL - Allowed to read printer information
public const int WRITE_DAC = 0x40000; // WRITE_DAC - Allowed to write device access control info
public const int WRITE_OWNER = 0x80000; // WRITE_OWNER - Allowed to change the object owner
public const int SERVER_ACCESS_ADMINISTER = 0x1;
public const int SERVER_ACCESS_ENUMERATE = 0x2;
public const int PRINTER_ACCESS_ADMINISTER = 0x4;
public const int PRINTER_ACCESS_USE = 0x8;
public const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
public const int PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE);
public const int SERVER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE);
public const int MAX_PORTNAME_LEN = 64;
public const int MAX_NETWORKNAME_LEN = 49;
public const int MAX_SNMP_COMMUNITY_STR_LEN = 33;
public const int MAX_QUEUENAME_LEN = 33;
public const int MAX_IPADDR_STR_LEN = 16;
public const int ERROR_INSUFFICIENT_BUFFER = 122;
public const int ERROR_INVALID_FLAGS = 1004;
}
public IntPtr OpenPrinterHandle(string printerName)
{
var def = new PRINTER_DEFAULTS { pDatatype = null, pDevMode = IntPtr.Zero, DesiredAccess = OpenPrinterAccessCodes.PRINTER_ALL_ACCESS };
var hPrinter = IntPtr.Zero;
if (!OpenPrinter(printerName, ref hPrinter, def))
{
var lastWin32Error = new Win32Exception(Marshal.GetLastWin32Error());
Logger.Log("Failed open Printer: " + lastWin32Error.Message);
throw lastWin32Error;
}
return hPrinter;
}
public void ClosePrinterHandle(IntPtr hPrinter)
{
ClosePrinter(hPrinter);
}
87(0x57)是错误\无效\参数,“参数不正确”。换句话说,调用OpenPrinter中的一个参数错误
的Win32 API参考告诉我们以下哪一个:
pDefault[in]指向打印机默认结构的指针。此值可以为空
您在调用中将此设置为InPtr.Zero
,但声明InPtr.Zero“表示已初始化为零的指针或句柄”,并继续特别警告它不等同于Nothing
因此,对OpenPrinter的正确调用应该是:
OpenPrinter(szPrinterName.Normalize(),hPrinter,Nothing)
[DllImport("winspool.drv", EntryPoint = "OpenPrinter", SetLastError = true)]
internal static extern bool OpenPrinter(string pPrinterName, ref IntPtr phPrinter, PRINTER_DEFAULTS pDefault);
[DllImport("winspool.drv", EntryPoint = "ClosePrinter", SetLastError = true)]
internal static extern int ClosePrinter(IntPtr hPrinter);
[StructLayout(LayoutKind.Sequential)]
public class PRINTER_DEFAULTS
{
public string pDatatype;
public IntPtr pDevMode;
public int DesiredAccess;
}
public struct OpenPrinterAccessCodes
{
public const int DELETE = 0x10000; // DELETE - Allowed to delete printers
public const int READ_CONTROL = 0x20000; // READ_CONTROL - Allowed to read printer information
public const int WRITE_DAC = 0x40000; // WRITE_DAC - Allowed to write device access control info
public const int WRITE_OWNER = 0x80000; // WRITE_OWNER - Allowed to change the object owner
public const int SERVER_ACCESS_ADMINISTER = 0x1;
public const int SERVER_ACCESS_ENUMERATE = 0x2;
public const int PRINTER_ACCESS_ADMINISTER = 0x4;
public const int PRINTER_ACCESS_USE = 0x8;
public const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
public const int PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE);
public const int SERVER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SERVER_ACCESS_ADMINISTER | SERVER_ACCESS_ENUMERATE);
public const int MAX_PORTNAME_LEN = 64;
public const int MAX_NETWORKNAME_LEN = 49;
public const int MAX_SNMP_COMMUNITY_STR_LEN = 33;
public const int MAX_QUEUENAME_LEN = 33;
public const int MAX_IPADDR_STR_LEN = 16;
public const int ERROR_INSUFFICIENT_BUFFER = 122;
public const int ERROR_INVALID_FLAGS = 1004;
}
public IntPtr OpenPrinterHandle(string printerName)
{
var def = new PRINTER_DEFAULTS { pDatatype = null, pDevMode = IntPtr.Zero, DesiredAccess = OpenPrinterAccessCodes.PRINTER_ALL_ACCESS };
var hPrinter = IntPtr.Zero;
if (!OpenPrinter(printerName, ref hPrinter, def))
{
var lastWin32Error = new Win32Exception(Marshal.GetLastWin32Error());
Logger.Log("Failed open Printer: " + lastWin32Error.Message);
throw lastWin32Error;
}
return hPrinter;
}
public void ClosePrinterHandle(IntPtr hPrinter)
{
ClosePrinter(hPrinter);
}