Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 3, 2012 18:20
Show Gist options
  • Save davybrion/3611711 to your computer and use it in GitHub Desktop.
Save davybrion/3611711 to your computer and use it in GitHub Desktop.
code snippets for "Creating Remote TCP/IP Printer Ports From Code" post
BOOL WINAPI XcvData(HANDLE hXcv, LPCWSTR pszDataName, PBYTE pInputData, DWORD cbInputData,
PBYTE pOutputData, DWORD cbOutputData, PDWORD pcbOutputNeeded, PDWORD pdwStatus);
typedef struct _PORT_DATA_1 {
WCHAR sztPortName[MAX_PORTNAME_LEN];
DWORD dwVersion;
DWORD dwProtocol;
DWORD cbSize;
DWORD dwReserved;
WCHAR sztHostAddress[MAX_NETWORKNAME_LEN];
WCHAR sztSNMPCommunity[MAX_SNMP_COMMUNITY_STR_LEN];
DWORD dwDoubleSpool;
WCHAR sztQueue[MAX_QUEUENAME_LEN];
WCHAR sztIPAddress[MAX_IPADDR_STR_LEN];
BYTE Reserved[540];
DWORD dwPortNumber;
DWORD dwSNMPEnabled;
DWORD dwSNMPDevIndex;
} PORT_DATA_1, *PPORT_DATA_1;
public enum PrinterAccess
{
ServerAdmin = 0x01,
ServerEnum = 0x02,
PrinterAdmin = 0x04,
PrinterUse = 0x08,
JobAdmin = 0x10,
JobRead = 0x20,
StandardRightsRequired = 0x000f0000,
PrinterAllAccess = (StandardRightsRequired | PrinterAdmin | PrinterUse)
}
[StructLayout(LayoutKind.Sequential)]
public struct PrinterDefaults
{
public IntPtr pDataType;
public IntPtr pDevMode;
public PrinterAccess DesiredAccess;
}
[DllImport("winspool.drv", SetLastError = true)]
public static extern int OpenPrinter(string printerName, out IntPtr phPrinter,
ref PrinterDefaults printerDefaults);
[DllImport("winspool.drv", SetLastError = true)]
public static extern int ClosePrinter(IntPtr phPrinter);
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 RESERVED_BYTE_ARRAY_SIZE = 540;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct PortData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PORTNAME_LEN)]
public string sztPortName;
public UInt32 dwVersion;
public UInt32 dwProtocol;
public UInt32 cbSize;
public UInt32 dwReserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_NETWORKNAME_LEN)]
public string sztHostAddress;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_SNMP_COMMUNITY_STR_LEN)]
public string sztSNMPCommunity;
public UInt32 dwDoubleSpool;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_QUEUENAME_LEN)]
public string sztQueue;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_IPADDR_STR_LEN)]
public string sztIPAddress;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = RESERVED_BYTE_ARRAY_SIZE)]
public byte[] Reserved;
public UInt32 dwPortNumber;
public UInt32 dwSNMPEnabled;
public UInt32 dwSNMPDevIndex;
}
[DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int XcvDataW(IntPtr hXcv, string pszDataName,
IntPtr pInputData, UInt32 cbInputData, out IntPtr pOutputData, UInt32 cbOutputData,
out UInt32 pcbOutputNeeded, out UInt32 pdwStatus);
IntPtr printerHandle;
InteropStuff.PrinterDefaults defaults =
new InteropStuff.PrinterDefaults { DesiredAccess = InteropStuff.PrinterAccess.ServerAdmin };
InteropStuff.OpenPrinter(@"\myPrintServer,XcvMonitor Standard TCP/IP Port", out printerHandle, ref defaults);
InteropStuff.PortData portData = new InteropStuff.PortData
{
dwVersion = 1, // has to be 1 for some unknown reason
dwProtocol = 1, // 1 = RAW, 2 = LPR
dwPortNumber = 9100, // 9100 = default port for RAW, 515 for LPR
dwReserved = 0, // has to be 0 for some unknown reason
sztPortName = "DBR_172.30.164.15",
sztIPAddress = "172.30.164.15",
sztSNMPCommunity = "public",
dwSNMPEnabled = 1,
dwSNMPDevIndex = 1
};
uint size = (uint)Marshal.SizeOf(portData);
portData.cbSize = size;
IntPtr pointer = Marshal.AllocHGlobal((int)size);
Marshal.StructureToPtr(portData, pointer, true);
try
{
IntPtr outputData;
UInt32 outputNeeded;
UInt32 status;
InteropStuff.XcvDataW(printerHandle, "AddPort", pointer, size, out outputData, 0,
out outputNeeded, out status);
}
finally
{
InteropStuff.ClosePrinter(printerHandle);
Marshal.FreeHGlobal(pointer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment