Re: Strings again :(
hamishd wrote:
This is the entire function:
// Get the name of the Excel-ODBC driver
void CSpreadSheet::GetExcelDriver()
{
I tried to modified your code and it seems to work (also in Unicode
builds)...
Here it is a compilable function GetExcelDriver(), that returns empty
string on error, or the required string on success:
<code>
// Gets the name of the Excel-ODBC driver.
// Returns empty string on error.
CString GetExcelDriver()
{
TCHAR szBuf[64*1024];
WORD cchBufMax = _countof(szBuf) - 1;
WORD cchBufOut;
TCHAR *pszBuf = szBuf;
// Get the names of the installed drivers ("odbcinst.h" has to be
included )
if(!SQLGetInstalledDrivers(szBuf,cchBufMax, &cchBufOut))
{
// Error
return _T("");
}
// Search for the driver...
do
{
if( _tcsstr( pszBuf, _T("Excel") ) != NULL )
{
// Found !
return CString( pszBuf );
}
pszBuf = _tcschr( pszBuf, _T('\0') ) + 1;
}
while( pszBuf[1] != _T('\0') );
// Not found - return empty string
return _T("");
}
</code>
You can make it a static member function of your class, and call it like
this:
...
m_sExcelDriver = GetExcelDriver();
...
However, reading the code you linked from CodeProject, it seems to me
that there are other problems in that code (that seems to be written for
ANSI-only builds, it seems to me not Unicode aware).
If you want to learn how to properly use CString class and write
Unicode-aware code, you may want to read this article by Joe on CodeProject:
http://www.codeproject.com/KB/string/cstringmgmt.aspx
(Probably you will find also similar articles on Joe's own site).
Giovanni