BSTR TP_SysAllocStringByteLen(LPCSTR psz, size_t len) { #ifdef WINDOWS return SysAllocStringByteLen(psz, (UINT)len); #else BSTR bstr; ULONG cbTotal = 0; if (FAILED(CbSysStringSize(len, TRUE, &cbTotal))) return NULL; bstr = (BSTR)TP_CoTaskMemAlloc(cbTotal); if (bstr != NULL) { #if defined(_WIN64) *(DWORD *)((char *)bstr + sizeof (DWORD)) = (DWORD)len; #else *(DWORD *)bstr = (DWORD)len; #endif bstr = (WCHAR*) ((char*) bstr + sizeof(DWORD_PTR)); if (psz != NULL) { memcpy(bstr, psz, len); } // NULL-terminate with both a narrow and wide zero. *((char *)bstr + len) = '\0'; *(WCHAR *)((char *)bstr + ((len + 1) & ~1)) = 0; } return bstr; #endif }
BSTR TP_SysAllocStringLen(LPWSTR psz, size_t len) { ULONG cbTotal = 0; if (FAILED(CbSysStringSize((ULONG)len, FALSE, &cbTotal))) return NULL; BSTR bstr = (BSTR)TP_CoTaskMemAlloc(cbTotal); if(bstr != NULL){ #if defined(_WIN64) // NOTE: There are some apps which peek back 4 bytes to look at the size of the BSTR. So, in case of 64-bit code, // we need to ensure that the BSTR length can be found by looking one DWORD before the BSTR pointer. *(DWORD_PTR *)bstr = (DWORD_PTR) 0; bstr = (BSTR) ((char *) bstr + sizeof (DWORD)); #endif *(DWORD *)bstr = (DWORD)len * sizeof(OLECHAR); bstr = (BSTR) ((char*) bstr + sizeof(DWORD)); if(psz != NULL){ memcpy(bstr, psz, len * sizeof(OLECHAR)); } bstr[len] = '\0'; // always 0 terminate } return bstr; }
DLL_EXPORT ExplStruct* WINAPI CdeclSimpleExplStruct(ExplStruct p,BOOL *result) { ExplStruct *pExplStruct; if((p.a !=1) || (p.udata.b != FALSE)) { *(result)= FALSE; return NULL; } pExplStruct = (ExplStruct*) TP_CoTaskMemAlloc(sizeof(ExplStruct) * 1); pExplStruct->a = 2; pExplStruct->udata.d = 3.142; *(result)= TRUE; return pExplStruct; }
DLL_EXPORT Sstr_simple* WINAPI CdeclSimpleStruct(Sstr_simple p,BOOL *result) { Sstr_simple *pSimpleStruct; if((p.a !=100) || (p.b != FALSE) || (p.c != 3.142)) { *(result)= FALSE; return NULL; } pSimpleStruct = (Sstr_simple*) TP_CoTaskMemAlloc(sizeof(Sstr_simple) * 1); pSimpleStruct->a = 101; pSimpleStruct->b = TRUE; pSimpleStruct->c = 10.11; *(result)= TRUE; return pSimpleStruct; }