我现在有一台USB打印机联到我的PC上,我想编一个程序运行在我的PC上。
1)。 找到打印机连接的端口
2)。 打开该端口并读取里面的信息(里面会读到数据吗?)
3)。 把我要打印的内容直接写到该端口,让打印机打印出来请各位大侠给段代码, 第一步已经实现了,第2,3不不知道如何做, 是不是使用CreateFile 和WriteFile函数就可以吗?
具体参数要如何传?
谢谢,谢谢。

解决方案 »

  1.   

    MSDN中不是有示例,直接发送数据到打印机。无论是usb还是并口
    Sending Data Directly to a Printer
    It is sometimes necessary to bypass the driver and send printer-specific data directly to a printer. The first code sample shows how this can be done for both local and networked printers that use GDI-based printer drivers. The second code sample below shows how to send printer control data directly to printers that use XPSDrv printer drivers. This method can be used to replace the PASSTHROUGH escape and SpoolFile methods.     //
        // 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 );
            if (bStatus) {
                // Fill in the structure with info about this "document."
                DocInfo.pDocName = (LPTSTR)_T("My Document");
                DocInfo.pOutputFile = NULL;
                DocInfo.pDatatype = (LPTSTR)_T("RAW");            // Inform the spooler the document is beginning.
                dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
                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;
        }
               
        //
        // RawDataToXpsPrinter - sends binary data directly to a printer
        // with an XPSDrv Printer Driver
        // 
        // 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 RawDataToXpsPrinter (LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
        {
            BOOL  bStatus = FALSE;
            HANDLE     hPrinter = NULL;
            DOC_INFO_1  DocInfo;
            DWORD  dwPrtJob = 0L;
            DWORD  dwBytesWritten = 0L;        // Open a handle to the printer.
            bStatus = OpenPrinter (szPrinterName, &hPrinter, NULL);
            
            if (bStatus) {
                // Fill in the structure with info about this "document."
                DocInfo.pDocName = (LPTSTR)_T("My Document");
                DocInfo.pOutputFile = NULL;
                DocInfo.pDatatype = (LPTSTR)_T("XPS_PASS");            dwPrtJob = StartDocPrinter (
                                hPrinter,
                                1,
                                (LPBYTE)&DocInfo);            if (dwPrtJob > 0) {
                        // Send the data to the printer.
                        bStatus = WritePrinter (
                        hPrinter,
                        lpData,
                        dwCount,
                        &dwBytesWritten);
                }
                
                EndDocPrinter (hPrinter);            // Close the printer handle.
                bStatus = ClosePrinter(hPrinter);
            }
            
            if (!bStatus || (dwCount != dwBytesWritten)) {
                bStatus = FALSE;
            } else {
                bStatus = TRUE;
            }        return bStatus;
        }      
      

  2.   

    这样做 应该很麻烦,等于写一个打印机的显示库,不一样型号的打印机 显示库也不一样。
    这个方法基本不可行。为啥不直接用打印机的驱动呢??
    给你个例子http://www.vckbase.com/document/viewdoc/?id=1618
      

  3.   

    参考:
    http://www.semiapps.com.cn/content.php?content_id=80705225019921834&node_id=138
      

  4.   

    To Ghost90,
    是的, 调打印驱动肯定没有问题。
    但是现在想做个工具直接把数据发送到打印机所在的USB端口。找了一堆资料。
    现在的问题是不知道向哪里写数据, USB有好多的设备, HC, HID,Hub,
    谁知道打开那个设备吗?现在可以看到打印机连在: \\\\.\\HCD2上。
    只是想试试, 不知道能不能成功。
      

  5.   

    吧问题简化一下,原来的打印机都是连并口的,把数据送到并口就直接送到打印机里了。
    回到你的问题,只要把数据送到打印机对应的USB口上就可以了,上网找下怎么向USB里写数据就可以了
      

  6.   

    再请教一个问题:
    USB设备都怎么分类, 我找到一段代码, 分什么HID (用SetupDiGetClassDevs函数读取信息), 感觉是鼠标之类; 另一类好像是Host   Controler设备, (通过列举HCName, 形如“\\\\.\\HCD2”来获取信息), 我的打印机就是这类。 最后一类就是U盘了。
    有谁知道这方面的知识吗?给个文档或链接好不好? 多谢。
      

  7.   


    http://www.vckbase.com/document/viewdoc/?id=900
      

  8.   

    To Ghost90:
    是的,谢谢简化了问题,不过不知道“打印机对应的USB口”是什么东西,怎么打开呀。 刚才乱找了一个句柄(用CreateFile传入 设备名称得到 ),没有成功。
    有代码共享一下吗?
     
      

  9.   

    没有代码,上
    www.vckbase.com
    搜下