I am looking for a method to change the Page Layout (Landscape or Portrait) property of a printer programmatically.

I got this piece of code from somewhere which seems to run as expected. It sets the DEVMODE structure and exits without error. But when I check the property of the printer, it hasn't changed could somebody please verify if this code runs properly on their machines.
If you notice any flaws or errors do let me know

Code:
#include "stdafx.h"
#include <windows.h>

LPDEVMODE GetLandscapeDevMode(HWND hWnd, char *pDevice)
{
	HANDLE      hPrinter;
	LPDEVMODE   pDevMode;
	DWORD       dwNeeded, dwRet;

	/* Start by opening the printer */
	if (!OpenPrinter(pDevice, &hPrinter, NULL))
	   return NULL;
	
	/*
	* Step 1:
	* Allocate a buffer of the correct size.
	*/
	dwNeeded = DocumentProperties(hWnd,
	   hPrinter,       /* Handle to our printer. */
	   pDevice,        /* Name of the printer. */
	   NULL,           /* Asking for size, so */
	   NULL,           /* these are not used. */
	   0);             /* Zero returns buffer size. */
	pDevMode = (LPDEVMODE)malloc(dwNeeded);

	/*
	* Step 2:
	* Get the default DevMode for the printer and
	* modify it for your needs.
	*/
	dwRet = DocumentProperties(hWnd,
	   hPrinter,
	   pDevice,
	   pDevMode,       /* The address of the buffer to fill. */
	   NULL,           /* Not using the input buffer. */
	   DM_OUT_BUFFER); /* Have the output buffer filled. */
	if (dwRet != IDOK)
	{
	   /* If failure, cleanup and return failure. */
	   free(pDevMode);
	   ClosePrinter(hPrinter);
	   return NULL;
	}

	/*
	*	Make changes to the DevMode which are supported.
	*/
	if (pDevMode->dmFields & DM_ORIENTATION)
	{
	   /* If the printer supports paper orientation, set it.*/
	   pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
	}
	/*
	* Step 3:
	* Merge the new settings with the old.
	* This gives the driver an opportunity to update any private
	* portions of the DevMode structure.
	*/
	dwRet = DocumentProperties(hWnd,
	   hPrinter,
	   pDevice,
	   pDevMode,       /* Reuse our buffer for output. */
	   pDevMode,       /* Pass the driver our changes. */
	   DM_IN_BUFFER |  /* Commands to Merge our changes and */
	   DM_OUT_BUFFER); /* write the result. */

	/* Finished with the printer */
	ClosePrinter(hPrinter);

	if (dwRet != IDOK)
	{
	   /* If failure, cleanup and return failure. */
	   free(pDevMode);
	   return NULL;
	}

	/* Return the modified DevMode structure. */
	return pDevMode;

}
				
int main(void)
{
	GetLandscapeDevMode(NULL,"Ghostscript PDF");
	return 0;
}