|
Hi all,
i m trying to read a Text file using CStdioFile,but this is fail and read some garbage values whan the file is encoded with other than ANSI format like Unicode,UTF-8 etc.
please tell me before start reading how can i identify the file is not in ANSI format.
i m waiting for ur valuable suggestions.
thanks in advance.
|
|
|
|
|
It depends,
For some file formats there is no way to tell them apart programatically (e.g. Chinese Big5 vs. Ansi).
I imagine you are more interested in unicode specifically, though. For Unicode files there is normally a BOM (byte order mark) at the beginning of the file which tells you what the file format is. Look up Byte order marks on wikipedia for a really good explanation.
Alternatively if you don't want to do this by hand, read this article[^] for a CStdiofile derived class that auto-detects the encoding.
|
|
|
|
|
See here[^] for explanations of how to identify different encoded files.
|
|
|
|
|
I use this way to read the binary buffers from the SQL's image field type.
the m_pBuffer's value is 0xFF all the same int the Debug Mode / Watch.
But I use the SQL Query Analyzer, it can be found. why is it ?
the filed info:
Version varchar(100)
Buffer image(16)
the code fragment:
BOOL CTestDlg::ReadBinary()
{
try
{
OleInitialize(NULL);
CString strTemp = _T("");
CString strDat = _T("");
CString strVer = _T("");
char *pBuf = NULL;
_bstr_t bstrsource = "select * from db_somedatease";
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open(bstrsource,_variant_t((IDispatch*)m_pConnection,TRUE),adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset->MoveFirst();
while(VARIANT_FALSE == m_pRecordset->adoEOF)
{
HANDLE m_hFile = INVALID_HANDLE_VALUE;
HANDLE m_hMapping = INVALID_HANDLE_VALUE;
DWORD m_dwSize = 0;
char *m_pSrc = NULL;
char *m_pBuffer = NULL;
_variant_t vtVer = m_pRecordset->GetCollect("Version");
VARIANT varBLOB;
strVer = VariantToCString(vtVer);
strTemp.Format(_T("%s\\Dat\\%s.tmp"),GetExePath(),strVer);
m_dwSize = m_pRecordset->GetFields()->GetItem("Buffer")->ActualSize;
if (m_dwSize > 0)
{
m_hFile = CreateFile(strTemp,GENERIC_ALL,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(m_hFile == INVALID_HANDLE_VALUE)
{
CString strError = _T("");
strError.Format(_T("CreateFile Failed!Error Code: %d"),GetLastError());
::MessageBox(NULL,strError,NULL,NULL);
return FALSE;
}
m_hMapping = CreateFileMapping(m_hFile,NULL,PAGE_READWRITE,0,m_dwSize,NULL);
if(m_hMapping == NULL)
{
CString strError = _T("");
strError.Format(_T("CreateFileMapping Failed!Error Code: %d"),GetLastError());
::MessageBox(NULL,strError,NULL,NULL);
CloseHandle(m_hFile);
return FALSE;
}
m_pSrc = (char *)MapViewOfFile(m_hMapping,FILE_MAP_ALL_ACCESS,0,0,0);
if(m_pSrc == NULL)
{
CString strError = _T("");
strError.Format(_T("MapViewOfFile Failed!Error Code: %d"),GetLastError());
::MessageBox(NULL,strError,NULL,NULL);
CloseHandle(m_hMapping);
CloseHandle(m_hFile);
}
memset(m_pSrc,0xFF,m_dwSize);
VariantInit(&varBLOB);
varBLOB = m_pRecordset->GetFields()->GetItem("Buffer")->GetChunk(m_dwSize);
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
if(m_pBuffer = new char[m_dwSize + 1])
{
memset(m_pBuffer,0x00,m_dwSize);
SafeArrayAccessData(varBLOB.parray,(void **)m_pBuffer);
memcpy(m_pSrc,m_pBuffer,m_dwSize);
UnmapViewOfFile(m_pSrc);
CloseHandle(m_hMapping);
CloseHandle(m_hFile);
SafeArrayUnaccessData(varBLOB.parray);
delete [] m_pBuffer;
m_pBuffer = NULL;
}
}
}
m_pRecordset->MoveNext();
}
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("read version error!\r\n\r\nerror code:『%s』",e.ErrorMessage());
AfxMessageBox(errormessage);
return FALSE;
}
return TRUE;
}
if I use this way to read ,it is normal.
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
memcpy(m_pSrc,varBLOB.parray->pvData,m_dwSize);
UnmapViewOfFile(m_pSrc);
CloseHandle(m_hMapping);
CloseHandle(m_hFile);
SafeArrayUnaccessData(varBLOB.parray);
VariantClear(&varBLOB);
}
because it doesn't use the SafeArrayAccessData, but is this way safety ?
thansk for your reply !
Best Regards !
modified on Friday, April 8, 2011 10:46 PM
|
|
|
|
|
Hi ,
I have created a shared memory using CreateSharedMemory () and mapping is done using MapViewOfFile () API. Is it possible to map only some part of Shared Memory? Does DWORD dwOffSetHigh and DWORD dwOffsetLow (i.e 3rd and 4th Parameter of MapViewOfFile () Method) need to be modified considering Allocation Granularity. Currently I can see the System granularity as 65535 using GetSystemInfo ().
Thanks
|
|
|
|
|
The offset values are a 64-bit value split into two 32-bit ones, and as far as I know, can map any portion of a file. Did you encounter any problems?
|
|
|
|
|
what are the valid registry setting for calling CoCreateInstanceAsAdmin()?
Regards,
Vishal
|
|
|
|
|
|
consulted but still no success.
when using CLSCTX_INPROC_SERVER CoCreateInstance() returns S_OK
but when using CLSCTX_LOCAL_SERVER CoCreateInstance() Fails.
Regards,
Vishal
|
|
|
|
|
vishalgpt wrote: Fails.
That does not help much; tell us what failure you see, try looking up the meaning of any error codes you get.
|
|
|
|
|
HRESULT hr = CoCreateInstance(CLSID_CExample,NULL,CLSCTX_LOCAL_SERVER,IID_CExample,(void**)&inf);
hr value is E_NOINTERFACE
No Such Interface Supported.
But if CLSCTX_INPROC_SERVER is used then the value of hr is S_OK
sorry for posting twice.
Regards,
Vishal
|
|
|
|
|
See here[^] for the reason.
|
|
|
|
|
|
Hi everyone . I front with a problem . I have an custom CComboBox control , named CComboBoxExt , which I use it into an SDI application on CFormView .
CComboBoxExt m_Combo;
From an button , I call :
m_Combo.SetCurSel(2);
but on this call , I want to set up something in CComboBoxExt , but I didn't know how to catch this event in control ?
I try every hadlers :
void CComboBoxExt::OnSelendok()
{
TRACE("\n selok \n");
}
or
LRESULT CComboBoxExt::OnSelchange()
{
TRACE("\n sel change \n");
return 0;
}
but in vain ... can you help me , please ? Thank you !
|
|
|
|
|
I try in this way too , but nothing :
BOOL CComboBoxExt::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == CB_SETCURSEL)TRACE("\n aaaa \n");
return CComboBox::PreTranslateMessage(pMsg);
}
|
|
|
|
|
I think you need to capture the CBN_SELCHANGE[^] notification for the ComboBox . My MFC skills are extremely rusty so I cannot remember exactly how you code this in your class.
|
|
|
|
|
Well , as I said above , I try this :
LRESULT CComboBoxExt::OnSelchange()
{
TRACE("\n sel change \n");
return 0;
}
but with no effect , I mean , on
void CTestComboView::OnButton4()
{
m_Combo.SetCurSel(2);
}
I have not TRACE ....
|
|
|
|
|
I think you just overide the SetCurSel in your CComboBoxExt class.So when you call like this m_Combo.SetCurSel(2); your overidden function will be called.
int CComBoExt::SetCurSel(int index)
{
return ::SendMessage(m_hWnd, CB_SETCURSEL, WPARAM(index), 0);
}
Also if you Can handle CB_SETCURSEL message with ON_MESSAGE in your CComboBoxExt
|
|
|
|
|
I try this :
public:
int SetCurSel(int nSelect);
and
BEGIN_MESSAGE_MAP(CComboBoxExt, CComboBox)
.....
ON_MESSAGE(CB_SETCURSEL,SetCurSel)
END_MESSAGE_MAP()
int CComboBoxExt::SetCurSel(int nSelect)
{
TRACE("\n bingo \n");
return ::SendMessage(m_hWnd, CB_SETCURSEL, WPARAM(nSelect), 0);
}
but give an error :
unhandled exception on output.c , line 336 ...
|
|
|
|
|
You have to handle CB_SETCURSEL Separately when you want to catch the CB_SETCURSEL this is general case.But in your case where you are calling m_Combo.SetCurSel(2), You just override the SetCurSel function.So first comment the line ON_MESSAGE(CB_SETCURSEL,SetCurSel) and then check now.
|
|
|
|
|
I think it's OK now !! Thank you all very much !!!
|
|
|
|
|
Flaviu 2 wrote: From an button , I call
m_Combo.SetCurSel(2);
but on this call , I want to set up something in CComboBoxExt , but I didn't know how to catch this event in control ?
Why not just "set up something" before calling SetCurSel() ?
"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
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
I want to setup an standard behaviour to my CComboBox control , so user no need take care of ... and it's about an protected member to set up ...
modified on Friday, April 8, 2011 2:11 PM
|
|
|
|
|
CODE: because my english is so bad;
in onbutton;
CString tempstr1;
CString tempstr2;
getdlgitemText(comboBoxID,tempstr1);
m_comboBox.setCursel(2);
getdlgitemText(comboBoxID,tempstr2);
if(tempstr1 != tempstr2)
{
TRACE("CHANGE");
}
i don't know that it is ok;
you try;
|
|
|
|
|
This is not working because I could have identical string with different item data associated ...
|
|
|
|
|