www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Windows printing

reply frame <frame86 live.com> writes:
Is there some Windows expert or someone you can tell me if I do 
things wrong?

I can open the printer and also print but I'm failing to restore 
the printer settings from a file.

```d
import core.sys.windows.windows;
import core.sys.windows.winspool;
import std;

pragma(lib, "winspool.lib");

extern (Windows) BOOL IsValidDevmodeA(PDEVMODEA pDevmode, size_t 
DevmodeSize);

// ...

string path; // contains a valid path to a previously saved 
printer properties struct file
char* cPrinterName; // just to simplify, it comes from toStringz 
and contains the correct name

const(ubyte)[] pDevModeInputBuf = cast(ubyte[]) read(path); // 
reads 1689 bytes
DEVMODEA* initData = cast(DEVMODEA*) pDevModeInputBuf.ptr;

HANDLE pHandle;
OpenPrinterA(cPrinterName, &pHandle, null); // success, pHandle 
points to something

// returns the bytes needed for the printer properties
long docRs = DocumentPropertiesA(null, pHandle, cPrinterName, 
null, null, 0); // 1689 bytes, so also correct

// everyting correct:
// initData.dmDeviceName == cPrinterName
// initData.dmSize + initData.dmDriverExtra == docRs, 1689 bytes

IsValidDevmodeA(initData, docRs); // true

// just to be sure
SetLastError(0); // GetLastError() is 0

// and now this fails:
uint flags = DM_IN_BUFFER;
docRs = DocumentPropertiesA(null, pHandle, cPrinterName, null, 
initData, flags);

// docRs is -1
// GetLastError() is 131
// 131 = ERROR_NEGATIVE_SEEK
```

Negative seek? This is file related - what's going on here?
Oct 06 2021
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 7 October 2021 at 01:55:15 UTC, frame wrote:
 Is there some Windows expert or someone you can tell me if I do 
 things wrong?

 [...]
Maybe I'm reading this wrong, but don't you want DM_OUT_BUFFER?
Oct 07 2021
parent reply frame <frame86 live.com> writes:
On Thursday, 7 October 2021 at 15:38:27 UTC, Imperatorn wrote:
 On Thursday, 7 October 2021 at 01:55:15 UTC, frame wrote:
 Is there some Windows expert or someone you can tell me if I 
 do things wrong?

 [...]
Maybe I'm reading this wrong, but don't you want DM_OUT_BUFFER?
No, please see: https://docs.microsoft.com/en-us/windows/win32/printdocs/documentproperties The output of mode DM_OUT_BUFFER is what I am tyring to import again.
Oct 07 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 7 October 2021 at 17:13:45 UTC, frame wrote:
 On Thursday, 7 October 2021 at 15:38:27 UTC, Imperatorn wrote:
 On Thursday, 7 October 2021 at 01:55:15 UTC, frame wrote:
 Is there some Windows expert or someone you can tell me if I 
 do things wrong?

 [...]
Maybe I'm reading this wrong, but don't you want DM_OUT_BUFFER?
No, please see: https://docs.microsoft.com/en-us/windows/win32/printdocs/documentproperties The output of mode DM_OUT_BUFFER is what I am tyring to import again.
Oh, now I see. Sorry. I think that might depend on OS version iirc. Sidenote: shouldn't you use the unicode variant? 🤔
Oct 07 2021
next sibling parent frame <frame86 live.com> writes:
On Thursday, 7 October 2021 at 18:00:17 UTC, Imperatorn wrote:

 Oh, now I see. Sorry. I think that might depend on OS version 
 iirc. Sidenote: shouldn't you use the unicode variant? 🤔
Why? It shouldn't matter for this purpose.
Oct 07 2021
prev sibling parent reply frame <frame86 live.com> writes:
On Thursday, 7 October 2021 at 18:00:17 UTC, Imperatorn wrote:

 I think that might depend on OS version iirc.
Well, I guess you are right. If I just put the DocumentPropertiesA() call in a loop, it locks in immediately with 0 or between 60 - 400 failed attempts, depending on the printer driver. Very weird behavior but It cannot caused by my program - the input data in memory is constant and not corrupted. Also interesting the amount of failed attempts stays exactly the same or just moves a bit but is never really random.
Oct 11 2021
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Monday, 11 October 2021 at 17:59:39 UTC, frame wrote:
 On Thursday, 7 October 2021 at 18:00:17 UTC, Imperatorn wrote:

 I think that might depend on OS version iirc.
Well, I guess you are right. If I just put the DocumentPropertiesA() call in a loop, it locks in immediately with 0 or between 60 - 400 failed attempts, depending on the printer driver. Very weird behavior but It cannot caused by my program - the input data in memory is constant and not corrupted. Also interesting the amount of failed attempts stays exactly the same or just moves a bit but is never really random.
Interesting. Have you tried any sanity checking using any other language or using some existing code? Like, maybe you can use wmi for this? idk
Oct 11 2021
prev sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 7 October 2021 at 01:55:15 UTC, frame wrote:
 Is there some Windows expert or someone you can tell me if I do 
 things wrong?

 [...]
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printerconfiguration
Oct 12 2021
parent frame <frame86 live.com> writes:
On Tuesday, 12 October 2021 at 17:36:06 UTC, Imperatorn wrote:
 On Thursday, 7 October 2021 at 01:55:15 UTC, frame wrote:
 Is there some Windows expert or someone you can tell me if I 
 do things wrong?

 [...]
https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printerconfiguration
I need interoperability with the printer driver dialogs. I had a look on C++ examples but don't see huge differences. It should just work. I assume I would need to use a printer driver debug framework to find the issue but I don't know where to start but also have no time to debug this. Thank you anyway.
Oct 12 2021