Click here to Skip to main content
15,894,180 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Expression in C Pin
Richard MacCutchan7-May-14 22:10
mveRichard MacCutchan7-May-14 22:10 
QuestionHow to check a DateTimeCtrl field is dirty or not in MFC Pin
sma123#7-May-14 12:47
sma123#7-May-14 12:47 
GeneralRe: How to check a DateTimeCtrl field is dirty or not in MFC Pin
Richard MacCutchan7-May-14 22:08
mveRichard MacCutchan7-May-14 22:08 
AnswerRe: How to check a DateTimeCtrl field is dirty or not in MFC Pin
David Crow9-May-14 3:35
David Crow9-May-14 3:35 
QuestionScreensaver: keyboard and mouse hook and kill screensaver Pin
Drugodrf7-May-14 7:34
Drugodrf7-May-14 7:34 
AnswerRe: Screensaver: keyboard and mouse hook and kill screensaver Pin
Drugodrf9-May-14 11:20
Drugodrf9-May-14 11:20 
GeneralRe: Screensaver: keyboard and mouse hook and kill screensaver Pin
Drugodrf10-May-14 5:18
Drugodrf10-May-14 5:18 
QuestionDocumentProperties() cause a First-chance exception Pin
Drugodrf7-May-14 7:17
Drugodrf7-May-14 7:17 
In an application i wrote years ago, i need to have a handle to printer propertiers device mode structure; i searched documents and i finded this function (DocumentProperties()); i searched example, i tried to use it, i writed code and the application works yet.
Now i have to write similar application and i note when i call this function, it cause a first-chance exception (only when i call it to know the size of buffer); so i try with the old application and also that cause the exception; i try to solve it, but the the example i find and the documentation i have tell me that the use of the function i make it's correct; the funny fact is this method works very well in release mode and with the old application the customer don't have problems.
The code is this:

C++


BOOL PrinterSettings(LPTSTR pszPrinterName, BOOL bSet)
{
	if (_tcslen(pszPrinterName) <= 0)
		return FALSE;

	if (bSet && theApp.m_hBarcodePrinterDev == NULL)
		return FALSE;

	PRINTER_DEFAULTS pd;
	DWORD			 dwNeeded;
	LONG			 lFlag;
	HANDLE			 hPrinter     = NULL;
	DEVMODE FAR*	 pDevMode     = NULL;
	PRINTER_INFO_2*	 pPrintInfo2  = NULL;
	BOOL			 bReturn      = TRUE;

	ZeroMemory(&pd, sizeof(pd));
    pd.DesiredAccess = PRINTER_ALL_ACCESS;

	bReturn = OpenPrinter(pszPrinterName, &hPrinter, &pd);

	if (bReturn)
	{
		SetLastError(0);
		bReturn = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
		
		if (!bReturn && (GetLastError() == ERROR_INSUFFICIENT_BUFFER) && dwNeeded > 0)
			bReturn = TRUE;
	}

	if (bReturn && (dwNeeded <= 0))
		bReturn = FALSE;

	if (bReturn)
		pPrintInfo2 = (PRINTER_INFO_2 *) GlobalAlloc (GPTR, dwNeeded);
    
	if (bReturn && pPrintInfo2 == NULL)
		bReturn = FALSE;

	if (bReturn)
		bReturn = GetPrinter(hPrinter, 2, (LPBYTE) pPrintInfo2, dwNeeded, &dwNeeded);

	if (bReturn && pPrintInfo2->pDevMode == NULL)
	{
                // Here cause exception
		dwNeeded = DocumentProperties(NULL, hPrinter, pszPrinterName, NULL, NULL, 0);
    
		if (dwNeeded <= 0)
			bReturn = FALSE;
    
		if (bReturn)
			pDevMode = (DEVMODE FAR*) GlobalAlloc(GHND, dwNeeded);
    
		if (bReturn && pDevMode == NULL)
			bReturn = FALSE;
    
		if (bReturn)
		{
			lFlag = DocumentProperties(NULL, hPrinter, pszPrinterName, pDevMode, NULL,DM_OUT_BUFFER);
    
			if (lFlag != IDOK || pDevMode == NULL)
				bReturn = FALSE;
		}

		if (bReturn)
			pPrintInfo2->pDevMode = pDevMode;
	}

	if (bReturn && pPrintInfo2->pDevMode == NULL)
		bReturn = FALSE;

	if (bReturn && bSet)
	{	
		pPrintInfo2->pDevMode = (DEVMODE FAR*)::GlobalLock(theApp.m_hBarcodePrinterDev);

		DWORD dwAttrib = pPrintInfo2->Attributes;
		dwAttrib &= ~(PRINTER_ATTRIBUTE_WORK_OFFLINE);
		pPrintInfo2->Attributes = dwAttrib;

		lFlag = DocumentProperties(NULL, hPrinter, pszPrinterName, pPrintInfo2->pDevMode,
									pPrintInfo2->pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);

		if (lFlag != IDOK)
			bReturn = FALSE;

		if (bReturn)
			bReturn = SetPrinter(hPrinter, 2, (LPBYTE) pPrintInfo2, 0);

		GlobalUnlock(theApp.m_hBarcodePrinterDev);
	}

	if (bReturn && !bSet)
	{
		GlobalFree(theApp.m_hBarcodePrinterDev);

                // Here cause exception
		dwNeeded = DocumentProperties(NULL, hPrinter, pszPrinterName, NULL, NULL, 0);
    
		if (dwNeeded <= 0)
			bReturn = FALSE;
    
		if (bReturn)
			theApp.m_hBarcodePrinterDev = ::GlobalAlloc(GPTR, dwNeeded);
    
		if (bReturn && theApp.m_hBarcodePrinterDev == NULL)
			bReturn = FALSE;
    
		if (bReturn)
		{
			lFlag = DocumentProperties(NULL, hPrinter, pszPrinterName,
										(DEVMODE FAR*) theApp.m_hBarcodePrinterDev, NULL, DM_OUT_BUFFER);
    
			if (lFlag != IDOK || theApp.m_hBarcodePrinterDev == NULL)
				bReturn = FALSE;
		}

		if (bReturn && theApp.m_hBarcodePrinterDev == NULL)
			bReturn = FALSE;
	}

    if (pPrintInfo2)
        GlobalFree(pPrintInfo2);

	if (pDevMode)
        GlobalFree(pDevMode);

    if (hPrinter)
        ClosePrinter(hPrinter);
    
	return bReturn;
};


