Thanks Salem. You've been here a *long* time and you helped me with something unrelated to this about 4 or 5 years ago.
The code here is real old code, not written by me. It may have worked once but not now.
Clearly, I'm not a c programmer. I'm trying to learn fast though)
I need this extension to work for a client.
I am compiling in VS 14 (2015) but I'm running nmake set up for php7.
I don't know if I'm compiling in UNICODE or not.
I've changed emalloc to ecalloc which is supposed to initialize with 0. I'm not sure if I'm using it right though.
I should add that the header for my extension defines the printer.name (resource->name) as LPTSTR, so I'm not sure what you mean by 'wide string'. UNICODE?
Code:
typedef struct
{
HANDLE handle;
LPTSTR name;
DOCINFO info;
HDC dc;
PRINTER_INFO_2 *pi2;
DWORD dmModifiedFields;
} printer;
It compiled OK and I tested it.
No change, it still returns the defaults of the printer, not the changes I made.
Here is the new code:
Code:
case VALID_OPTIONS:
resource->pi2->pSecurityDescriptor=NULL;
resource->pi2->pDevMode->dmFields=resource->dmModifiedFields;
resource->dmModifiedFields=0;
DocumentProperties(NULL,resource->handle,resource->name,resource->pi2->pDevMode,resource->pi2->pDevMode,DM_IN_BUFFER | DM_OUT_BUFFER);
//DocumentProperties(NULL,resource->handle,resource->name,NULL,resource->pi2->pDevMode,DM_IN_BUFFER);
SetPrinter(resource->handle,2,(LPBYTE)resource->pi2,0);
//SendMessageTimeout(HWND_BROADCAST,WM_DEVMODECHANGE,0L,(LPARAM)(LPCSTR)resource->name,SMTO_NORMAL,1000,NULL);
SendMessageTimeout(HWND_BROADCAST,WM_DEVMODECHANGE,0L,(LPARAM)resource->name,SMTO_NORMAL,1000,NULL);
//DocumentProperties(NULL,resource->handle,resource->name,resource->pi2->pDevMode,NULL,DM_OUT_BUFFER);
sprintf(sRtn, "Printer Options Set: \r\nCopies: %lu \r\nOrientation: %lu \r\nPaper Bin: %lu \r\nPaper Size: %lu",
resource->pi2->pDevMode->dmCopies,
resource->pi2->pDevMode->dmOrientation,
resource->pi2->pDevMode->dmDefaultSource,
resource->pi2->pDevMode->dmPaperSize);
break;
Code:
PHP_FUNCTION(printer_open)
{
zval *arg1;
printer *resource;
size_t DevModeSize;
size_t PrinterSize = sizeof(printer);
int argc = ZEND_NUM_ARGS();
//resource = (printer *)emalloc(sizeof(printer));
resource = (printer *)ecalloc(PrinterSize, PrinterSize);
resource->dmModifiedFields = 0;
if( argc == 1 && zend_get_parameters_ex(1, &arg1) != FAILURE ) {
convert_to_string_ex(arg1);
resource->name = Z_STRVAL_P(arg1);
}
else if( argc == 0 ) {
resource->name = PRINTER7_G(default_printer);
}
else {
WRONG_PARAM_COUNT;
}
/*
if (OpenPrinter(resource->name, &resource->handle, NULL) != 0) {
resource->pi2 = (PRINTER_INFO_2 *)emalloc(sizeof(PRINTER_INFO_2));
resource->pi2->pDevMode = (DEVMODE *)emalloc(DocumentProperties(NULL, NULL, resource->name, NULL, NULL, 0));
if (DocumentProperties(NULL, resource->handle, resource->name, resource->pi2->pDevMode, NULL, DM_OUT_BUFFER) == IDOK) {
resource->info.lpszDocName = estrdup("PHP generated Document");
resource->info.lpszOutput = NULL;
resource->info.lpszDatatype = estrdup("TEXT");
resource->info.fwType = 0;
resource->info.cbSize = sizeof(resource->info);
resource->dc = CreateDC(NULL, resource->name, NULL, resource->pi2->pDevMode);
RETURN_RES(zend_register_resource(resource, le_printer));
}
}
*/
if (OpenPrinter(resource->name, &resource->handle, NULL) != 0) {
DevModeSize = DocumentProperties(NULL, NULL, resource->name, NULL, NULL, 0);
resource->pi2 = (PRINTER_INFO_2 *)ecalloc(sizeof(PRINTER_INFO_2),sizeof(PRINTER_INFO_2));
resource->pi2->pDevMode = (DEVMODE *)ecalloc(DevModeSize,DevModeSize);
if (DocumentProperties(NULL, resource->handle, resource->name, resource->pi2->pDevMode, NULL, DM_OUT_BUFFER) == IDOK) {
resource->info.lpszDocName = estrdup("PHP generated Document");
resource->info.lpszOutput = NULL;
resource->info.lpszDatatype = estrdup("TEXT");
resource->info.fwType = 0;
resource->info.cbSize = sizeof(resource->info);
resource->dc = CreateDC(NULL, resource->name, NULL, resource->pi2->pDevMode);
RETURN_RES(zend_register_resource(resource, le_printer));
}
}
else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "couldn't connect to the printer [%s]", resource->name);
RETURN_FALSE;
}
}