了解C++示例 - 打印机,句柄,字符串

Suh*_*pta 0 c++ printing visual-c++

我在这里遇到的以下代码中几乎没有问题:

// RawDataToPrinter - sends binary data directly to a printer 
//  
// szPrinterName: NULL-terminated string specifying printer name 
// lpData:        Pointer to raw data bytes 
// dwCount        Length of lpData in bytes 
//  
// Returns: TRUE for success, FALSE for failure. 
//  
BOOL RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
    BOOL     bStatus = FALSE;
    HANDLE     hPrinter = NULL;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob = 0L;
    DWORD      dwBytesWritten = 0L;

    // Open a handle to the printer. 
    bStatus = OpenPrinter( szPrinterName, &hPrinter, NULL );  // question 1
    if (bStatus) {
        // Fill in the structure with info about this "document." 
        DocInfo.pDocName = (LPTSTR)_T("My Document");  // question 2
        DocInfo.pOutputFile = NULL;                 // question 3
        DocInfo.pDatatype = (LPTSTR)_T("RAW");   // question 4

        // Inform the spooler the document is beginning. 
        dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );  // question 5
        if (dwJob > 0) {
            // Start a page. 
            bStatus = StartPagePrinter( hPrinter );
            if (bStatus) {
                // Send the data to the printer. 
                bStatus = WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten);
                EndPagePrinter (hPrinter);
            }
            // Inform the spooler that the document is ending. 
            EndDocPrinter( hPrinter );
        }
        // Close the printer handle. 
        ClosePrinter( hPrinter );
    }
    // Check to see if correct number of bytes were written. 
    if (!bStatus || (dwBytesWritten != dwCount)) {
        bStatus = FALSE;
    } else {
        bStatus = TRUE;
    }
    return bStatus;
}
Run Code Online (Sandbox Code Playgroud)

请在评论中查找问题编号

问题1

  • 当我设定hPrinter = null然后是什么意思&hPrinter

问题2

  • 什么(LPTSTR)_T表示? (见T)

问题3

  • 这里的null表示什么?

问题4

  • 什么RAW意思(它是什么类型的数据?)

问题5

  • 1这里有什么表示?

Bli*_*ndy 8

  1. &hPrinterhPrinter变量的地址.您需要传递它,以便OpenPrinter函数可以在其中写入打印机的实际句柄.

  2. _T获取您的字符串并根据您的编译设置将其转换为正确的ANSI(8位/字符)或Unicode(16位/字符)表示.基本上它是一个扩展为空(ANSI)或L(Unicode)加上实际字符串的宏.

  3. 在这里,在代码中?NULL是一个扩展到(基本上)的宏0.它用于提供合适的默认值.

  4. 原始数据正是它听起来的原始数据.它不是以任何方式构造的,它只是传递给(通常)设备的一大块字节.

  5. 来自http://msdn.microsoft.com/en-us/library/dd145115(v=vs.85).aspx:

    pDocInfo指向的结构版本.该值必须为1.


Mar*_*all 6

  1. &hPrinter是符号hPrinter占用的内存空间的地址.例如,如果值hPrinter存储在地址0x1000(不太可能!),则四个(或八个或更多; HANDLE定义为PVOID,因此它将在架构之间变化)从0x1000开始的字节将包含值0(NULL).换句话说,当hPrinter可能是NULL,它仍然有一个地址,它仍然占据内存空间.

    编辑:我的回答可能不是你所追求的.OpenPrinter要传递地址的第二个参数hPrinter实际上是输出参数,而不是输入参数.这是一个常见的解决方法,因为C限制为一个返回值,并且大多数Win32 API函数返回状态值.一旦OpenPrinter功能成功完成,它将接收打印机的句柄.

  2. _T()宏确定您的项目是否配置为使用Unicode,并相应地解释字符串文字; 请参阅MSDN的解释.

  3. pOutputFile 在这里描述为:

    指向以null结尾的字符串的指针,该字符串指定输出文件的名称.要打印到打印机,请将其设置为NULL.

    换句话说:在打印到打印机而不是文件时,将其设置为NULL.NULL通常在指针类型的上下文中表示"无值".

  4. 别人更好地回答了.

  5. 有关详细信息,请参阅内容,但基本上

    pDocInfo指向的结构版本.该值必须为1.

    ......所以如果我是你,我也不会过分担心.;)