As I said before, you are specifying a buffer size of 3, so any number of characters greater than that will cause the error. Check the documentation[^] for specific details of the parameters and their values.
One of these days I'm going to think of a really clever signature.
The second parameter of sprintf_s as used here is the maximum allowed length of the output buffer. If the buffer is of size 50 and you want to write starting in element 3 then the obvious size remaining is 'sizeof(buffer) - offset' and not 'offset'.
It fails because the memory allocated by GlobalAlloc is not enough. See, how you use sizeof(BITMAPINFO) + w * h * 3 value to allocate memory, and then you are trying to calculate the m_dwSizeImage value, which is much greater then requested.
You have got two options:
Either move this chunk of code after you have calculated the image size, so that your code becomes:
Code snippet below. The SeekToBegin fails because the CFile object has an invalid file handle. But the open returns true, so the code proceeds to die an ugly death. The code is called within a tight loop passing through a couple of hundred files. It feels like I'm overrunning the hard drive or file system, but I have no idea how I might do that... any thoughts are greatly appreciated.
// in a loop of many files....
// do some compression.
// now open a temp file to dump raw data into:
FilePath = (CString)directoryBuf;
FilePath += (CString)"\\uncompressed.unc";
if(UncompFile.Open(FilePath, CFile::shareExclusive | CFile::modeCreate | CFile::modeReadWrite), &fe)
cmpCRC = helper.ComputeFileCRC( FilePath );
if(uncCRC != cmpCRC)
ErrorString = (CString)infoStruct->sourceFileName + "\n failed CRC test.";
Charlie Gilley You're going to tell me what I want to know, or I'm going to beat you to death in your own house.
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
modified 10-Oct-12 13:11pm.
Last Visit: 31-Dec-99 18:00 Last Update: 8-Mar-14 21:13