Thread: How can I find out why my function won't work?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    96

    How can I find out why my function won't work?

    I'm writing a php extension that allows users on an Intranet to print documents. PHP, and therefore the extension is written in c.

    I had this just about wrapped up when a new error cropped up.
    It's on a line that has always worked, now it doesn't.

    I've trapped errors everywhere I think there might be a problem. No errors in the extension are thrown at all.

    When I run the php file I get an http 500 error.
    The line that causes the problem is in: printer_set_option under the VALID_OPTIONS case.
    The line is:
    Code:
    SetPrinter(resource->handle,2,(LPBYTE)resource->pi2,0)
    If I rem SetPrinter out the 500 error goes away.
    If in the php I rem out this line:
    Code:
    printer_set_option($hndlPrinter , PTR7_VALID_OPTIONS, 0);
    the 500 error goes away.

    You can see previous threads on this extension:
    Link 1
    Link 2
    Link 3

    Here is all the code that is pertinent:
    printer_set_option
    Code:
    PHP_FUNCTION(printer_set_option)
    {
    	DWORD dwNeeded =0;
    	PRINTER_DEFAULTS pd;
    	zval *arg1, *arg2, *arg3;
    	printer *resource;
    	
    	long bfrCopies = 0;
    	long bfrOrientation = 0;
    	long bfrPaperSource = 0;
    	long bfrPaperSize = 0;			
    	char sRtn[500];
    
    	if( zend_get_parameters_ex(3, &arg1, &arg2, &arg3) == FAILURE ) {
    		WRONG_PARAM_COUNT;
    	}
    
    	if ((resource = (printer *)zend_fetch_resource_ex(arg1, "Printer Handle", le_printer)) == NULL) 
    	{
    		RETURN_FALSE;
    	}
    	convert_to_long_ex(arg2);
    
    	memset(&pd,0,sizeof(pd));
    	pd.DesiredAccess = PRINTER_ALL_ACCESS;
    	SetLastError(0);
    
    	switch(Z_LVAL_P(arg2)) {
    		case COPIES:
    			convert_to_long_ex(arg3);
    			resource->pi2->pDevMode->dmCopies=(short)Z_LVAL_P(arg3);
    			resource->dmModifiedFields|=DM_COPIES;
    			break;
    
    		case MODE:
    			convert_to_string_ex(arg3);
    			if (resource->info.lpszDatatype) {
    				efree((char *)resource->info.lpszDatatype);
    			}
    			resource->info.lpszDatatype		= estrdup(Z_STRVAL_P(arg3));
    			resource->info.cbSize			= sizeof(resource->info);
    			break;
    
    		case TITLE:
    			convert_to_string_ex(arg3);
    			if (resource->info.lpszDocName) {
    				efree((char *)resource->info.lpszDocName);
    			}
    			resource->info.lpszDocName		= estrdup(Z_STRVAL_P(arg3));
    			resource->info.cbSize			= sizeof(resource->info);
    			break;
    
    		case OUTPUT_FILE:
    			convert_to_string_ex(arg3);
    			if (resource->info.lpszOutput) {
    				efree((char *)resource->info.lpszOutput);
    			}
    			resource->info.lpszOutput		= estrdup(Z_STRVAL_P(arg3));
    			resource->info.cbSize			= sizeof(resource->info);
    			break;
    
    		case ORIENTATION:
    			convert_to_long_ex(arg3);
    			resource->pi2->pDevMode->dmOrientation=(short)Z_LVAL_P(arg3);
    			resource->dmModifiedFields|=DM_ORIENTATION;
    			break;
    
    		case YRESOLUTION:
    			convert_to_long_ex(arg3);
    			resource->pi2->pDevMode->dmYResolution = (short)Z_LVAL_P(arg3);
    			resource->dmModifiedFields|=DM_YRESOLUTION;
    			break;
    		
    		case XRESOLUTION:
    			convert_to_long_ex(arg3);
    			resource->pi2->pDevMode->dmPrintQuality= (short)Z_LVAL_P(arg3);
    			resource->dmModifiedFields|=DM_PRINTQUALITY;
    			break;
    			
    		case PAPER_BIN:
    			convert_to_long_ex(arg3);
    			resource->pi2->pDevMode->dmDefaultSource = (short)Z_LVAL_P(arg3);
    			resource->dmModifiedFields|=DM_DEFAULTSOURCE;
    			break;
    
    		case PAPER_SIZE:
    			convert_to_long_ex(arg3);
    			resource->pi2->pDevMode->dmPaperSize	= (short)Z_LVAL_P(arg3);
    			resource->dmModifiedFields|=DM_PAPERSIZE;
    			break;
    
    		case PAPER_LENGTH:
    			convert_to_long_ex(arg3);
    			resource->pi2->pDevMode->dmPaperLength = (short)(Z_LVAL_P(arg3) * 10);
    			resource->dmModifiedFields|=DM_PAPERLENGTH;
    			break;
    
    		case PAPER_WIDTH:
    			convert_to_long_ex(arg3);
    			resource->pi2->pDevMode->dmPaperWidth	= (short)(Z_LVAL_P(arg3) * 10);
    			resource->dmModifiedFields |=  DM_PAPERWIDTH;
    			break;
    
    		case SCALE:
    			convert_to_long_ex(arg3);
    			resource->pi2->pDevMode->dmScale		= (short)Z_LVAL_P(arg3);
    			resource->dmModifiedFields |= DM_SCALE;
    			break;
    
    		case BG_COLOR:
    			convert_to_string_ex(arg3);
    			SetBkColor(resource->dc, hex_to_rgb(Z_STRVAL_P(arg3)));
    			break;
    
    		case TEXT_COLOR:
    			convert_to_string_ex(arg3);
    			SetTextColor(resource->dc, hex_to_rgb(Z_STRVAL_P(arg3)));
    			break;
    
    		case TEXT_ALIGN:
    			convert_to_string_ex(arg3);
    			SetTextAlign(resource->dc, Z_LVAL_P(arg3));
    			break;
    			
    		case VALID_OPTIONS:					
    			resource->pi2->pSecurityDescriptor=NULL;
    			resource->pi2->pDevMode->dmFields=resource->dmModifiedFields;
    			resource->dmModifiedFields=0;
    			if(DocumentProperties(NULL,resource->handle,resource->name,resource->pi2->pDevMode,resource->pi2->pDevMode,DM_IN_BUFFER | DM_OUT_BUFFER) < 0)
    			{
    				php_error_docref(NULL TSRMLS_CC, E_WARNING, "printer.set.option VALID_OPTIONS DocumentProperties failed: [%d]", GetLastError());
    				RETURN_FALSE;
    			}
    			if(ResetDC(resource->dc, resource->pi2->pDevMode) == NULL)
    			{
    				php_error_docref(NULL TSRMLS_CC, E_WARNING, "printer.set.option VALID_OPTIONS ResetDC failed: [%d]", GetLastError());
    				RETURN_FALSE;
    			}
    			if(SetPrinter(resource->handle,2,(LPBYTE)resource->pi2,0)==0) //THIS IS THE PROBLEM LINE
    			{
    				php_error_docref(NULL TSRMLS_CC, E_WARNING, "printer.set.option VALID_OPTIONS SetPrinter failed: [%d]", GetLastError());
    				RETURN_FALSE;
    			}
    			SendMessageTimeout(HWND_BROADCAST,WM_DEVMODECHANGE,0L,(LPARAM)resource->name,SMTO_NORMAL,1000,NULL);					
    			break;
    			
    		default:
    			php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown option passed to printer_set_option()");
    			RETURN_FALSE;
    	}
    
    	RETURN_TRUE;
    }
    printer_open
    Code:
    PHP_FUNCTION(printer_open)
    {
    	zval *arg1;
    	printer *resource;
    
    	int argc = ZEND_NUM_ARGS();
    
    	resource = (printer *)emalloc(sizeof(printer));
    	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 = (LPDEVMODE *)emalloc(DocumentProperties(NULL, resource->handle, 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));
    		}
    		else
    		{
    			php_error_docref(NULL TSRMLS_CC, E_WARNING, "printer.open: could not set DEVMODE. [%s]", resource->name);
    			RETURN_FALSE;
    		}				
    	}
    	else {
    		php_error_docref(NULL TSRMLS_CC, E_WARNING, "printer.open: could not connect to the printer [%s]", resource->name);
    		RETURN_FALSE;
    	}
    	RETURN_TRUE
    }
    php that uses these functions
    Code:
    $hndlPrinter = printer_open($PrinterName);
    printer_set_option($hndlPrinter , PTR7_ORIENTATION, $Orientation);
    printer_set_option($hndlPrinter, PTR7_PAPER_BIN, $nBin);	
    printer_set_option($hndlPrinter , PTR7_PAPER_SIZE, $PaperSizeNum);
    printer_set_option($hndlPrinter , PTR7_COPIES, $nCopies);
    
    //If I rem this line out the 500 error goes away.
    printer_set_option($hndlPrinter , PTR7_VALID_OPTIONS, 0);	
    
    printer_close($hndlPrinter);

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    96
    This is solved. I never found the problem but started again with an older file and worked my way back to functional.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,059
    Quote Originally Posted by MAtkins View Post
    This is solved. I never found the problem but started again with an older file and worked my way back to functional.
    Version control - Wikipedia, the free encyclopedia

    Using 'git' as an example:
    You would make a commit every time your latest edit compiles.
    You would give it a label every time your latest effort passes the test suite (you do have some kind of auto test right?)
    You would make a branch every time you decide to do something "experimental".
    When your experiment works, you can merge it into the main line. If the experiment fails, you can just delete the branch.

    With a version control package, you can do things like
    - jump to any revision
    - compare any two revisions (say a working and a broken)

    The point is, you're never going to be far away (usually just one or two commands) from recovering something which works.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where to find projects to work on?
    By SuperMiguel in forum Tech Board
    Replies: 5
    Last Post: 07-14-2012, 05:49 AM
  2. help to find some function
    By desimoos in forum C Programming
    Replies: 2
    Last Post: 05-28-2012, 10:54 AM
  3. If you've begun work RECENTLY, how did you find your way in?
    By wildcard_seven in forum General Discussions
    Replies: 30
    Last Post: 03-08-2012, 11:58 PM
  4. help can't find a way to make this work
    By Pe6r0 in forum C Programming
    Replies: 10
    Last Post: 12-04-2006, 06:08 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread