|
-
December 20th, 2009, 09:22 PM
#9
Re: get computer name
 Originally Posted by memeloo
Took the above sample and cleaned it up a little....
Code:
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace Sample
{
class Program
{
[DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
public static extern int OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);
[DllImport("winspool.Drv", EntryPoint = "GetPrinterA", SetLastError = true,
CharSet = CharSet.Ansi, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern bool GetPrinter(IntPtr hPrinter, Int32 dwLevel,
IntPtr pPrinter, Int32 dwBuf, out Int32 dwNeeded);
[DllImport("winspool.drv", CharSet = CharSet.Auto)]
public static extern int EnumJobs(IntPtr hPrinter, int FirstJob, int
NoJobs, int Level, IntPtr pInfo, int cdBuf, out int pcbNeeded, out int
pcReturned);
[DllImport("winspool.drv", SetLastError = true)]
static extern int ClosePrinter(IntPtr hPrinter);
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct JOB_INFO_1
{
public int JobId;
public string pPrinterName;
public string pMachineName;
public string pUserName;
public string pDocument;
public string pDatatype;
public string pStatus;
public int Status;
public int Priority;
public int Position;
public int TotalPages;
public int PagesPrinted;
public SYSTEMTIME Submitted;
}
[StructLayout(LayoutKind.Sequential)]
struct PRINTER_DEFAULTS
{
public IntPtr pDatatype;
public IntPtr pDevMode;
public int DesiredAccess;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PRINTER_INFO_2
{
[MarshalAs(UnmanagedType.LPTStr)]
public string pServerName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pPrinterName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pShareName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pPortName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDriverName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pComment;
[MarshalAs(UnmanagedType.LPTStr)]
public string pLocation;
public IntPtr pDevMode;
[MarshalAs(UnmanagedType.LPTStr)]
public string pSepFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string pPrintProcessor;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDatatype;
[MarshalAs(UnmanagedType.LPTStr)]
public string pParameters;
public IntPtr pSecurityDescriptor;
public uint Attributes;
public uint Priority;
public uint DefaultPriority;
public uint StartTime;
public uint UntilTime;
public uint Status;
public uint cJobs;
public uint AveragePPM;
}
private const int PRINTER_ACCESS_ADMINISTER = 0x4;
private const int PRINTER_ACCESS_USE = 0x8;
private const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
private const int PRINTER_ALL_ACCESS =
(STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_ADMINISTER
| PRINTER_ACCESS_USE);
static void AccessPrinter(string printerName)
{
int result;
IntPtr hPrinter;
int nBytesNeeded;
int pcbNeeded;
int pcReturned;
IntPtr ptrPrinterInfo;
int nRet;
int nJunk;
PRINTER_DEFAULTS pd = new PRINTER_DEFAULTS();
PRINTER_INFO_2 pinfo = new PRINTER_INFO_2();
int rawsize = Marshal.SizeOf(pd);
IntPtr pdPtr = Marshal.AllocHGlobal(rawsize);
pd.DesiredAccess = PRINTER_ALL_ACCESS;
Marshal.StructureToPtr(pd, pdPtr, true);
result = OpenPrinter(printerName, out hPrinter, pdPtr);
if (result == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out nBytesNeeded);
if (nBytesNeeded <= 0)
{
throw new System.Exception("Unable to allocate memory");
}
else
{
ptrPrinterInfo = Marshal.AllocCoTaskMem(nBytesNeeded);
ptrPrinterInfo = Marshal.AllocHGlobal(nBytesNeeded);
nRet = Convert.ToInt32(GetPrinter(hPrinter, 2,
ptrPrinterInfo, nBytesNeeded, out nJunk));
if (nRet == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
pinfo = (PRINTER_INFO_2)Marshal.PtrToStructure(ptrPrinterInfo,
typeof(PRINTER_INFO_2));
EnumJobs(hPrinter, 0, (int)pinfo.cJobs, 1, IntPtr.Zero, 0, out pcbNeeded, out pcReturned);
IntPtr pData = Marshal.AllocHGlobal(pcbNeeded);
EnumJobs(hPrinter, 0, (int)pinfo.cJobs, 1, pData, pcbNeeded, out pcbNeeded, out pcReturned);
JOB_INFO_1[] jobs = new JOB_INFO_1[pcReturned];
int pTemp = pData.ToInt32(); //start pointer
for (int i = 0; i < pcReturned; ++i)
{
jobs[i] = (JOB_INFO_1)Marshal.PtrToStructure(new
IntPtr(pTemp), typeof(JOB_INFO_1));
Console.WriteLine(jobs[i].JobId.ToString() + Environment.NewLine);
Console.WriteLine(jobs[i].pUserName + Environment.NewLine);
pTemp+= Marshal.SizeOf( typeof(JOB_INFO_1) );
}
Marshal.FreeHGlobal(pData);
ClosePrinter(hPrinter);
for (int j = 0; j < pcReturned; ++j)
{
Console.WriteLine(jobs[j].pMachineName);
Console.WriteLine(jobs[j].pUserName);
}
}
static void Main()
{
AccessPrinter(@"\\myServer\myPrinter");
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|