<p>I have a BSTR object that I would like to convert to copy to a wchar__t object. The tricky thing is the length of the BSTR object could be anywhere from a few kilobytes to a few hundred kilobytes. Is there an efficient way of copying the data across? I know I could just declare a wchar_t array and alway allocate the maximum possible data it would ever need to hold. However, this would mean allocating hundreds of kilobytes of data for something that potentially might only require a few kilobytes. Any suggestions?</p>

[{'answer_id': 72022, 'author': 'moonshadow', 'author_id': 11834, 'author_profile': 'https://Stackoverflow.com/users/11834', 'pm_score': 3, 'selected': False, 'text': "<p>BSTR objects contain a length prefix, so finding out the length is cheap. Find out the length, allocate a new array big enough to hold the result, process into that, and remember to free it when you're done.</p>\n"}, {'answer_id': 72033, 'author': 'titanae', 'author_id': 2387, 'author_profile': 'https://Stackoverflow.com/users/2387', 'pm_score': 0, 'selected': False, 'text': "<p>Use ATL, and CStringT then you can just use the assignment operator. Or you can use the USES_CONVERSION macros, these use heap alloc, so you will be sure that you won't leak memory.</p>\n"}, {'answer_id': 72760, 'author': 'Mike Dimmick', 'author_id': 6970, 'author_profile': 'https://Stackoverflow.com/users/6970', 'pm_score': 2, 'selected': False, 'text': "<p>There is never any need for conversion. A <code>BSTR</code> pointer points to the first character of the string and it is null-terminated. The length is stored before the first character in memory. <code>BSTR</code>s are always Unicode (UTF-16/UCS-2). There was at one stage something called an 'ANSI BSTR' - there are some references in legacy APIs - but you can ignore these in current development.</p>\n\n<p>This means you can pass a <code>BSTR</code> safely to any function expecting a <code>wchar_t</code>.</p>\n\n<p>In Visual Studio 2008 you may get a compiler error, because <code>BSTR</code> is defined as a pointer to <code>unsigned short</code>, while <code>wchar_t</code> is a native type. You can either cast or turn off <code>wchar_t</code> compliance with <code>/Zc:wchar_t</code>.</p>\n"}, {'answer_id': 72866, 'author': 'Nemanja Trifunovic', 'author_id': 8899, 'author_profile': 'https://Stackoverflow.com/users/8899', 'pm_score': 2, 'selected': False, 'text': '<p>One thing to keep in mind is that <code>BSTR</code> strings can, and often do, contain embedded nulls. A null does not mean the end of the string.</p>\n'}, {'answer_id': 73928, 'author': 'Euro Micelli', 'author_id': 2230, 'author_profile': 'https://Stackoverflow.com/users/2230', 'pm_score': 3, 'selected': False, 'text': '<p>First, you might not actually have to do anything at all, if all you need to do is read the contents. A BSTR type is a pointer to a null-terminated wchar_t array already. In fact, if you check the headers, you will find that BSTR is essentially defined as:</p>\n<pre><code>typedef BSTR wchar_t*;\n</code></pre>\n<p>So, the compiler can\'t distinguish between them, even though they have different semantics.</p>\n<p>There is are two important caveat.</p>\n<ol>\n<li><p><s>BSTRs are supposed to be immutable. You should never change the contents of a BSTR after it has been initialized. If you &quot;change it&quot;, you have to create a new one assign the new pointer and release the old one (if you own it).</s><br />\n<i>[<strong>UPDATE</strong>: this is not true; sorry! You can modify BSTRs in place; I very rarely have had the need.]</i></p>\n</li>\n<li><p>BSTRs are allowed to contain embedded null characters, whereas traditional C/C++ strings are not.</p>\n</li>\n</ol>\n<p>If you have a fair amount of control of the source of the BSTR, and can guarantee that the BSTR does not have embedded NULLs, you can read from the BSTR as if it was a wchar_t and use conventional string methods (wcscpy, etc) to access it. If not, your life gets harder. You will have to always manipulate your data as either more BSTRs, or as a dynamically-allocated array of wchar_t. Most string-related functions will not work correctly.</p>\n<p>Let\'s assume you control your data, or don\'t worry about NULLs. Let\'s assume also that you really need to make a copy and can\'t just read the existing BSTR directly. In that case, you can do something like this:</p>\n<pre><code>UINT length = SysStringLen(myBstr);        // Ask COM for the size of the BSTR\nwchar_t *myString = new wchar_t[length+1]; // Note: SysStringLen doesn\'t \n                                           // include the space needed for the NULL\n\nwcscpy(myString, myBstr);                  // Or your favorite safer string function\n\n// ...\n\ndelete myString; // Done\n</code></pre>\n<p>If you are using class wrappers for your BSTR, the wrapper should have a way to call SysStringLen() for you. For example:</p>\n<pre><code>CComBString    use .Length();\n_bstr_t        use .length();\n</code></pre>\n<p><strong>UPDATE</strong>: This is a good article on the subject by someone far more knowledgeable than me:<br />\n<a href="http://blogs.msdn.com/ericlippert/archive/2003/09/12/52976.aspx" rel="nofollow noreferrer">&quot;Eric [Lippert]\'s Complete Guide To BSTR Semantics&quot;</a></p>\n<p><strong>UPDATE</strong>: Replaced <code>strcpy()</code> with <code>wcscpy()</code> in the example.</p>\n'}]
['https://Stackoverflow.com/questions/71980', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']