Click here to Skip to main content
15,914,795 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: LPARAM problem Pin
Joe Woodbury21-Aug-08 19:46
professionalJoe Woodbury21-Aug-08 19:46 
Questioncall OnCancel of Dialog from a thread in the dialog Pin
ptr_Electron21-Aug-08 18:47
ptr_Electron21-Aug-08 18:47 
AnswerRe: call OnCancel of Dialog from a thread in the dialog Pin
Jijo.Raj21-Aug-08 19:41
Jijo.Raj21-Aug-08 19:41 
QuestionHow do I edit a combo box's list-box item in-place ? Pin
SherTeks21-Aug-08 18:12
SherTeks21-Aug-08 18:12 
AnswerRe: How do I edit a combo box's list-box item in-place ? Pin
Hamid_RT21-Aug-08 18:18
Hamid_RT21-Aug-08 18:18 
Questionprintf like function question! Pin
fantasy121521-Aug-08 17:58
fantasy121521-Aug-08 17:58 
AnswerRe: printf like function question! Pin
Hamid_RT21-Aug-08 18:14
Hamid_RT21-Aug-08 18:14 
AnswerRe: printf like function question! [modified] Pin
Joe Woodbury21-Aug-08 19:07
professionalJoe Woodbury21-Aug-08 19:07 
Sorry for the long post, but here's my solution (there is a companion wide string set of functions as well.) The DT... is my library prefix.

There are similar looking functions in Microsoft safe string library, but I was able to cause every one of them to throw exceptions in common scenarios. This set of code had to run in a DLL which logged errors so it simple couldn't throw exceptions (though I suppose it could in very fringe cases.)

#include <stdarg.h>

inline
char* DTSafeStrCopyLen(LPSTR pDst, LPCSTR pSrc, int len)
{
	if (pDst)
	{
		if (pSrc && len > 0)
		{
			while (len && *pSrc)
			{
				*pDst++ = *pSrc++;
				len--;
			}
		}
		*pDst = 0;
	}
	return pDst;
}

inline
int DTSafeStrCopyLen2(LPSTR pDst, LPCSTR pSrc, int len)
{
	return DTSafeStrCopyLen(pDst, pSrc, len) - pDst;
}

#pragma warning(disable:4702) // unreachable code

int DTSafeFormatStringV(LPSTR pBuffer, int bufferLen, LPCSTR pFormat, va_list args)
{
	if (!pBuffer || bufferLen <= 0)
		return -1;

	int returnLen = 0;

	if (pFormat && *pFormat)
	{
#if _MSC_VER >= 8
		__try
#else
		try
#endif
		{
			returnLen = _vsnprintf(pBuffer, bufferLen, pFormat, args);
			if (returnLen < 0)
			{
				pBuffer[bufferLen - 1] = 0;
				returnLen = bufferLen - 1;
			}
		}
#if _MSC_VER >= 8
		__except (EXCEPTION_EXECUTE_HANDLER)
#else
		catch (...)
#endif
		{
			returnLen = DTSafeStrCopyLen2(pBuffer, "!exception thrown during formatting: \"", bufferLen);
			returnLen += DTSafeStrCopyLen2(&pBuffer[returnLen], pFormat, bufferLen - returnLen); 

			if (returnLen < bufferLen - 1)
			{
				pBuffer[returnLen++] = '"';
				pBuffer[returnLen] = 0;
			}
		}
	}
	else
	{
		pBuffer[0] = 0;
	}

	return returnLen;
}

int DTSafeFormatString(LPSTR pBuffer, int bufferLen, LPCSTR pFormat, ...)
{
	if (!pBuffer || bufferLen <= 0)
		return -1;

	int returnLen = 0;

	if (pFormat && *pFormat)
	{
		va_list args;
		va_start (args, pFormat);
		returnLen = DTSafeFormatStringV(pBuffer, bufferLen, pFormat, args);
		va_end (args);
	}
	else
	{
		pBuffer[0] = 0;
	}

	return returnLen;
}



Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke


modified on Friday, August 22, 2008 1:47 AM

GeneralRe: printf like function question! Pin
fantasy121524-Aug-08 14:39
fantasy121524-Aug-08 14:39 
AnswerRe: printf like function question! Pin
Nibu babu thomas21-Aug-08 20:10
Nibu babu thomas21-Aug-08 20:10 
AnswerRe: printf like function question! Pin
CPallini21-Aug-08 21:38
mveCPallini21-Aug-08 21:38 
QuestionVC++ MFC Pin
Selvan.S21-Aug-08 17:34
Selvan.S21-Aug-08 17:34 
AnswerRe: VC++ MFC Pin
Jijo.Raj21-Aug-08 18:11
Jijo.Raj21-Aug-08 18:11 
AnswerRe: VC++ MFC Pin
Hamid_RT21-Aug-08 18:18
Hamid_RT21-Aug-08 18:18 
Questionpath from where project is executing Pin
ani_ikram21-Aug-08 17:00
ani_ikram21-Aug-08 17:00 
AnswerRe: path from where project is executing Pin
Naveen21-Aug-08 17:43
Naveen21-Aug-08 17:43 
AnswerRe: path from where project is executing Pin
fantasy121521-Aug-08 17:47
fantasy121521-Aug-08 17:47 
Questionquick question about SetWorkingDirectory Pin
monsieur_jj21-Aug-08 16:36
monsieur_jj21-Aug-08 16:36 
AnswerRe: quick question about SetWorkingDirectory Pin
Naveen21-Aug-08 16:56
Naveen21-Aug-08 16:56 
AnswerRe: quick question about SetWorkingDirectory Pin
Hamid_RT21-Aug-08 18:20
Hamid_RT21-Aug-08 18:20 
QuestionWhen I Click a row of DataGrid control,What message occurs? Pin
aygrhnwms30421-Aug-08 14:58
aygrhnwms30421-Aug-08 14:58 
AnswerRe: When I Click a row of DataGrid control,What message occurs? Pin
_AnsHUMAN_ 21-Aug-08 18:32
_AnsHUMAN_ 21-Aug-08 18:32 
QuestionNot able to receive messages to the control procedure on invoking the shortcut keys. Pin
Hari-Adarapu21-Aug-08 10:08
Hari-Adarapu21-Aug-08 10:08 
AnswerRe: Not able to receive messages to the control procedure on invoking the shortcut keys. Pin
Naveen21-Aug-08 14:19
Naveen21-Aug-08 14:19 
GeneralRe: Not able to receive messages to the control procedure on invoking the shortcut keys. Pin
Hari-Adarapu22-Aug-08 4:08
Hari-Adarapu22-Aug-08 4:08 

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.