From 4b6c0e31385f5f27a151088c0a2b614495c4e589 Mon Sep 17 00:00:00 2001
From: Paul Duncan
+I've been using CAPICOM
+at work. Since most COM objects are supposed to
+work with VB, the string values returned by
+COM functions (in my
+case CAPICOM::Certificate.Export())
+have some bizarre and baroque semantics when called from C++. One quirk
+I found particularly amusing was the memory allocation behind BSTRs; here's what "Eric's
+Complete Guide to BSTR Semantics" has to say about what's
+happening under the hood for BSTRs:
+
+COM code uses the BSTR to store a Unicode string, short for "Basic
+String". (So called because this method of storing strings was developed
+for OLE Automation, which was at the time motivated by the development
+ of the Visual Basic language engine.)
+ ...
+ A BSTR always points to the first valid character in the buffer.
+This is not legal:
+bstrLast is not a legal BSTR
+
+
+
+
+
+
+
+BSTR bstrName = SysAllocString(L"John Doe");
+BSTR bstrLast = &bstrName[5]; // ERROR
+
+
....
+ ++When you call SysAllocString(L"ABCDE") the operating system actually allocates sixteen bytes. The first four bytes are a 32 bit integer representing the number of valid bytes in the string -- initialized to ten in this case. The next ten bytes belong to the caller and are filled in with the data passed in to the allocator. The final two bytes are filled in with zeros. You are then given a pointer to the data, not to the header. +
+ + +(Emphasis is mine)
+ ++Strings with a length prefix and a double-NULL suffix. Now +that's what I call efficient use of memory! Seriously though, +this is like some sort of programming time warp; it reminds me of both +the Pascal-induced single-byte length prefix strings the +Mac Toolbox +calls used and the associated (and equally wacky) string-conversion +functions. + Ah, history. +
+ -- cgit v1.2.3