Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem reading port information
    primarykey
    data
    text
    <p>I have a problem retrieving the information of printer ports. I already use <a href="http://msdn.microsoft.com/en-ca/library/ff562761%28VS.85%29.aspx" rel="nofollow">XcvData</a> function to add, configure and delete ports, but I can not get the following to run as I expect it to:</p> <pre><code>public PrinterNativeMethods.PORT_DATA_1 GetPortData1FromPort(string serverName, string portName) { IntPtr printerHandle; PrinterNativeMethods.PRINTER_DEFAULTS defaults = new PrinterNativeMethods.PRINTER_DEFAULTS { DesiredAccess = PrinterNativeMethods.PrinterAccess.ServerAdmin }; string connection = string.Format(@"{0},XcvPort {1}", serverName, portName); PrinterNativeMethods.OpenPrinter(connection, out printerHandle, ref defaults); PrinterNativeMethods.CONFIG_INFO_DATA_1 configData = new PrinterNativeMethods.CONFIG_INFO_DATA_1 { dwVersion = 1, }; uint size = (uint)Marshal.SizeOf(configData); IntPtr pointer = Marshal.AllocHGlobal((int)size); Marshal.StructureToPtr(configData, pointer, true); PrinterNativeMethods.PORT_DATA_1 portData = new PrinterNativeMethods.PORT_DATA_1(); uint portDataSize = (uint)Marshal.SizeOf(portData); IntPtr portDataHandle = Marshal.AllocHGlobal((int)portDataSize); try { uint outputNeeded; uint status; var retVal = PrinterNativeMethods.XcvData(printerHandle, "GetConfigInfo", pointer, size, out portDataHandle, portDataSize, out outputNeeded, out status); //portDataHandle now points to a different location!? Unmarshalling will fail: portData = (PrinterNativeMethods.PORT_DATA_1)Marshal.PtrToStructure(portDataHandle, typeof(PrinterNativeMethods.PORT_DATA_1)); } finally { PrinterNativeMethods.ClosePrinter(printerHandle); Marshal.FreeHGlobal(pointer); Marshal.FreeHGlobal(portDataHandle); } return portData; } </code></pre> <p>from PrinterNativeMethods:</p> <pre><code> [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode)] internal static extern int XcvData( IntPtr handle, string dataName, IntPtr inputData, uint inputDataSize, out IntPtr outputData, uint outputDataSize, out uint outputNeededSize, out uint status); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct PORT_DATA_1 { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public string sztPortName; public uint dwVersion; public uint dwProtocol; public uint cbSize; public uint dwReserved; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 49)] public string sztHostAddress; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33h)] public string sztSNMPCommunity; public uint dwDoubleSpool; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)] public string sztQueue; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string sztIPAddress; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 540)] public byte[] Reserved; public uint dwPortNumber; public uint dwSNMPEnabled; public uint dwSNMPDevIndex; } </code></pre> <p>Additional comment: I cannot use WMI or prnadmin.dll as an alternative.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload