我在使用win32 api addJob()的时候 无法从那个byte缓冲区中取得 jobID和Path (ADDJOB_INFO_1结构)
但是addJob()执行成功 在打印机队列里可以看到因为没有jobID 所以调用 scheduleJob()的时候return false,没办法把那个文件提交到打印机spool中
程序执行完 打印队列现在 刚才的那个任务状态是 删除中 然后就结束了我的问题就是 如何在成功调用addJob后 得到jobId和Path那两个参数值?下面是部分代码:namespace printPdf
{
public class Print
{
public Print(){ } [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct ADDJOB_INFO_1
{
public Int32 JobID;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpPath;
}
//声明win32 api打开打印机 获得打印机句柄
[DllImport("winspool.drv",CharSet=CharSet.Auto)]
static extern bool OpenPrinter(
[MarshalAs(UnmanagedType.LPTStr)]
string strPrinterName,
out IntPtr ptrPrinter,
IntPtr ptrDefalut);
//声明win32 api关闭打印机 获得打印机句柄
[DllImport("winspool.drv",CharSet=CharSet.Auto)]
static extern bool ClosePrinter(
IntPtr ptrPrinter);
//声明win32 api添加打印机作业
[DllImport("winspool.drv",CharSet=CharSet.Auto)]
static extern bool AddJob(
IntPtr ptrPrinter,
Int32 iLevel,
IntPtr ptrJob, 
Int32 iSize,
out Int32 iCpSize);
//声明win32 api提交打印机作业
[DllImport("winspool.drv",CharSet=CharSet.Auto)]
static extern bool ScheduleJob(
IntPtr ptrPrinter,
Int32 JobID);  public static void pdfPrint(){
PrintDocument prtdoc = new PrintDocument(); 
                         string strPrnName = prtdoc.PrinterSettings.PrinterName;//获取默认的打印机名
if (strPrnName != null && strPrnName.Length > 0)
{
  IntPtr prnHandle;
IntPtr jobInfo = Marshal.AllocHGlobal(100);
  if(OpenPrinter(strPrnName,out prnHandle,IntPtr.Zero))
  { 
Int32 handle = prnHandle.ToInt32();
ADDJOB_INFO_1 job = new ADDJOB_INFO_1();
Int32 cpSize;
Marshal.StructureToPtr(job,jobInfo,true);
bool flagA = AddJob(prnHandle,1,jobInfo,100,out cpSize);
job = (ADDJOB_INFO_1)Marshal.PtrToStructure(jobInfo,typeof(ADDJOB_INFO_1));
File.Copy(@"d:\1.pdf",job.lpPath,true);           bool flagS = ScheduleJob(prnHandle,job.JobID);
ClosePrinter(prnHandle);
           }
    Marshal.FreeHGlobal(jobInfo);
}
}

解决方案 »

  1.   

    Int32  handle  =  prnHandle.ToInt32();
    这句没有用 忘记删了
      

  2.   

    Marshal.StructureToPtr(job,jobInfo,true);
    也没用 调试用的 忘删了
      

  3.   

    你好.我最近在写一个打印有关的程序,通过PrintDocument类的Print方法启动打印.但我想等当前的打印任务完成,就是打印队列已经为空的时候,再送入下一个打印任务. 所以, 我需要监控打印队列的情况,我下了PrinterQueueWatch组件,但没找到相关使用手册 .
    希望楼主能告诉我方法. 谢谢!
      

  4.   

    你好.我最近在写一个打印有关的程序,通过PrintDocument类的Print方法启动打印.但我想等当前的打印任务完成,就是打印队列已经为空的时候,再送入下一个打印任务. 所以, 我需要监控打印队列的情况,我下了PrinterQueueWatch组件,但没找到相关使用手册 .
    希望楼主能告诉我方法. 谢谢!
      

  5.   

    chengsion()你可以查阅csdn文档 上面有个endpage事件 可以通知打印结束
      

  6.   


    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    public struct ADDJOB_INFO_1
    {
    [MarshalAs(UnmanagedType.LPTStr)]
    public string lpPath;
    public Int32 JobID;
    }
    顺序修改一下试试~