[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 902
  • Last Modified:

MIDL and variable length structures

Given a variable length structure like this:

typedef struct tagWHAT {
DWORD dwSize;
DWORD dwValue;
// more data here, allocated by caller
} WHAT;

Can this be passed as an [in,out] argument to a COM/DCOM method?

HRESULT method_name(
[in,out] WHAT *pWhat
);

The size will not (probably??) change from in to out, but the data in the structure will be changed by the function.
How do I tell RPC about the size of the structure to transmit???  size_is won't work (refers to number of Elements, not bytes).

0
tkirby052098
Asked:
tkirby052098
1 Solution
 
hakobCommented:
size_is is used for the arrays, not for the structures. To
transmit the types other than the predefined ones, you need to define this type in idl file near the method declaration, the argument of which is of this type.

So, write

typedef struct tagWHAT {
DWORD dwSize;
DWORD dwValue;
// more data here, allocated by caller
} WHAT;          
HRESULT method_ name( [in,out] WHAT* pstruct);

together in idl file

0
 
tkirby052098Author Commented:
Key words: variable length.
0
 
jkrCommented:
Why not transmitting the struct's contents as a byte array? This will save you a lot of headache and you could even reach a maximum flexibility when also transmitting a struct type ID - you could use the same data-passing function for a lot of operations, e.g.

typedef struct tagWHAT {
DWORD dwSize;
DWORD dwValue;
// more data here, allocated by caller
} WHAT;


HRESULT method_name_without_type(
[in,out] LONG *plSize,
[in,out size_is( *plSize)] BYTE *pWhat
);

or

HRESULT method_name_with_type_tag(
[in,out] LONG *plTypeID,
[in,out] LONG *plSize,
[in,out size_is( *plSize)] BYTE *pWhat
);

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BeyondWuCommented:
Listening...
0
 
ccaprarCommented:
Hi tkirby. There is a very good article on MSDN that concerns returning data out of COM method calls. It describes IDL syntax and also examples with both automation and C++ clients. You should take a look and pick up the method that most suits your needs. The article can be found in MSDN at: http://www.microsoft.com/MSJ/0599/wicked/wicked0599.htm

jkr's method is good also, as long as you don't plan to return in your structure some data allocated inside the COM method. The data allocated in the caller will be lost and also you will try to dealloc wrong data after return. Also, be aware not to return more data than the initial buffer. If returning less data, you could also check out length_is() in IDL.
0
 
tkirby052098Author Commented:
jkr's method looks like it would work, but being a really stubborn person, I really want to use the structure data type as the argument to the function. The articles suggested by ccaprar were a good refresher, but do not explicitly cover variable length structures, per se.

There must be some way to do this... How about using transmit_as to "convert" the structure pointer to a BYTE pointer?
0
 
ccaprarCommented:
You should try with transmit_as. I searched MSDN, and it seems it should work for you. Just be careful in allocating/deallocatiing the memory for the transmitted data.
0
 
tkirby052098Author Commented:
Any suggestions on how to do this? The docs are a little twisted...
0
 
ccaprarCommented:
in Platform SDK there is an example called XMIT (just search for it). It uses data transfer with transfer_as. I started an example based on that one and your problem, but right now I don't have any more time to make it work. Take a look at XMIT code and maybe we wil discuss here further
0
 
tkirby052098Author Commented:
the xmit example uses malloc for local allocation. shouldn't this be CoTaskMemAlloc? why or why not?

Problem: XXX_TYPE_from_xmit function passes 2nd pointer argument already allocated by stub program. size of allocation based on structure typdef that has variable size. actual typedef has fixed size _less than actual size_. Is this (transfer_as) in general not compatible with variable length structures due to this allocation rule?

0
 
tkirby052098Author Commented:
After much consideration.... the easy way is usually best.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now