Print Spooler API - AddForm example

242 Views Asked by At

I am trying to make a AddForm function using C++ Windows.h's Print Spooler API. I haven't find any C++ examples or couldn't figure out solution, so I wanted ask your help.

My code should done same as in this picture:

My code should done same as in this picture

Source Material

Code input:

#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.
}

Problem: The code looks good, but nothing really happens.

Thank you all for your responses!

1

There are 1 best solutions below

2
On

Why are you defining your own FORM_INFO_1? Just use the header provided by Windows:

#include "Winspool.h"

Then fix the differences from your definition:

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;

Then fix the error you're getting with a C-style cast:

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

Or a C++-style cast:

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