c# 中调用EnumJobs时出问题,有一个参数值返回不回来,有代码,帮忙看看

feilong215 2003-08-26 02:56:48
using System;
using System.Data;
using System.Runtime.InteropServices;

namespace EnumPrintjobstest
{
class Class1
{
[DllImport("winspool.drv",CharSet=CharSet.Auto)]
static extern bool OpenPrinter(string name,out IntPtr hPrinter, IntPtr pDefault);

[DllImport("winspool.drv",CharSet=CharSet.Auto)]
static extern bool ClosePrinter(IntPtr hPrinter);


[DllImport("winspool.drv",CharSet=CharSet.Auto)]
// static extern bool EnumJobs(IntPtr hPrinter,int FirstJob,int NoJobs,int Level,out IntPtr pJob,int cbBuf,out int pcbNeeded,out int pcReturned);
static extern bool EnumJobs(IntPtr hPrinter,int FirstJob,int NoJobs,int Level,out IntPtr pJob,int cbBuf,out IntPtr pcbNeeded,out IntPtr pcReturned);

[STAThread]
static void Main(string[] args)
{
string PrintName="Epson LQ";
EnumJobs(PrintName);


// Console.Write(PrintName.ToString());
string str=Console.ReadLine();

}

private static void EnumJobs(string StrPrintName)
{
bool bRet;
// int cbBuf=0;
IntPtr pPrintHand= IntPtr.Zero;
IntPtr Pdef= IntPtr.Zero;
IntPtr pJob=IntPtr.Zero;
// int dwNeeded,dwReturned;
IntPtr dwNeeded,dwReturned;

bRet = OpenPrinter(StrPrintName, out pPrintHand, Pdef);

Console.Write("PrintName:{0}\n",StrPrintName); //ok
Console.Write("PrintHand:{0}\n",pPrintHand); //ok

if (bRet)
{
if( ! EnumJobs( pPrintHand, 0, 256,1,out pJob , 0, out dwNeeded, out dwReturned ) )
{
Console.Write("dwNeeded {0}",dwNeeded); //ok
Console.Write("dwReturned {0}",dwReturned); //error ?
// ......
}

}
else
{
ClosePrinter(pPrintHand);
Console.Write("PrintName error");
}

ClosePrinter(pPrintHand);

}
}
}
...全文
80 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
namezhu 2003-09-01
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Runtime.InteropServices;

