This sample shows how to convert a C string to a 100% compatible Visual Basic string.
Codice:
STDMETHODIMP CClass::get_ErrorMessage(int dwErrorCode, BSTR *pVal)
{
char *sz = (char *)alloca(1024);
BSTR tmp = NULL;
int n;
switch(dwErrorCode)
{
case 0:
strcpy(sz, "OK.");
break;
default:
strcpy(sz, "Unknown error!");
}
n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, NULL, 0);
tmp = (BSTR)alloca(n);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, tmp, n);
*pVal = SysAllocString(tmp);
return S_OK;
}
Variant for standard DLL's:
Codice:
BSTR ErrorMessage(int dwErrorCode)
{
char *sz = (char *)alloca(1024);
BSTR tmp = NULL;
int n;
switch(dwErrorCode)
{
case 0:
strcpy(sz, "OK.");
break;
default:
strcpy(sz, "Unknown error!");
}
n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, NULL, 0);
tmp = (BSTR)alloca(n);
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sz, -1, tmp, n);
return SysAllocString(tmp);
}
Segnalibri