Click here to Skip to main content
16,017,015 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: SysAllocString Pin
Matthew Faithfull23-Nov-07 3:41
Matthew Faithfull23-Nov-07 3:41 
GeneralRe: SysAllocString Pin
George_George23-Nov-07 5:27
George_George23-Nov-07 5:27 
GeneralRe: SysAllocString Pin
Matthew Faithfull23-Nov-07 5:38
Matthew Faithfull23-Nov-07 5:38 
GeneralRe: SysAllocString Pin
George_George23-Nov-07 20:07
George_George23-Nov-07 20:07 
GeneralRe: SysAllocString Pin
Matthew Faithfull24-Nov-07 6:44
Matthew Faithfull24-Nov-07 6:44 
GeneralRe: SysAllocString Pin
George_George25-Nov-07 3:42
George_George25-Nov-07 3:42 
AnswerRe: SysAllocString Pin
Matthew Faithfull25-Nov-07 12:24
Matthew Faithfull25-Nov-07 12:24 
AnswerRe: SysAllocString Pin
Florin Crişan23-Nov-07 4:29
Florin Crişan23-Nov-07 4:29 
I don't know if you have noticed[^] but SysAllocString is part of the COM library (you need to link to oleaut32.lib if you use it).

The returned type is a BSTR – a string type used by COM – rather than a WCHAR *. The differences are:
- BSTRs are allocated using the COM allocator, so they can passed to another process (and deallocated by another process). This is why you should use SysFreeString rather than delete or free.
- BSTRs have a length prefix (besides having null terminator).

The length is kept (as a UINT) in the memory location just before the first character.
The BSTR type is actually defined as a WCHAR *, pointing to the first character (just like a regular LPWSTR), so you can use BSTRs wherever you can use LPWSTRs. Just remember that you should use SysFreeString to deallocate it.

So, a regular string looks in memory like this:
S     o     m     e     [space] t     e     x     t     [null]
53 00 6f 00 6d 00 65 00 20 00   74 00 65 00 78 00 74 00 00

whereas a BSTR looks like this
string length  S     o     m     e     [space] t     e     x     t     [null]
09 00 00 00    53 00 6f 00 6d 00 65 00 20 00   74 00 65 00 78 00 74 00 00

Hope this clears things up a little bit.

See also this page[^].



Florin Crişan

GeneralRe: SysAllocString Pin
George_George23-Nov-07 5:26
George_George23-Nov-07 5:26 
GeneralRe: SysAllocString Pin
Iain Clarke, Warrior Programmer23-Nov-07 5:49
Iain Clarke, Warrior Programmer23-Nov-07 5:49 
GeneralRe: SysAllocString Pin
George_George23-Nov-07 20:10
George_George23-Nov-07 20:10 
GeneralRe: SysAllocString Pin
Iain Clarke, Warrior Programmer24-Nov-07 11:34
Iain Clarke, Warrior Programmer24-Nov-07 11:34 
GeneralRe: SysAllocString Pin
George_George25-Nov-07 3:39
George_George25-Nov-07 3:39 
GeneralRe: SysAllocString Pin
Iain Clarke, Warrior Programmer25-Nov-07 14:21
Iain Clarke, Warrior Programmer25-Nov-07 14:21 
AnswerRe: SysAllocString Pin
Gary R. Wheeler24-Nov-07 2:29
Gary R. Wheeler24-Nov-07 2:29 
GeneralRe: SysAllocString Pin
George_George24-Nov-07 3:50
George_George24-Nov-07 3:50 
QuestionDoModal() question? Pin
Gofur Halmurat23-Nov-07 2:52
Gofur Halmurat23-Nov-07 2:52 
QuestionRe: DoModal() question? Pin
Maximilien23-Nov-07 3:11
Maximilien23-Nov-07 3:11 
AnswerRe: DoModal() question? Pin
Gofur Halmurat23-Nov-07 3:38
Gofur Halmurat23-Nov-07 3:38 
GeneralRe: DoModal() question? Pin
toxcct23-Nov-07 3:48
toxcct23-Nov-07 3:48 
GeneralRe: DoModal() question? Pin
Gofur Halmurat23-Nov-07 3:50
Gofur Halmurat23-Nov-07 3:50 
GeneralRe: DoModal() question? Pin
toxcct23-Nov-07 3:53
toxcct23-Nov-07 3:53 
GeneralRe: DoModal() question? Pin
Gofur Halmurat23-Nov-07 4:18
Gofur Halmurat23-Nov-07 4:18 
AnswerRe: DoModal() question? Pin
Mark Salsbery23-Nov-07 5:04
Mark Salsbery23-Nov-07 5:04 
AnswerRe: DoModal() question? Pin
Gary R. Wheeler24-Nov-07 2:41
Gary R. Wheeler24-Nov-07 2:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.