473,883 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

x64 and BSTR allocation, what has changed?

In win32 mode, a BSTR was UINT length prefixed and terminated exactly by a
zero char.

So if you allocated "Hello World" that would allocate 28 bytes.

In x64 and (IA64 as well) it would become 32 bytes, because of the fact that
a pointer, is 8 bytes instead of 4 bytes.

The length prefix -still- is a UINT however and not a UINT_PTR.

But it seems that I'm still not quite complete on par. Is there any other
info or rules that have changed on top off the win32 BSTR allocations?

Thanks!

Mar 21 '06
37 5497
Oh, I know how it's allocated :). I'm just pointing out that
this is officially undocumented.

So in Win64 we have:

byte len content
-8 4 unused
-4 4 length
0 n string

Or did I miss something?

--
=============== =============== =======
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: ag********@mvps .org
MVP VC FAQ: http://www.mvps.org/vcfaq
=============== =============== =======

"Egbert Nierop (MVP for IIS)" <eg***********@ nospam.invalid> wrote in
message news:Ot******** ******@TK2MSFTN GP12.phx.gbl...

"Alexander Nickolov" <ag********@mvp s.org> wrote in message
news:uk******** *****@TK2MSFTNG P09.phx.gbl...
Let's not forget that the memory must be 8-byte aligned.
I would guess it allocates 8 bytes for the length prefix, and
ignores the first four. And also let's not forget that the
allocation strategy for BSTR is officially undocumented...
Only the layout is documented.


Your guess is right about the 8 bytes length prefix.

The BSTR is just allocated by CoTaskMemAlloc and and CoTaskMemReallo c that
is using RtlHeap functions.

I have no source code, but my COM components, have been working for 3
years on this. Except on the 64-bit platform that I am testing now.

Mar 21 '06 #11
>Let's not forget that the memory must be 8-byte aligned.
I would guess it allocates 8 bytes for the length prefix, and
ignores the first four.


OK.

.... so why does this matter to Egbert?

Dave
Mar 21 '06 #12

"Alexander Nickolov" <ag********@mvp s.org> wrote in message
news:OS******** ******@TK2MSFTN GP11.phx.gbl...
Oh, I know how it's allocated :). I'm just pointing out that
this is officially undocumented.

So in Win64 we have:

byte len content
-8 4 unused
-4 4 length
0 n string and add a '\0' after the n string.
(unicode)
Or did I miss something?


It's working. I had still somewhere an invalid memory location calculation
(-4 instead of -8)

#include "stdafx.h"
#include "bstrnocach e.h"

void __stdcall FreeString(BSTR * theString) throw()
{
if (theString != NULL && *theString != NULL )
{
SysFreeString2( *theString);
*theString = NULL;
}
}

