BSTR tUtil::string2bstr(const char * string, size_t len) { if(!string) return NULL; BSTR bstr; if(len == 0) { bstr = SysAllocStringLen(NULL, 0); } else { long wclength = MultiByteToWideChar(CP_UTF8, 0, string, len, NULL, 0); try { if(wclength == 0) LUACOM_ERROR(tUtil::GetErrorMessage(GetLastError())); } catch(class tLuaCOMException& e) { UNUSED(e); return NULL; } bstr = SysAllocStringLen(NULL, wclength); MultiByteToWideChar(CP_UTF8, 0, string, len, bstr, wclength); } return bstr; }
BSTR tUtil::string2bstr(const char * string) { if(!string) return NULL; BSTR bstr; long length = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0); try { if(length == 0) LUACOM_ERROR(tUtil::GetErrorMessage(GetLastError())); } catch(class tLuaCOMException& e) { UNUSED(e); return NULL; } wchar_t *widestr = new wchar_t[length]; MultiByteToWideChar(CP_UTF8, 0, string, -1, widestr, length); bstr = SysAllocString(widestr); delete[] widestr; widestr = NULL; return bstr; }
void tLuaCOM::releaseConnection(tLuaCOM* server, DWORD cookie) { IConnectionPointContainer *pcpc = NULL; IConnectionPoint *connection_point; HRESULT hr = pdisp->QueryInterface ( IID_IConnectionPointContainer, (void **) &pcpc ); if(FAILED(hr)) { LUACOM_ERROR("Object does not accept connections!"); } { IID guid; server->GetIID(&guid); hr = pcpc->FindConnectionPoint(guid, &connection_point); } pcpc->Release(); pcpc = NULL; if(FAILED(hr)) { LUACOM_ERROR("No connection point for this interface!"); } connection_point->Unadvise(cookie); connection_point->Release(); connection_point = NULL; }
const char * tUtil::bstr2string(BSTR bstr) { char* str = NULL; long size = 0; int result = 0; try { if(bstr != NULL) { // gets the size of the buffer size = WideCharToMultiByte( CP_UTF8, // code page 0, // performance and mapping flags bstr, // wide-character string -1, // number of chars in string str, // buffer for new string 0, // size of buffer NULL, // default for unmappable chars NULL // set when default char used ); if(!size) LUACOM_ERROR(tUtil::GetErrorMessage(GetLastError())); str = new char[size]; result = WideCharToMultiByte( CP_UTF8, // code page 0, // performance and mapping flags bstr, // wide-character string -1, // number of chars in string str, // buffer for new string size, // size of buffer NULL, // default for unmappable chars NULL // set when default char used ); if(!result) LUACOM_ERROR(tUtil::GetErrorMessage(GetLastError())); } else { str = new char[1]; str[0] = '\0'; } } catch(class tLuaCOMException& e) { UNUSED(e); if(str) delete[] str; str = new char[1]; str[0] = '\0'; } tUtil::string_buffer.copyToBuffer(str); delete[] str; return tUtil::string_buffer.getBuffer(); }