我正尝试使用C++ Windows. h的打印假脱机程序API创建一个AddForm函数。我还没有找到任何C++示例或无法找出解决方案,所以我想寻求您的帮助。
我的代码应该与此图片中的代码相同:
源材料
- https://learn.microsoft.com/en-us/windows/win32/printdocs/addform
- https://learn.microsoft.com/en-us/windows/win32/api/windef/ns-windef-size
- https://www.codeproject.com/Articles/12229/Adding-Custom-Paper-Sizes-to-Named-Printers-2
- Use PrintSpoolerAPI function SetForm() in 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.
}
**问题:**代码看起来不错,但实际上什么也没发生。
谢谢大家的回复!
1条答案
按热度按时间lp0sw83n1#
为什么要定义自己的FORM_INFO_1?只需使用Windows提供的头文件:
然后修正与定义的差异:
然后修复使用C样式强制转换时出现的错误:
或者C++-style cast: