I've a COM 64-bit dll built with Windows 2003 SP1 SDK April 2005
edition on Windows XP SP2. All MIDL options and project settings are
done correctly (as far as i know) to build the 64-bit DLL.
This DLL defines a structure of the form
typedef struct sVALIDATION
{
UINT32 unDataLen;
BYTE *pData;
}S_VALIDATION;
and has an interface method to fill up this structure of the form:
HRESULT FillUpStructure([in,out,ptr]S_VALIDATION *pSValidation);
{
//pseudo code
pSValidation->unDataLen = 3;
pSValidation->pData = (BYTE *)CoTaskMemAlloc(3);
//... set bytes
}

Now I'm using the tlb generated above, in my client application and
calling the FillUp method
//pseudo code
#import COM64.tlb
using namespace COM64Lib;
void main()
{
S_VALIDATION sValid;
memset(&sValid,0,sizeof(sValid));
try
{

IValidationPtr pValidation =
IValidationPtr(__uuidof(ValidationClass));
//....
pValidation->FillUpStructure(&sValid);
wprintf(L"%d",sValid.unDataLen); // prints "3" correctly

wprintf(L"%X",sValid.pData[0]); //throw up here
}
catch(...)
{
//....
}
//cleanup code
}
QUESTION:
When I'm accessing the pData value, it is throwing error. I debugged on
XP x64 machine and noticed
that pData pointer occupies 8 bytes. But when it returns from the
method call, the higher 4 bytes are being set
so the client code ends up accessing value at [00a1308e'00000000]
instead of [00000000'00a1308e] where actual data is present.

pData pointer before the call as seen in memory window {00 00 00 00 00
00 00 00}
pData pointer after the call as seen in memory window {00 00 00 00 8e
30 a1 00}

Why is it happening this way? I started stepping through code and went
little deep where RPCRT4!.... module doing some unmarshalling stuff.. I
didn't understand this part and quit. Please help me.

Thanks in advance,
Vedala

Re: 64-bit COM dll structure access by yezdived

yezdived
Wed Mar 08 00:29:07 CST 2006

Hi,
I've found what is causing this issue.. when I'm importing the TLB file
#import "COM64.tlb" the tlh which is generated has compiler inserted
pack pragma around the structure definition
##pragma pack(push, 8) //module level due to /Zp8

#pragma pack(push, 4)
struct sVALIDATION
{
UINT32 unDataLen;
BYTE *pData;
}S_VALIDATION;
#pragma pack(pop)
....
....
#pragma pack(pop) //module level


Due to this my client code got compiled with 4-byte alignment of this
structure while server dll got compiled with 8-byte alignment. This
happened even-though I've set module-level option /Zp8 for my client
project options.

Now my question is how should I tell the compiler to use 8-byte
alignment on this structure during generation of tlh file via #import
"COM64.tlb"?
Thanks in advance.


Re: 64-bit COM dll structure access by Rob

Rob
Wed Mar 08 09:58:23 CST 2006

yezdived@gmail.com wrote:

> Due to this my client code got compiled with 4-byte alignment of this
> structure while server dll got compiled with 8-byte alignment. This
> happened even-though I've set module-level option /Zp8 for my client
> project options.

I wonder if these sorts of problems could happen to an "AnyProc"
compiled IL DLL assembly, cause I think you summarized in four lines why
Microsoft when to all the trouble of making C# in the first place... :)

Rob

Re: 64-bit COM dll structure access by Mike

Mike
Thu May 04 17:38:19 CDT 2006

I'm sure it'll help him with his problem