int __stdcall SysReAllocStrin gByteLen2(BSTR * bstr, const char* input, UINT
cch) throw()
{
// ATLASSERT(bstr != NULL);
if (*bstr == NULL)
*bstr = SysAllocStringB yteLen(input, cch);
else
{
UINT copyIntoLen = input != NULL ? lstrlenA(input) : 0;
if (copyIntoLen > cch)
copyIntoLen = cch;
UINT_PTR newlen = cch + sizeof(OLECHAR) + sizeof(UINT_PTR );

PCHAR temp = (PCHAR)(*bstr) - sizeof(UINT_PTR );

temp = (PCHAR)::CoTask MemRealloc(temp , newlen);

if (temp != NULL)
{
UINT_PTR* plen = (UINT_PTR*)temp ;
UINT* plen2 = (UINT*)temp;
plen2[sizePtrCorrecti on] = cch ; //asign bytelength for BSTR
if (copyIntoLen > 0) //copy chars
memcpy(&plen[1],
input,
(copyIntoLen + 1));

temp[sizeof(UINT_PTR ) + cch ] = 0; //terminate LPWSTR with a wide_string
temp[sizeof(UINT_PTR ) + cch + 1] = 0;
*bstr = (BSTR)&temp[sizeof(UINT_PTR )];
}
}
return *bstr == NULL ? FALSE : TRUE;
}
int __stdcall SysReAllocStrin gLen2(BSTR * bstr, const OLECHAR* input, UINT
cch) throw()
{
//ATLASSERT(bstr != NULL);
if (*bstr == NULL)
{
*bstr = SysAllocStringL en2(input, cch);
}
else
{
UINT_PTR copyIntoLen = input != NULL ? lstrlenW(input) : 0;
if (copyIntoLen > cch)
copyIntoLen = cch;
UINT_PTR newlen = cch * sizeof(OLECHAR) + sizeof(OLECHAR) +
sizeof(UINT_PTR );

PWSTR temp = *bstr - sizeof(UINT_PTR ) / sizeof(OLECHAR) ;

temp = (PWSTR)::CoTask MemRealloc(temp , newlen);

if (temp != NULL)
{
UINT_PTR* plen = (UINT_PTR*)temp ;
UINT* plen2 = (UINT*)temp;
plen2[sizePtrCorrecti on] = cch * sizeof(OLECHAR) ; //asign bytelength for
BSTR
if (copyIntoLen > 0) //copy values
memcpy(&temp[sizeof(UINT_PTR ) / sizeof(OLECHAR)],
input,
(copyIntoLen + 1) * sizeof(OLECHAR) );

temp[sizeof(UINT_PTR ) / sizeof(OLECHAR) + cch ] = 0; //terminate LPWSTR
*bstr = &temp[sizeof(UINT_PTR ) / sizeof(OLECHAR)];

}

}
return *bstr == NULL ? FALSE : TRUE;
}
BSTR __stdcall SysAllocStringL en2(const OLECHAR * input, UINT cch) throw()
{
UINT_PTR copyIntoLen = input != NULL ? lstrlenW(input) : 0;
if (copyIntoLen > cch)
copyIntoLen = cch;

UINT_PTR newlen = cch * sizeof(OLECHAR) + sizeof(OLECHAR) +
sizeof(UINT_PTR );

PWSTR temp = (PWSTR)::CoTask MemAlloc(newlen );
if (temp != NULL)
{
UINT_PTR* plen = (UINT_PTR*)temp ;
UINT*plen2 = (UINT*)temp;
plen2[0] =0;
plen2[sizePtrCorrecti on] = cch * sizeof(OLECHAR) ;
if (copyIntoLen > 0)
memcpy(&plen[1],
input,
(copyIntoLen + 1) * sizeof(OLECHAR) );

temp[sizeof(UINT_PTR ) / sizeof(OLECHAR) + cch ] = 0;
return &temp[sizeof(UINT_PTR ) / sizeof(OLECHAR)];
}
else
return NULL;

}

BSTR __stdcall SysAllocStringB yteLen2(const char* input, UINT cch)
throw()
{
UINT_PTR copyIntoLen = input != NULL ? lstrlenA(input) : 0;
if (copyIntoLen > cch)
copyIntoLen = cch;

UINT_PTR newlen = cch + sizeof(OLECHAR) + sizeof(UINT_PTR );

PWSTR temp = (PWSTR)::CoTask MemAlloc(newlen );
if (temp != NULL)
{
UINT_PTR* plen = (UINT_PTR*)temp ;
UINT* plen2 = (UINT*)temp;
plen2[sizePtrCorrecti on] = cch;
if (copyIntoLen > 0)
memcpy(&temp[sizeof(UINT_PTR ) / sizeof(OLECHAR)],
input,
copyIntoLen + 1);
char* zeroit = (char*)temp;
zeroit[sizeof(UINT_PTR ) + cch ] = 0; //terminate LPWSTR with a wide_string
zeroit[sizeof(UINT_PTR ) + cch + 1] = 0;
//temp[sizeof(unsigned int) / sizeof(wchar_t) + cch ] = 0;
return &temp[sizeof(UINT_PTR ) / sizeof(OLECHAR)];
}
else
return NULL;

}
///<summary>
/// Identical to SysAllocString
///</summary>
BSTR __stdcall SysAllocString2 (const OLECHAR * input) throw()
{
PWSTR retval = NULL;

if (input != NULL)
{
UINT_PTR slen = lstrlenW(input) ;
UINT_PTR newlen = slen * sizeof(OLECHAR) + sizeof(UINT_PTR ) +
sizeof(OLECHAR) ;
PWSTR temp = NULL;
temp = (PWSTR)::CoTask MemAlloc(newlen );

if (temp != NULL)
{
UINT* plen = (UINT*)temp;
plen[sizePtrCorrecti on] = (UINT)slen * sizeof(OLECHAR) ;
retval = &temp[sizeof(UINT_PTR ) / sizeof(OLECHAR)];
if (slen > 0)
memcpy(retval, input, (slen + 1) * sizeof(OLECHAR) );
}
}
return retval;
}

