c++ 打印后台处理程序API - AddForm示例

u59ebvdq  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(103)

我正尝试使用C++ Windows. h的打印假脱机程序API创建一个AddForm函数。我还没有找到任何C++示例或无法找出解决方案,所以我想寻求您的帮助。

我的代码应该与此图片中的代码相同:

源材料

#include <iostream>
#include "Windows.h"
#include "Winspool.h"

using namespace std;

int main() {

    LPHANDLE hPrinter = NULL;
    OpenPrinter(
        NULL,
        hPrinter,
        NULL
    );  // No critical error here: "hPrinter could be "0"

    FORM_INFO_1 exampleform;
    exampleform.Flags = 0;
    exampleform.pName = (LPWSTR)"0A"; // No critical error here: "Cast between semantically different string types. Use of invalid string can lead to undefined behaviour."
    exampleform.Size.cx = 1260 * 1000;
    exampleform.Size.cy = 891 * 1000;
    exampleform.ImageableArea.left = 0;
    exampleform.ImageableArea.top = 0;
    exampleform.ImageableArea.right = exampleform.Size.cx;
    exampleform.ImageableArea.bottom = exampleform.Size.cy;

    cout << exampleform.pName; //output as: "00007FF68EFA9C24"

    AddForm(hPrinter, 1, reinterpret_cast<LPBYTE>(&exampleform)); // No critical error here : "hPrinter could be "0"

    //C:\Users\XXX\source\repos\paperApp\x64\Debug\paperApp.exe (process 16792) exited with code 0.
}

**问题:**代码看起来不错,但实际上什么也没发生。

谢谢大家的回复!

lp0sw83n

lp0sw83n1#

为什么要定义自己的FORM_INFO_1?只需使用Windows提供的头文件:

#include "Winspool.h"

然后修正与定义的差异:

FORM_INFO_1 exampleform;
exampleform.Flags = 0;
exampleform.pName = (LPWSTR)"MyName";
exampleform.Size.cx = 1260 * 1000;
exampleform.Size.cy = 891 * 1000;
exampleform.ImageableArea.left = 0;
exampleform.ImageableArea.top = 0;
exampleform.ImageableArea.right = exampleform.Size.cx;
exampleform.ImageableArea.bottom = exampleform.Size.cy;

然后修复使用C样式强制转换时出现的错误:

test = AddForm(hPrinter, 1, (LPBYTE)&exampleform);

或者C++-style cast

test = AddForm(hPrinter, 1, reinterpret_cast<LPBYTE>(&exampleform));

相关问题