|
The affected program text was writing to a .BMP graphics file.
It used fopen() and fwrite(), and it acted odd. (It always behaved correctly under Windows Vista; I now have Windows 8.).
Just now I changed it to using _sopen_s() and _write(), and FOR NOW it seems to be behaving correctly.
The text that writes to the .BMP file is now this:-
if(_sopen_s(&fil,out,_O_RDWR|_O_CREAT|_O_BINARY, _SH_DENYNO, _S_IREAD|_S_IWRITE)) {
M="I can't open this file"; goto BAD;}
for(i=0;i<256;i++) {BM[54+i*4]=BM[54+i*4+1]=BM[54+i*4+2]=(char)i; BM[54+i*4+3]=0;} /* palettes */
BM[0]='B'; BM[1]='M'; *((long*)(BM+2))=(54+1024+ly*lxx)&0x7fffffff; /* file size */
BM[6]=BM[7]=BM[8]=BM[9]=0;
BM[10]=54; BM[11]=4; BM[12]=BM[13]=0; /* 54+1024 = data offset, little-endian */
BM[14]=40; BM[15]=BM[16]=BM[17]=0;
*((long*)(BM+18))=lx; *((long*)(BM+22))=ly; /* image size */ BM[26]=1; BM[27]=0; /* nplanes*/
BM[28]=8; BM[29]=0; /* bpp */
BM[30]=BM[31]=BM[32]=BM[33]=0; /* no compression */
*((long*)(BM+34))=ly*lxx; /* image size */
BM[38]=100; BM[39]=BM[40]=BM[41]=0; BM[42]=100; BM[43]=BM[44]=BM[45]=0; /* resolution/inch */
BM[46]=0; BM[47]=1; BM[48]=BM[49]=BM[50]=0; BM[51]=1; BM[52]=BM[53]=0; /* 256 = ncolors'es */
BM[5]&=0x7f; _write(fil,BM,54+1024); _write(fil,D,ly*lxx); _close(fil);
sprintf(BM,"I have created this texture map file, it should have %d bytes",54+1024+ly*lxx);
MessageBox(wn,out,BM,MB_OK); BAD: FF(D); FF(DI); goto DEF;}
modified 16-May-13 6:02am.
|
|
|
|
|
That should make no difference, since all you are doing is opening the file with sharing enabled. This has nothing to do with your variables getting corrupted in memory. The fact that it works now, is more luck than judgement. And given all the index values to your arrays, it's highly likely that you are overruning or otherwis writing in the wrong place in your buffer.
BTW it makes it much easier for us to read your code if you format your code with proper indents, and place it between <pre> tags, like:
if(_sopen_s(&fil,out,_O_RDWR|_O_CREAT|_O_BINARY, _SH_DENYNO, _S_IREAD|_S_IWRITE))
{
M="I can't open this file";
goto BAD;
}
for(i = 0; i < 256; i++)
{
BM[54+i*4] = BM[54+i*4+1] = BM[54+i*4+2] = (char)i;
BM[54+i*4+3] = 0;
}
Use the best guess
|
|
|
|
|
Anthony Appleyard wrote: and malloc()'ed storage stay in the same place through a run of a program?
I doubt that concept even exists in Vista although if you can educate me otherwise I would like to see something about it. Specifically what options you set in the compiler to get it to do that.
Excluding perhaps some of the most basic parts of the OS and perhaps, only perhaps, so very low level drivers everything else is relocatable because that is how virtual memory works. I suppose there might be a way to change the relocation address or whatever it is called and then if one uses no threads and fixed input data then one run to the next would map the same. But it still can move in physical memory when it runs.
|
|
|
|
|
hello,
I tried googling for an algorithm to compress/encrypt a shor fixed size string from 52 characters down to 40 but can't seem to find any.
Target strings are random alphanumeric [A-Z0-9]
e.g "M5KS07VHN2X42JCY1PFHE1ZZGI2XUBDFAKQBEPFB7CH4SECXHJXL"
I have tried huffman and smaz (https://github.com/antirez/smaz") and both inflated to size of the original string.
Does anyone know a good algorith for such purpose?
Thanks,
|
|
|
|
|
The characters in the given range are hex 0x30 to 0x5A. When subtracting 0x30, you will get a range from 0 to 0x2A which can be represented by 6 bits (there is also space for handling null when subtracting 0x29). The range can be additionally reduced by subtracting different values for digits and letters. When using 6 bits, you can compress the data to 52 / 8 * 6 = 39 byte. All you have to do is subtracting the offset from each character and shifting the 6 bits into your output buffer.
|
|
|
|
|
Try LZ4 (http://code.google.com/p/lz4/[^]). It's great, though I don't know how much it would help you since that's a very short string and not very compressible as is.
|
|
|
|
|
If your input characters are all 0-127 ASCII, you could store 9 characters in 8 bytes. It only amounts to about a 12.5% savings (which does not meet your requirement), but it's easy enough to code up.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
I was just wondering, but have you made any progress with this?
|
|
|
|
|
yes. the answer is base64. (since all characters are alphanumeric, they can be encoded in 6 bits instead of 8)
btw, thank you all for your answers.
|
|
|
|
|
I want to check the checkboxes in a CLIstCtrl object, on a CDialog based application:
void CTestListCtrlDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(0 <= pNMListView->iItem)
m_List.SetCheck(pNMListView->iItem, TRUE);
*pResult = 0;
}
when I made a click on a list item, the checkbox of the clicked item will check, but when I try to check the checkbox from check box itself (made a click inside of checkbox), it does not check it ... why ? It will be checked only from a double-click ... what I'm doing wrong ?
|
|
|
|
|
The list control handles clicks on the check box itself. Whenever a click on the box occurs, the check is toggled. When now clicking on a check box, your handler will set the check and the default handler which is called afterwards will toggle the check so that it is removed. This can be verified when you click on a marked check box: The check marker disappears.
The solution would be to do nothing in your handler when clicking on the box. To determine the width of the check box, use ImageList_GetIconSize passing the image list returned by GetImageList(LVSIL_STATE) . The check box itself is drawn with some spacing so that it may be necessary to add some pixels to the width.
|
|
|
|
|
But I am already do nothing in checking box handler ... or not ? The only method that I had used is:
void CTestListCtrlDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(0 <= pNMListView->iItem)
m_List.SetCheck(pNMListView->iItem, TRUE);
*pResult = 0;
}
is the only thing that I treat it ...
|
|
|
|
|
You are setting the check marker when clicking on a row. That is more than nothing.
I will try to explain in more detail what happens:
When you click on a row right of a check box:
Your handler is called and sets the check.
When you click on a check box:
Your handler is called and sets the check and then the internal check box click handler is called and toggles the check.
What I mean with do nothing: When clicking on the check box, your handler should not set the check.
|
|
|
|
|
Yes, I understand now ... I'm getting harder ... thanks ! You are trying to say that when I click on checkbox (inside of checkbox) I will do nothing ... remain the question, how can I know if I had click inside of checkbox, or not ? I will start digging ....
|
|
|
|
|
The passed struct to the click handler is of type NMITEMACTIVATE . This struct contains the ptAction member indicating where the click occurs. You must check if the x position is right of the check box (see my first answer on how to determine the width of the check box). As far as I remember, the width is 13 pixels by default. But there is also some spacing between the left border and the check box. To make it more complicat, you must also check if the list is scrolled horizontically.
|
|
|
|
|
I think that I had solved :
void CTestListCtrlDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
const CPoint pt = pNMListView->ptAction;
const BOOL bCurrentCheckState = m_List.GetCheck(pNMListView->iItem);
if(0 <= pNMListView->iItem && 15 < pt.x)
m_List.SetCheck(pNMListView->iItem, ! bCurrentCheckState);
*pResult = 0;
}
I don't know if is OK, but seems to work ...
|
|
|
|
|
Fine that it is solved. So it seems I was right with 13 pixels width when there are 2 pixels spacing.
|
|
|
|
|
I want to kindly thank you ! Best wishes ! Bye !
|
|
|
|
|
Look the fragment of this code. I wanted to ask, why Draw text to static bitmap does not work.
m_bmpModulation.CreateBitmap(xOrder, yOrder, 1, 24, (void *)lpBits);
if (!::IsWindow(m_staModulation.m_hWnd))
m_staModulation.Create(_T("A bitmap static control (A)"), SS_CENTERIMAGE | SS_BITMAP | WS_VISIBLE, CRect(20, 20, 20 + xOrder, 20 + yOrder), this);
m_staModulation.SetBitmap((HBITMAP)m_bmpModulation);
delete [] lpBits;
CDC *pDC = m_staModulation.GetDC();
if (pDC)
{
int nIldMin, nIldMax;
swscanf(set.m_strModulationIldMin, L"%d", &nIldMin);
swscanf(set.m_strModulationIldMax, L"%d", &nIldMax);
int x, y;
x = 218 - (int)(nIldMin * (float)197 / (float)295);
y = 0;
pDC -> DrawText(L"Min", 3, CRect(y, x, y + 20, x + 13), 0);
x = 218 - (int)(nIldMax * (float)197 / (float)295);
pDC -> DrawText(L"Max", 3, CRect(y, x - 13, y + 21, x), 0);
float fFrequency;
CString str;
str = set.m_strModulationFrequency;
str.Replace(L",", L".");
swscanf(str, L"%f", &fFrequency);
str.Format(L"%.02f", (double)1 / fFrequency);
pDC -> DrawText(str, str.GetLength(), CRect(290, 224, 330, 236), 0);
}
else
AfxMessageBox(L"No Device Context.");
}
I think, problem is, that drawed text is under the bitmap. Any suggestion how to fix it?
modified 15-May-13 3:37am.
|
|
|
|
|
|
Hi, everyone.
I am finding solution which can use to read Excel format into MFC. I have questions in next 2cases. Answer me please.
case 1. Without MS Office package.
Although Excel isn't installed in my computer, I must read excel file into my MFC program. How can I parse this format without execl's COM interface?
case 2. importing Excel version is different.
I installed Excel 2007 into my computer, but I must read Excel 2010 file into my MFC program. If MS Office were installed in my computer, we can use to read Excel format into MFC using Excel's open COM interface. But, when supporting version is different from my version, how can I solve this problem?
|
|
|
|
|
|
You can use ODBC driver for Excel and read a data from Excel sheet by database functions (CRecordSet etc.).
|
|
|
|
|
There are four ways to read and write Excel files:
- Using Excel Automation (COM / OLE Dispatch) with the Excel type library. This requires an installed Excel on the machine running your application and the Excel type library on the development machine.
- Using ODBC. Open Database Connectivity is an operating system independent standard for database management. With Excel ODBC drivers, it can be used to read and write Excel files. Because it is a database interface, it handles sheets from an Excel file like tables in a database file. The required Excel ODBC driver is installed by the Windows Setup.
- Using ADO (OLE DB). ActiveX Data Objects is like ODBC a database interface, handling sheets from an Excel file like tables in a database file. It is more up-to-date than ODBC. This requires the ADO DLL wich is installed by Windows Setup. ADO may be also used to connect to Excel files using the ODBC drivers using a different connect string.
- Using a library that provides reading and writing Excel files.
Examples are:
- BasicExcel [^],
- ExcelFormat Library [^] (based on BasicExcel),
- LibXL (commercial) [^].
When deciding to use ODBC or ADO, ADO is preferred because the ODBC drivers has some limitations due to bugs and ADO supports accessing fields by value while ODBC only supports text strings.
To use ODBC and ADO with Excel 2007 files (XLSX), the 'Microsoft Access Database Engine 2010 Redistributable' package must be installed.
To exchange data with Excel, two additional methods using text formats can be used:
- Creating Text, CSV, RTF, or HTML files that can be opened by Excel and other spreadsheet programs or save data from within Excel in one of these formats.
- Providing Text, CSV, RTF, or HTML data for clipboard and OLE Drag&Drop operations.
With Excel 2007, the new OpenXML based file formats (mainly XLSX, XLSB, XLSM) has been introduced. Later versions use the same format. Older Office versions including the viewers can use the new format when installing the Microsoft Office Compatibility Pack.
|
|
|
|
|
Now say a very big thankyou to those two previous excellent answers....
==============================
Nothing to say.
|
|
|
|
|