1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <windows.h>
BSTR BstrFromChars(const char *strA, int codePage)
{
/*MultiByteToWideChar returns a size that includes the NUL terminator*/
int cchWideSize = MultiByteToWideChar(codePage, 0, strA, -1, NULL, 0);
int cchWideLength = cchWideSize - 1;
BSTR bstrRet;
if(cchWideSize == 0) { return NULL; }
/*SysAllocStringLen() takes care of adding the NUL terminator */
bstrRet = SysAllocStringLen(NULL, cchWideLength);
if(bstrRet == NULL) { return NULL; }
MultiByteToWideChar(codePage, 0, strA, -1, bstrRet, cchWideSize);
return bstrRet;
}
SAFEARRAY* MakeSafeArrayOfBytes(const void *mem, unsigned int size) /*Sorry, safe arrays don't accept a size_t*/
{
SAFEARRAY* pRet = SafeArrayCreateVector(VT_UI1, 0, size);
BOOL bSuccess = FALSE;
if(pRet != NULL)
{
void* pSafeArrayData;
if(SUCCEEDED(SafeArrayAccessData(pRet, &pSafeArrayData)))
{
memcpy(pSafeArrayData, mem, size);
SafeArrayUnaccessData(pRet);
bSuccess = TRUE;
}
}
/*If it failed, clean up*/
if(!bSuccess)
{
SafeArrayDestroy(pRet);
pRet=NULL;
}
return pRet;
} |
Partager