for his post) and it worked perfectly.
"glennanthonyb" <glenn.csharp@yahoo.co.uk> ha scritto nel messaggio
news:OOitmM84HHA.484@TK2MSFTNGP06.phx.gbl...
Here's the C# interface definition...
int ListItem( int itemNumber, ref string buffer, int tabbed, int
bufferLength );
I did not ask for it... and this information does not help me.
However, you may consider the following code to convert BSTR <-> ANSI (I
embedded the conversion code into a couple of functions, and did a test in
WinMain; you should review and better test the code; however, it seems to
work OK):
<code>
//=========================================================
// Converstion of strings BSTR <-> ANSI
// By Giovanni Dicanio
// (2007, August)
//=========================================================
#include <Windows.h>
#include <atlbase.h>
#include <atlconv.h>
#include <strsafe.h>
//---------------------------------------------------------
// Convert an ANSI string to a BSTR
//---------------------------------------------------------
void StringAnsiToBstr(
IN const char * sz,
OUT BSTR * pbstr
)
{
ATLASSERT( sz != NULL );
ATLASSERT( pbstr != NULL );
// Build a CComBSTR object assigning the input
// ANSI string (the constructor converts from
// ANSI to BSTR)
CComBSTR bstr( sz );
// Copy to destination BSTR parameter,
// releasing the BSTR
*pbstr = bstr.Detach();
}
//---------------------------------------------------------
// Convert a BSTR to ANSI.
// The caller must provide memory buffer for ANSI,
// and the buffer size (in char's)
//---------------------------------------------------------
void StringBstrToAnsi(
IN BSTR * pbstr,
OUT char * sz,
IN size_t cch
)
{
ATLASSERT( pbstr != NULL );
ATLASSERT( *pbstr != NULL );
ATLASSERT( sz != NULL );
ATLASSERT( cch != 0 );
// Build an helper CComBSTR object with the input string
CComBSTR bstr( *pbstr );
// Convert from Unicode to ANSI
CW2A ansi( bstr );
// Copy ANSI to destination buffer
StringCchCopyA( sz, cch, ansi );
}
//---------------------------------------------------------
// *** TEST ***
//---------------------------------------------------------
int CALLBACK WinMain(
IN HINSTANCE hInstance,
IN HINSTANCE hPrevInstance,
IN LPSTR lpCmdLine,
IN int nShowCmd )
{
// Source ANSI string
const char * name = "Bob";
// Convert from ANSI to BSTR
BSTR bstr;
StringAnsiToBstr( name, &bstr );
// Display the BSTR string
MessageBoxW( NULL, bstr, L"The BSTR string is: ", MB_OK );
// Convert from BSTR back to ANSI
static const int ansiCch = 100;
char ansi[ ansiCch ];
ZeroMemory( ansi, sizeof(ansi) );
StringBstrToAnsi( &bstr, ansi, ansiCch );
// Display ANSI string
MessageBoxA( NULL, ansi, "The ANSI string is: ", MB_OK );
// Manually release the bstr
SysFreeString( bstr );
bstr = NULL;
return 0;
}
</code>
Giovanni