Something can help me ?

Thanks

Drugo
QuestionRe: DocumentProperties() cause a First-chance exception Pin
Richard MacCutchan7-May-14 22:06
mveRichard MacCutchan7-May-14 22:06 
AnswerRe: DocumentProperties() cause a First-chance exception Pin
Drugodrf7-May-14 23:13
Drugodrf7-May-14 23:13 
GeneralRe: DocumentProperties() cause a First-chance exception Pin
Richard MacCutchan7-May-14 23:21
mveRichard MacCutchan7-May-14 23:21 
GeneralRe: DocumentProperties() cause a First-chance exception Pin
Drugodrf9-May-14 2:04
Drugodrf9-May-14 2:04 
GeneralRe: DocumentProperties() cause a First-chance exception Pin
Richard MacCutchan9-May-14 2:19
mveRichard MacCutchan9-May-14 2:19 
GeneralRe: DocumentProperties() cause a First-chance exception Pin
Drugodrf9-May-14 3:03
Drugodrf9-May-14 3:03 
QuestionSendMessageCallback() - what is execution context of callback function Pin
Derell Licht7-May-14 7:07
professionalDerell Licht7-May-14 7:07 
AnswerRe: SendMessageCallback() - what is execution context of callback function Pin
CPallini8-May-14 0:04
mveCPallini8-May-14 0:04 
GeneralRe: SendMessageCallback() - what is execution context of callback function Pin
Derell Licht8-May-14 5:02
professionalDerell Licht8-May-14 5:02 
QuestionCompiling C and C++ files together Pin
AmbiguousName7-May-14 6:59
AmbiguousName7-May-14 6:59 
AnswerRe: Compiling C and C++ files together Pin
Richard MacCutchan7-May-14 7:15
mveRichard MacCutchan7-May-14 7:15 
AnswerRe: Compiling C and C++ files together Pin
leon de boer15-May-14 4:44
leon de boer15-May-14 4:44 
QuestionWSACleanup delays when called how can I get around this? Pin
Member 43367557-May-14 6:18
Member 43367557-May-14 6:18 
AnswerRe: WSACleanup delays when called how can I get around this? Pin
Gisle Vanem8-May-14 23:30
Gisle Vanem8-May-14 23:30 
GeneralRe: WSACleanup delays when called how can I get around this? Pin
Member 433675510-May-14 5:50
Member 433675510-May-14 5:50 
QuestionMCI QUESTION!!! Is it different file extension name and codec? Pin
AmissU6-May-14 21:00
AmissU6-May-14 21:00 
QuestionCompiling VLC in Visual Studio 2013 Pin
Django_Untaken5-May-14 5:32
Django_Untaken5-May-14 5:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.