Click to See Complete Forum and Search --> : get computer name


homer.favenir
December 16th, 2009, 12:51 AM
hi,
i want to get the computer name of incoming print to my printer in a network.

anyone knows how to do it?

thanks

Krishnaa
December 16th, 2009, 01:28 AM
You could use EnumJobs (http://msdn.microsoft.com/en-us/library/dd162625(VS.85).aspx)Win32 API to do this, you will get a JOB_INFO structure which has all the details of that job, like user, machine which created this job etc.

Here is a sample on MSDN (http://support.microsoft.com/kb/821769).

homer.favenir
December 16th, 2009, 01:54 AM
thanks

btw i already made a printer disable/enable apps, all i need is to check the computername of any client who is trying to print in my printer. i checked your link and its VB.Net and not csharp.net.
my app is in csharp.net

tia

Krishnaa
December 16th, 2009, 02:29 AM
So, read the code and create C# equivalent, they just have few differences only, if you can't understand then post that piece of code and ask.

homer.favenir
December 16th, 2009, 03:23 AM
its too long, and i only want is the machine name of the client. :(

Krishnaa
December 16th, 2009, 06:06 AM
Then do it yourself, you need to Open Handle to the printer and then call EnumJobs on it, you will have to do a p/invoke thing.

If that's too hard then there is no way by which I can help.

memeloo
December 16th, 2009, 06:10 AM
maybe this will help you http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.languages.csharp/2004-04/4001.html
almost exacly what you need

homer.favenir
December 19th, 2009, 05:17 AM
You could use EnumJobs (http://msdn.microsoft.com/en-us/library/dd162625(VS.85).aspx)Win32 API to do this, you will get a JOB_INFO structure which has all the details of that job, like user, machine which created this job etc.

Here is a sample on MSDN (http://support.microsoft.com/kb/821769).

hi,
i have an error in

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

the error is

Error 1 'Protected Overrides Sub Dispose(disposing As Boolean)' has multiple definitions with identical signatures.


tia

BobS0327
December 20th, 2009, 08:22 PM
maybe this will help you http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.languages.csharp/2004-04/4001.html
almost exacly what you need

Took the above sample and cleaned it up a little....

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");
}
}
}

homer.favenir
December 21st, 2009, 03:52 AM
exactly what i needed.
thanks a lot!
one more thing how can i insert the machinename in a listbox?
i tried everything but it didnt work

tia

BobS0327
December 21st, 2009, 07:52 AM
one more thing how can i insert the machinename in a listbox?

Does the following work for you?

string test= "Testing";
myListBox.Items.Add( test );

memeloo
December 21st, 2009, 08:26 AM
one more thing how can i insert the machinename in a listbox?
banned by google? http://msdn.microsoft.com/en-us/library/aa288403(VS.71).aspx
i tried everything but it didnt work
if you had tried everything it would certainly work.

homer.favenir
December 21st, 2009, 08:18 PM
i know how to add items in a listbox
but from the code above that gets the machine name, i cant find how to put it in a variable and insert it in listbox.

i tried everything, do some trial and error, but i dont know how to do it correctly.

thanks

BobS0327
December 22nd, 2009, 01:43 PM
i know how to add items in a listbox
but from the code above that gets the machine name, i cant find how to put it in a variable and insert it in listbox.

i tried everything, do some trial and error, but i dont know how to do it correctly.

thanks

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

public class ListBoxItemAdd : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;

[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, ref JOB_INFO_1[] jobout, ref int count)
{
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);
jobout = jobs;
count = pcReturned;
}

private System.ComponentModel.Container components = null;

public ListBoxItemAdd()
{
InitializeComponent();

}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();

this.button1.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 32);
this.button1.TabIndex = 0;
this.button1.Text = "Add";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.BackColor = System.Drawing.Color.Red;
this.listBox1.ForeColor = System.Drawing.SystemColors.HighlightText;
this.listBox1.Location = new System.Drawing.Point(8, 64);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(272, 238);
this.listBox1.TabIndex = 2;
//
// ListBoxItemAdd
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 318);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listBox1,
this.button1});
this.Name = "ListBoxItemAdd";
this.Text = "ListBoxItemAdd";
this.ResumeLayout(false);

}

[STAThread]
static void Main()
{
Application.Run(new ListBoxItemAdd());
}

private void button1_Click(object sender, System.EventArgs e)
{
JOB_INFO_1[] j = new JOB_INFO_1[1000];
int i = 0;
AccessPrinter(@"\\myServer\myPrinter", ref j, ref i);
for (int x = 0; x < i; ++x)
{
listBox1.Items.Add(j[x].pMachineName);
}
}
}

homer.favenir
December 22nd, 2009, 07:53 PM
thanks for the reply... i really appreciate it...:)
but it has an error...:(

Error 1 Program 'C:\Documents and Settings\homer.favenir.AAIGLOBAL\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.exe' has more than one entry point defined: 'ListBoxItemAdd.Main()'. Compile with /main to specify the type that contains the entry point. C:\Documents and Settings\homer.favenir.AAIGLOBAL\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Form1.cs 230 17 WindowsFormsApplication1



[B]Error 2 Program 'C:\Documents and Settings\homer.favenir.AAIGLOBAL\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.exe' has more than one entry point defined: 'WindowsFormsApplication1.Program.Main()'. Compile with /main to specify the type that contains the entry point. C:\Documents and Settings\homer.favenir.AAIGLOBAL\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Program.cs 14 21 WindowsFormsApplication1

memeloo
December 23rd, 2009, 01:30 AM
should we google for you again? :mad:

BobS0327
December 23rd, 2009, 06:17 AM
thanks for the reply... i really appreciate it...:)
but it has an error...:(

Error 1 Program 'C:\Documents and Settings\homer.favenir.AAIGLOBAL\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.exe' has more than one entry point defined: 'ListBoxItemAdd.Main()'. Compile with /main to specify the type that contains the entry point. C:\Documents and Settings\homer.favenir.AAIGLOBAL\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Form1.cs 230 17 WindowsFormsApplication1



[B]Error 2 Program 'C:\Documents and Settings\homer.favenir.AAIGLOBAL\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.exe' has more than one entry point defined: 'WindowsFormsApplication1.Program.Main()'. Compile with /main to specify the type that contains the entry point. C:\Documents and Settings\homer.favenir.AAIGLOBAL\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Program.cs 14 21 WindowsFormsApplication1


It's probably because you've included the void main function from the example above in your project which also has another entry point. Remove the void main function from your project.

homer.favenir
December 23rd, 2009, 06:29 AM
thank you