|
Try to this
int ii = 20;
char tmp[32] = "loveing";
printf("[%d]hello[%s]\n",ii, tmp);
//no integer fill the %d format
char szData[52] = {0};
//sprintf( buffer, "\tString: %s\n", s );
sprintf(szData,"[%d]saying \n",ii);
//no string fill the %s format
printf("%s", szData);
|
|
|
|
|
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
|
|
|
|
|
I really wanna say what a kind person you are!
|
|
|
|
|
fantasy1215 wrote: The compiler don't complain in the compile phase, but when you press F5 to debug, 'unhandle exception' prompt. while not debug, run it at once, you just get wield strings or output.
We all use some sort of printf or CString::Format in the code, we are not careful sometimes. So how to handle this problem or check out the bugs in the exist project?
I've had some bugs due to wrong usage of sscanf and fscanf . VC6 didn't report stack corruption errors but VC8 does! Here is a blog post on these bugs, hope this helps!
http://nibuthomas.wordpress.com/2008/08/01/careful-with-sscanf-and-fscanf/[^]
I like printf , sprintf and other functions but at we times use these in a wrong way resulting in bugs. Reading the docs carefully, again carefully, "on the format specifiers to be used for different types" will help a lot in solving and finding these bugs!
Just now found one bug with the usage of the TRACE macro. One of the trace messages contained a '%' sign resulting in application disappearance. AtlTrace treats the first argument given as a format specifier IMO hence the bug. Replaced TRACE with TRACE0 macro and now it's fine.
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
fantasy1215 wrote: We all use some sort of printf or CString::Format in the code, we are not careful sometimes. So how to handle this problem
Being careful.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hai Dear,
How to create a color on "Group Box Caption Text and Border". Thanks to all.
Best Regards,
Selvan.S.
|
|
|
|
|
Please go through this link[^].
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
You must be use of WM_CTLCOLOR.
|
|
|
|
|
how can i get path of project in vc++6
my project folder name is "NOVA"
user can change the name of folder and can put this folder on any directry E,D, or C any one
i want to know path when this project execute
onInItDlg() i want to store full path in a variable
how this can perform?
|
|
|
|
|
ani_ikram wrote: i want to know path when this project execute
onInItDlg()
GetModuleFileName[^]
|
|
|
|
|
use GetModuleFileName;
code like:
TCHAR szPathBuf[MAX_PATH] = {0};
GetModuleFileName(NULL, szPathBuf, MAX_PATH);
then the buffer 'szPathBuf' contains the full path including the exe file name.
you can printf the buffer to see it.
you may need _tsplitpath too.
include the needed header files. and add the needed libs!
|
|
|
|
|
Hi all,
I am getting the error:
error C3861: 'SetWorkingDirectory': identifier not found
I have already included #include <shobjidl.h>
Is there anything I lack?
Is it defining cplusplus?
If so how do i do that?
Thanks
Jayjay
|
|
|
|
|
monsieur_jj wrote: error C3861: 'SetWorkingDirectory': identifier not found
Here is the answer in google's way ...
There is no such function. Did you mean SetCurrentDirectory[^]?
|
|
|
|
|
Did you see Requirements for SetCurrentDirectory?
Requirements
Client
Requires Windows Vista, Windows XP, or Windows 2000 Professional.
Server
Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
Header
Declared in WinBase.h; include Windows.h.
Library
Use Kernel32.lib.
DLL
Requires Kernel32.dll.
Unicode
Implemented as SetCurrentDirectoryW (Unicode) and SetCurrentDirectoryA (ANSI).
|
|
|
|
|
When I Click a row of DataGrid control,What message occurs?
I wanna export the data of a row of the DataGrid control to the excel.I think when I click or doubleclick the row,a Message would occur.What is the Message?
|
|
|
|
|
Does this[^] help?
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
Hello,
Here is the issue.
I have a win32 dialog on which there are some controls and I have call back method defined for handling the messages. One of the control is a button. Once the button is clicked and released, WM_COMMAND is received by the call back method. But as i need to handle button click and button release messages seperately i used Subclassing. I defined one more call back method for this button and now i am able to receive button down and button up messages.
There is a shortcut assigned to this button. On pressing the shortcut key, WM_COMMAND is received by the window call back but not the control call back. On releasing no message is sent to any of the call backs. Is there a way to enable the framework to send appropriate message to the control call back...
Please let me know if you need more details.
Thanks in advance,
Hari krishna.
|
|
|
|
|
Adarapu Harikrishna wrote: I defined one more call back method for this button and now i am able to receive button down and button up messages.
you mean Lbuttondown and LbuttonUp ?? Its better to handle the BM_CLICK message. It will be fired in all cases.
|
|
|
|
|
Thanks Naveen for your reply. I need any sort of message to the control call back on pressing the shortcut key and also on the release of the same. Right now it goes to the dialog call back as WM_COMMAND once the shortcut key is pressed.
|
|
|
|
|
Did you try handling BM_CLICK message in the button's call back function..?
|
|
|
|
|
Hi Experts,
I have a CListCtrl with LVS_EX_CHECKBOXES style but if I check/uncheck the item from list how can get the index of it?
I also used LVN_ITEMCHANGED and NM_CLICK but could not get the selected item index.
|
|
|
|
|
MPTP wrote: how can get the index of it?
Have you had a look at the dozens of CodeProject Articles about using CListCtrl?
led mike
|
|
|
|
|
|
I have never used MSXML, so bear with me. I have a small C++ app that receives a message via another app. The message received is XML format but is contained in a string and not passed as an XML doc. Stuck with this, nothing I can do about this part.
So, I need to implement MSXML, I am hoping this is the right path, to load the string into an XML doc and parse through it. Does anyone have or know of a simple example of how to do this? The xml string I receive only has 2 elements, so this does not have to be a very involved example with lots of bells and whistles. Just looking for the basics.
BTW, I am using MSXML 6.0.
Thanks a lot in advance for the help.
Troy Schenk
|
|
|
|
|
MSXML implements an XML DOM and maybe also a SAX parser. You can go study how those things work at web sites like TOP XML and www.w3schools.com
I also believe you will find sample code using MSXML at w3schools. Now those samples might be in something like Javascript but that doesn't matter because the COM interfaces are the same when you use them from C++. Of course if you don't have experience using COM interfaces from C++ then you will also need to learn about that.
led mike
|
|
|
|