Locked StartDocPrinter question in c#

  • Monday, July 16, 2012 2:17 PM
     
      Has Code

    I have this c++ code and I'm trying to port it to c#: 

    DOC_INFO_2 m_prtInfo;

    m_prtInfo.pDocName = "spooling";
    m_prtInfo.pOutputFile = 0;
    m_prtInfo.pDatatype = 0; 
    m_prtInfo.dwMode = DI_CHANNEL_WRITE;

    if(::OpenPrinter(m_prtName.GetBuffer(m_prtName.GetLength()),&m_prtHandle,NULL) == TRUE)
    {

    if(::StartDocPrinter(m_prtHandle,1,(BYTE*)&m_prtInfo) == 0)
    {
    ClosePrinter(m_prtHandle);
    return false;
    }
    ::WritePrinter(m_prtHandle,(LPVOID)m_prtLine,strlen(textToWrite),&written);

    ::EndDocPrinter(m_prtHandle) ;
    ::ClosePrinter(m_prtHandle);

    The bit I'm having difficulty with is : (BYTE*)&m_prtInfo

    The prototype in c++ is :

    DWORD StartDocPrinter(
      __in  HANDLE hPrinter,
      __in  DWORD Level,
      __in  LPBYTE pDocInfo
    );

    This can api can take a number of different structures depending on the Level value.

    I can do this :

       [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
            public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    but obviously that restricts the call to a DOCINFOA structure, or I can use the prototype as a DOC_INFO_2 but it the call fails if I try that.

    Any ideas or clarifications needed?

All Replies

  • Monday, July 16, 2012 5:46 PM
     
     

    Hi,

    Keep in mind that C# allows method overloading so defining multiple time the StartDocPrinter function (one declaration for each needed structure) is perfectly legal.

    Also do you really have to use the Windows API ? If the C++ code just prints a document, can't you just use the .NET Framework printing classes to print the same document ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

  • Monday, July 16, 2012 5:52 PM
     
     

    Hi,

    Keep in mind that C# allows method overloading so defining multiple time the StartDocPrinter function (one declaration for each needed structure) is perfectly legal.

    Also do you really have to use the Windows API ? If the C++ code just prints a document, can't you just use the .NET Framework printing classes to print the same document ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    I think I've solved the problem I used overloading...I just need to get a line printer to test it on(if I can still find one).

    No I can't use the .net stuff as this is for an audit trail printer and the .net printer functions are page printing. I just need a line printed after a transaction is completed. Of course maybe somebody knows better?

  • Monday, July 16, 2012 8:26 PM
     
     

    Rewrite the code to use the .NET PrintDocument component.  Or closer to the C++ code, the VB PowerPacks.


  • Friday, August 10, 2012 5:31 AM
     
     Answered

    Hi bealtine2,

      I have lots of documents about its detailed develop information,you could follow its guides:

      Use StartDoc when using GDI functions like TextOut.
    http://msdn.microsoft.com/library/d...l_95sz.asp

    Use StartDocPrinter when directly writing PCL, PostScript or other page
    description language to the printer, without GDI.
    http://msdn.microsoft.com/library/d...l_33n6.asp

    Please read the PlatformSDK and the section "Using the Printing Functions"
    where you can find out more, on how to print under windows.
    http://msdn.microsoft.com/library/d...l_1wfn.asp

    Christoph Lindemann
    Undocumented Printing
    http://undocprint.printassociates.com/

      And following kb article is about its usual issue collected by customers.

      Some Error Messages When Printing in Windows 

      Hope it helps you.


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".