int __stdcall SysReAllocStrin g2(BSTR * bstr, const OLECHAR * input) throw()
{
//ATLASSERT(bstr != NULL);
bool didEmpty = FALSE;
if (*bstr == NULL)
{
*bstr = SysAllocString2 (input);
}
else
{

if (input == NULL)
{
FreeString(bstr );
didEmpty = true;
}
else
{
UINT_PTR slen = lstrlenW(input) ;
UINT_PTR newlen = slen * sizeof(OLECHAR) + sizeof(OLECHAR) +
sizeof(UINT_PTR );

BSTR temp = *bstr - sizeof(INT_PTR) / sizeof(OLECHAR) ; //get real address
temp = (BSTR)::CoTaskM emRealloc(temp, newlen);

if (temp != NULL)
{
UINT_PTR* plen = (UINT_PTR*)temp ;
UINT* plen2 = (UINT*)temp;
plen2[sizePtrCorrecti on] = (UINT)slen * sizeof(OLECHAR) ;
*bstr = &temp[sizeof(UINT_PTR ) / sizeof(OLECHAR)];
memcpy(*bstr, input, (slen + 1) * sizeof(OLECHAR) );
}
}

}
return *bstr == NULL && didEmpty == false? FALSE : TRUE;
}

UINT __stdcall SysStringByteLe n2(BSTR theString) throw()
{
UINT retval = 0;
if (theString != NULL)
{
UINT* temp = (UINT*)theStrin g;
temp--;
retval = *temp;
}
return retval;

}

UINT __stdcall SysStringLen2(B STR theString) throw()
{
UINT retval = 0;
if (theString != NULL)
{
UINT* temp = (UINT*)theStrin g;
temp--;
retval = *temp / sizeof(OLECHAR) ;
}
return retval;
}
Mar 21 '06 #13
Hi Egbert!
So, you're saying that on 64-bit platforms, a BSTR has an 8 byte
length portion rather than a 4 byte length? I'd be surprised if it
was.


A BSTR does not have a "length portion"... it is just a pointer...

Or did I miss something?

BSTR is just "wchar_t*". ..


You missed a lot by thinking this :)


No problem for me... I never rely on undecomented features...

(As "Alexander said: "And also let's not forget that the
allocation strategy for BSTR is officially undocumented... ")

Greetings
Jochen
Mar 22 '06 #14

"David Lowndes" <Da****@example .invalid> wrote in message
news:jk******** *************** *********@4ax.c om...
Let's not forget that the memory must be 8-byte aligned.
I would guess it allocates 8 bytes for the length prefix, and
ignores the first four.


OK.

... so why does this matter to Egbert?


The prefix was concernig my question, the alignment not.

CoTaskMemAlloc nowhere states in the docs that you must 'align' memory. I
think that RtlHeap* etc do align the allocations by themselves but I'm not
that guru to know this.

Mar 22 '06 #15

"Jochen Kalmbach [MVP]" <no************ ********@holzma .de> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Hi Egbert!
So, you're saying that on 64-bit platforms, a BSTR has an 8 byte
length portion rather than a 4 byte length? I'd be surprised if it
was.

A BSTR does not have a "length portion"... it is just a pointer...

Or did I miss something?

BSTR is just "wchar_t*". ..
You missed a lot by thinking this :)


