无法从 LPT1 获取端口句柄

发布于 2024-10-29 02:33:41 字数 566 浏览 2 评论 0原文

我正在编写一个从文件读取并发送到打印机进行打印的程序。

我将“HP Laser Jet 4”设置为默认打印机,并在“HP Laser Jet 4”的打印机属性中选中“LPT1”。后台打印程序也在运行 (Windows 7)。

问题是 hPort 总是返回 INVALID_HANDLE_VALUE

我没有真正的打印机。有问题吗?

#include "stdafx.h"
#include <windows.h>

int main()
{
    HANDLE hPort = CreateFile(_T("LPT1:"), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if(hPort != INVALID_HANDLE_VALUE) {
        printf("success");
    } else {
        printf("%i", GetLastError());
    }    
    return 0;
}

I'm writing a program which reads from file and send to printer to print.

I set "HP Laser Jet 4" as default printer and checked "LPT1" in printer properties of "HP Laser Jet 4". Print spooler is also running (Windows 7).

The problem is hPort always returns INVALID_HANDLE_VALUE.

I don't have a real printer. Is it a problem?

#include "stdafx.h"
#include <windows.h>

int main()
{
    HANDLE hPort = CreateFile(_T("LPT1:"), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if(hPort != INVALID_HANDLE_VALUE) {
        printf("success");
    } else {
        printf("%i", GetLastError());
    }    
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风向决定发型 2024-11-05 02:33:41

为什么你尝试打开 LPT 端口。这对你来说真的有必要吗?
如果操作系统中安装了打印机(例如 HP Laser Jet 4 或任何其他打印机),并且您想直接写入而不使用打印机驱动程序,则可以写入数据(例如,从文件),如下所示:

TCHAR *pPrinterName = TEXT("HP Laser Jet 4");
TCHAR *pFileName = TEXT("c:\filename.prn");
HANDLE hPrinter = NULL;
DOC_INFO_1 docinfo;
FILE *pfile = NULL;
DWORD dwBytesWritten = 0L;
BYTE data[1024];
DWORD dwCount = 0L;

if (OpenPrinter(pPrinterName, &hPrinter, NULL))
{
  docinfo.pDocName = TEXT("RAW Output Document Name");
  docinfo.pOutputFile = NULL;
  docinfo.pDatatype = TEXT("RAW");

  DWORD dwPrtJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&docinfo);

  if (dwPrtJob > 0)
  {
    if (0 == _tfopen_s(&pfile, pFileName, TEXT("rb")))
    {
      while (!feof(pfile))
      {
        dwCount = (DWORD)fread(&data, 1, 1024, pfile);
        WritePrinter(hPrinter, &data, dwCount, &dwBytesWritten);
      }
      fclose(pfile);
    }
  }
  EndDocPrinter(hPrinter);
  ClosePrinter(hPrinter);
}

可能,此代码有一些错误。我不测试它。我只是从我的一个旧项目中剪掉了它。

以同样的方式,您可以将文件(包含打印机命令 - 例如,用于 HP PCL5/PCL6 打印机的 PCL/PJL)发送到任何打印机/端口。

Why you try for open LPT port. This is realy need for you?
If printer (e.g. HP Laser Jet 4 or any other) installed in OS and you want to write to it directly without using printer driver, you can write data (from file, for example) as showed bellow:

TCHAR *pPrinterName = TEXT("HP Laser Jet 4");
TCHAR *pFileName = TEXT("c:\filename.prn");
HANDLE hPrinter = NULL;
DOC_INFO_1 docinfo;
FILE *pfile = NULL;
DWORD dwBytesWritten = 0L;
BYTE data[1024];
DWORD dwCount = 0L;

if (OpenPrinter(pPrinterName, &hPrinter, NULL))
{
  docinfo.pDocName = TEXT("RAW Output Document Name");
  docinfo.pOutputFile = NULL;
  docinfo.pDatatype = TEXT("RAW");

  DWORD dwPrtJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&docinfo);

  if (dwPrtJob > 0)
  {
    if (0 == _tfopen_s(&pfile, pFileName, TEXT("rb")))
    {
      while (!feof(pfile))
      {
        dwCount = (DWORD)fread(&data, 1, 1024, pfile);
        WritePrinter(hPrinter, &data, dwCount, &dwBytesWritten);
      }
      fclose(pfile);
    }
  }
  EndDocPrinter(hPrinter);
  ClosePrinter(hPrinter);
}

May be, this code has some errors. I am do not test it. I am simply cut it from one my old project.

In same manner you can send file (that contains printer commands - PCL/PJL for HP PCL5/PCL6 printers, for example) to any printer/port.

雨后彩虹 2024-11-05 02:33:41

问题是 hPort 始终返回 INVALID_HANDLE_VALUE。

我没有真正的打印机。有问题吗?

如果您没有真正的 LPT1 (LPT2...) 端口,但未安装打印机设备或打印机驱动程序,则这是一个问题。

检查设备管理器或 PC 硬件,LPT1 端口是否确实存在。

对于真正出现的 LPT1 端口,您的代码必须返回“成功”。

如果 LPT1 没有真正出现在您的 PC 中,您的代码 ( GetLastError() ) 必须返回 2 ( ERROR_FILE_NOT_FOUND )。

The problem is hPort always returns INVALID_HANDLE_VALUE.

I don't have a real printer. Is it a problem?

This is a problem, if you do not have a real LPT1 (LPT2...) ports, but not a printer device or printer driver installed.

Check in device manager or PC-hardware, if LPT1 port is really present.

For really presented LPT1 port, you code must return "success"

If LPT1 does not really presented in you PC you code ( GetLastError() ) must return 2 ( ERROR_FILE_NOT_FOUND ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文