namespace EnumPrintjobstest
{
class Class1
{
[DllImport("winspool.drv",CharSet=CharSet.Auto)]
static extern bool OpenPrinter(string name,out IntPtr hPrinter, IntPtr pDefault);

[DllImport("winspool.drv",CharSet=CharSet.Auto)]
static extern bool ClosePrinter(IntPtr hPrinter);

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
struct SYSTEMTIME
{
public int wYear;
public int wMonth;
public int wDayOfWeek;
public int wDay;
public int wHour;
public int wMinute;
public int wSecond;
public int wMilliseconds;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
struct JOB_INFO_1
{
public int JobId;
[MarshalAs(UnmanagedType.LPTStr)]
public string pPrinterName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pMachineName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pUserName;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDocument;
[MarshalAs(UnmanagedType.LPTStr)]
public string pDatatype;
[MarshalAs(UnmanagedType.LPTStr)]
public string pStatus;
public int Status;
public int Priority;
public int Position;
public int TotalPages;
public int PagesPrinted;
public SYSTEMTIME Submitted;
}


[DllImport("winspool.drv",CharSet=CharSet.Auto)]
// static extern bool EnumJobs(IntPtr hPrinter,int FirstJob,int NoJobs,int Level,out IntPtr pJob,int cbBuf,out int pcbNeeded,out int pcReturned);
static extern bool EnumJobs(IntPtr hPrinter,int FirstJob,int NoJobs,int Level,IntPtr pJob,int cbBuf,out int pcbNeeded,out int pcReturned);

[STAThread]
static void Main(string[] args)
{
string PrintName="\\"+"\\"+"FRANK"+"\\"+"Epson LQ";
EnumJobs(PrintName);


// Console.Write(PrintName.ToString());
string str=Console.ReadLine();

}

private static void EnumJobs(string StrPrintName)
{
bool bRet;
IntPtr pPrintHand= IntPtr.Zero;
IntPtr Pdef= IntPtr.Zero;
IntPtr pJob=IntPtr.Zero;
int dwNeeded0=0;
int dwNeeded1=0;
int dwReturned=0;
int dwReturned1=0;
bRet = OpenPrinter(StrPrintName, out pPrintHand, Pdef);

Console.Write("PrintName:{0}\n",StrPrintName); //ok
Console.Write("PrintHand:{0}\n",pPrintHand); //ok

if (bRet)
{
try
{
EnumJobs( pPrintHand, 0, 256,1,pJob,0, out dwNeeded0,out dwReturned);
// EnumJobs( pPrintHand, 1, 256,1,pJob,0, out dwNeeded1,out dwReturned);
// dwReturned=dwNeeded0/(dwNeeded0-dwNeeded1);
// Console.Write("dwNeeded {0}",dwNeeded1); //ok
// Console.Write("dwReturned {0}",dwReturned); //error ?

pJob = Marshal.AllocHGlobal(dwNeeded0);
EnumJobs( pPrintHand, 0, 256,1,pJob,dwNeeded0, out dwNeeded0,out dwReturned);
JOB_INFO_1[] jobArray = new JOB_INFO_1[dwReturned];
IntPtr current=IntPtr.Zero;
current=pJob;
for (int i=0; i<jobArray.Length; i++)
{

jobArray[i] = (JOB_INFO_1) Marshal.PtrToStructure(current,typeof(JOB_INFO_1));
current=(IntPtr)((int)current+Marshal.SizeOf(typeof(JOB_INFO_1)));
// current=(IntPtr)((int)current+(int)(dwNeeded0/dwReturned));
Console.WriteLine(i+": \n"+"pUserName:"+jobArray[i].pUserName +"\n"+"pPrinterName:"+jobArray[i].pPrinterName+"\n"+"pDocument:"+jobArray[i].pDocument);
Console.WriteLine("pDatatype:"+jobArray[i].pDatatype +"\n"+"Submitted:"+jobArray[i].Submitted.ToString()+"\n"+"pStatus:"+jobArray[i].pStatus);
Console.WriteLine("TotalPages:"+jobArray[i].TotalPages+"\n"+"Priority:"+jobArray[i].Priority+"\n"+"PagesPrinted"+jobArray[i].PagesPrinted);
Console.WriteLine("JobId:"+jobArray[i].JobId+"\n"+"PagesPrinted:"+jobArray[i].PagesPrinted+"\n"+jobArray[i].pStatus);
Console.WriteLine("Status:"+jobArray[i].Status+"\n"+"pMachineName:"+jobArray[i].pMachineName+"\n"+jobArray[i].pStatus);
// Console.WriteLine(Convert.ToDateTime(jobArray[i].Submitted).ToString());
Console.WriteLine("SizeOf(typeof(JOB_INFO_1):"+Marshal.SizeOf(typeof(JOB_INFO_1)));
}
}

catch (Exception exp)
{
Console.WriteLine(exp.StackTrace);
Console.WriteLine(exp.Source);
Console.WriteLine(exp.Message);
Console.WriteLine(exp.HelpLink);
Console.WriteLine(exp.InnerException);
Console.WriteLine(exp.TargetSite);
Console.WriteLine(exp.GetBaseException());
Marshal.FreeHGlobal(pJob);
}

Marshal.FreeHGlobal(pJob);
}
else
{
ClosePrinter(pPrintHand);
Console.Write("PrintName error");
}

ClosePrinter(pPrintHand);

}
}
}
feilong215 2003-08-27
  • 打赏
  • 举报
回复
程序是没错,但运行时dwReturnde的值传回值为0,但它应该传回打印队列的个数啊.
rqxiang 2003-08-27
  • 打赏
  • 举报
回复
程序没错呀!
feilong215 2003-08-26
  • 打赏
  • 举报
回复
在打印對列中有數據時,傳不回數據.還有打印機名用你自己的本機的啊
wjohenw 2003-08-26
  • 打赏
  • 举报
回复
我机器上编译通过

能跑起来, 我是copy后csc的,没问题,

输出结果:
PrintName:Epson LQ
PrintHand:0
PrintName error

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