No problem for me... I never rely on undecomented features...


Sure, it's your party, but BSTR is _not_ just a wchar_t* is documented!

(As "Alexander said: "And also let's not forget that the
allocation strategy for BSTR is officially undocumented... ")


This is not rocket science :)
If you need an 11 wchar length string, what else must you store but the
length _and_ the compatibility with wchar_t* ie a zero terminator?

The other thing you need to know, is that all oleautomation memory allocs,
should go through CoTaskMemAlloc. Now I'm a rocket scientist :)

I started to hesitate on the x64 because of GPs but now I fixed it.
Mar 22 '06 #16
>> ... so why does this matter to Egbert?

The prefix was concernig my question, the alignment not.


I still don't see why any of this was of any significance to you. What
are you doing that you need to know this for?

Dave
Mar 22 '06 #17

"David Lowndes" <Da****@example .invalid> wrote in message
news:0p******** *************** *********@4ax.c om...
... so why does this matter to Egbert?


The prefix was concernig my question, the alignment not.


I still don't see why any of this was of any significance to you. What
are you doing that you need to know this for?


Well, in fact, in this thread I explained it. What I do? I'm a rocket
scientist :)

This explains it all...
http://technolog.nl/eprogrammer/

Mar 22 '06 #18
>>>> ... so why does this matter to Egbert?

The prefix was concernig my question, the alignment not.
I still don't see why any of this was of any significance to you. What
are you doing that you need to know this for?


Well, in fact, in this thread I explained it. What I do? I'm a rocket
scientist :)


I read all the thread and *I* couldn't work out why you needed to
know.
This explains it all...
http://technolog.nl/eprogrammer/


Aha, the key bit being:

"I was managing my own BSTR allocation replacements"

I'd not sussed that from this thread.

Dave
Mar 22 '06 #19
Jochen Kalmbach [MVP] wrote:
BSTR is just "wchar_t*". ..


You missed a lot by thinking this :)


No problem for me... I never rely on undecomented features...


But you do realize that you must create, manipulate and destroy a BSTR
only in the documented ways, essentially using the Sysxxx functions? You
can provide a BSTR in instances where a LPCWSTR is expected, but not the
other way round. The preceding length value is an implementation detail,
and therefore we should not make any assumptions -- agreed. But if you
fail to remember that a BSTR is /not/ just a wchar_t array, you'll get
into trouble.
Mar 22 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7133
by: Gilad Walden | last post by:
I use C# in .NET framework. I have an ActiveX implemented in C++ that has a COM interface method that gets as it’s out parameter a BSTR* . The interop translates this BSTR* into C# string. From my managed code, I am calling that function with ‘out’ parameter of ‘string’ type. For normal size strings it works fine, but when the string is very big (> 50,000 bytes), I get a null exception from the COM interop. What is the reason?...
5
4856
by: Karthik | last post by:
Hello! I am not a wizard in this area! Just need some help out in this. I am trying to convert bstr string to new character string. Here is the snippet of my code. **** Code Start**** GlobalInterfacePtr->GetName(bstr, bstrTNamePtr, &retVal);
5
2670
by: bluter | last post by:
We have server components which were created by a third party and compiled in VC++5 (sp3). They run fine on NT4 and 2000, however during testing of our migration to Server 2003, these components have been failing when performing Data Access functions. In particular we have been having problems with BSTR and _bstr_t. _bstr_t(BSTR*, 1) is the original code that the problem started with. Other findings are - 1) Converting a BSTR* to a...
12
3384
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before the beginning of the array. But I couldn't find this information by looking at the memory. (Via VS2005 - C++) My second questions is, if there is a mechanism to know how much memory is allocated for the array, why don't we use it for things like...
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7957
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5782
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5979
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.