I have an application in evc++, i need to read text file and return what i have read. The problem it’s that in the debug mode i see the text in the file under &strSN and i need to return CString (strSN it’s TCHAR). I don’t know how to convert it.
Please HELP!.
Thanks in advance. Guillermo.
HANDLE hInFile;
TCHAR strSN[65];
DWORD dwBytesRead;
GENERIC_READ,FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
hInFile = CreateFile(TEXT("\\windows\\Device_ID.txt"), GENERIC_READ,FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hInFile == INVALID_HANDLE_VALUE)
{
m_strCompaqIpaqId = _T("");
m_strErrorMessage += _T("Cannot read \\windows\\Device_ID.txt file.");
return TEXT("CreateFile");
}
memset(strSN, 0, 64 * sizeof(TCHAR));
ReadFile(hInFile, &strSN, 64, &dwBytesRead, NULL);
CloseHandle(hInFile);
return CString(strSN);

Convertion issue on embedded vc++ 4.0
Rabarbers
You'd need to use MultiByteToWideChar() to convert whatever encoding you're using in your text file to Unicode.
http://msdn.microsoft.com/library/default.asp url=/library/en-us/wcesdk40/html/cerefmultibytetowidechar.asp
eddieg
This is the code that has worked finally! Thanks!
CString CUtils::GetSerialNumber()
{
#ifdef UNICOUSUARIO
return L"EMULATOR";
#else
#ifdef _DEBUG
return L"EMULATOR";
#else
CString m_strCompaqIpaqId, m_strErrorMessage;
// Start CreateAssetFile.exe
PROCESS_INFORMATION pi;
if (!::CreateProcess(TEXT("\\windows\\DeviceID2003.exe"),
NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi)) // DeviceID.exe esta hecho en .NET y deja un archivo DeviceID.TXT con el DeviceID de la PocketPC en el mismo directorio donde se ejecuta.
{
/*
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),0, // Default language
(LPTSTR) &lpMsgBuf,0,NULL);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
MessageBox(NULL, (LPCTSTR)"Ahora viene el error", 0, MB_OK | MB_ICONINFORMATION );
MessageBox(NULL, (LPCTSTR)lpMsgBuf, 0, MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
*/
m_strCompaqIpaqId = _T("");
m_strErrorMessage += _T("Cannot run \\windows\\DeviceID2003.exe file.");
return TEXT("CreateProcess");
}
// Wait until DeviceID2003.exe will be finished
::WaitForSingleObject(pi.hProcess, INFINITE);
// Read data from cpqAssetData.dat file
HANDLE hInFile;
TCHAR strSN[65];
DWORD dwBytesRead;
hInFile = CreateFile(TEXT("\\windows\\Device_ID.txt"), GENERIC_READ,FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hInFile == INVALID_HANDLE_VALUE)
{
m_strCompaqIpaqId = _T("");
m_strErrorMessage += _T("Cannot read \\windows\\Device_ID.txt file.");
return TEXT("CreateFile");
}
// Se lee el Device ID de un archivo txt y luego se traduce entre UNICODE y ANSI
//get file length
int dwFileSize = GetFileSize( hInFile, NULL );
//allocate a buffer for the script file
char* pszFileBuff = (char*)LocalAlloc( LPTR, dwFileSize );
if( !pszFileBuff )
AfxMessageBox( TEXT( "Couldn't Allocate Memory"));
ReadFile( hInFile,(LPVOID)pszFileBuff,dwFileSize, &dwBytesRead, NULL);
TCHAR* pszUnicodeBuff = (TCHAR*)LocalAlloc( LPTR,sizeof(TCHAR)*( dwFileSize + 1 ));
mbstowcs( pszUnicodeBuff, (const char *)pszFileBuff,(size_t)strlen(pszFileBuff) );
CloseHandle(hInFile);
// CHAR * pszDest = (CHAR*)malloc(256 * sizeof(TCHAR));
// ConvertTToC(pszDest, pszUnicodeBuff);
return pszUnicodeBuff;
#endif // _DEBUG
#endif // UNICOUSUARIO
}
Software Arch
I have resolved it with this
void CUtils::ConvertTToC(CHAR* pszDest, const TCHAR* pszSrc)
= (CHAR) pszSrc
;
{
for(int i = 0; i < _tcslen(pszSrc); i++)
pszDest
}
and the call is this
CHAR * pszDest = (CHAR*)malloc(256 * sizeof(TCHAR));
ConvertTToC(pszDest, strSN);
Now i see characters, not boxes, but i see one character yes and one no, i think it’s because odf the unicode problem. I didn’t find how to use the function you’ve said correctly, do you know what my problem is If it’s again that i need to use that function can you give a quick example of how to use it
Thanks in advance. Guillermo.