Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. SysAllocString() allocation

SysAllocString() allocation

Scheduled Pinned Locked Moved COM
comperformancequestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abc876
    wrote on last edited by
    #1

    I just want to know when we allocate a BSTR using SysAllocString(), where does COM allocate its memory? In process's own heap? some shared memory or where? BSTR is just a wchar_t with size of string added in its start, if i create my own string in this format and pass it to any COM function, will that work? + LPWSTR allocated with CoTaskMemAlloc() doesnot have size in its start, how does COM decide whether to check size from start of string or check for null terminating string?? Muhammad Shoaib Khan http://geocities.com/lansolution

    G J 2 Replies Last reply
    0
    • A abc876

      I just want to know when we allocate a BSTR using SysAllocString(), where does COM allocate its memory? In process's own heap? some shared memory or where? BSTR is just a wchar_t with size of string added in its start, if i create my own string in this format and pass it to any COM function, will that work? + LPWSTR allocated with CoTaskMemAlloc() doesnot have size in its start, how does COM decide whether to check size from start of string or check for null terminating string?? Muhammad Shoaib Khan http://geocities.com/lansolution

      G Offline
      G Offline
      geo_m
      wrote on last edited by
      #2

      1. wild guess - on the heap of the library that exported the SysAllocString ? 2. I think yes, as long as you will be the one who releases the memory back 3. when expects BSTR, it expects the size on the start, when parameter is LPWSTR, it checks for zero termination.

      1 Reply Last reply
      0
      • A abc876

        I just want to know when we allocate a BSTR using SysAllocString(), where does COM allocate its memory? In process's own heap? some shared memory or where? BSTR is just a wchar_t with size of string added in its start, if i create my own string in this format and pass it to any COM function, will that work? + LPWSTR allocated with CoTaskMemAlloc() doesnot have size in its start, how does COM decide whether to check size from start of string or check for null terminating string?? Muhammad Shoaib Khan http://geocities.com/lansolution

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #3

        I believe it's allocated on the COM heap. M.Shoaib Khan wrote: LPWSTR allocated with CoTaskMemAlloc() doesnot have size in its start, how does COM decide whether to check size from start of string or check for null terminating string?? When COM allocates a BSTR, it does something like this:

        struct _BSTR {
        int length;
        OLECHAR str[1];
        };

        BSTR SysAllocString(LPCOLESTR src)
        {
        int bstr_len = wcslen(src) + 1; // 1 for NUL character
        _BSTR* bstr = CoTaskMemAlloc(bstr_len * sizeof(OLECHAR) + sizeof(int));
        bstr->length = bstr_len;
        wcscpy(bstr->str, src, bstr_len - 1);
        return bstr->str;
        }

        As you can see, a BSTR really isn't a LPWSTR. It just appears to be. :) -- My name in Katakana is ヨルゲン. My name in German is Jörgen. My name in Mandarin/Kanji is 乔尔根 西格瓦德森. I blog too now[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups