Solved

MIDL and variable  length structures

Posted on 2001-07-14
11
872 Views
Last Modified: 2008-02-01
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
Comment
Question by:tkirby052098
11 Comments
 
LVL 1

Expert Comment

by:hakob
ID: 6283152
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
 

Author Comment

by:tkirby052098
ID: 6283587
Key words: variable length.
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6283963
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
 
LVL 9

Expert Comment

by:BeyondWu
ID: 6284640
Listening...
0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6284819
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tkirby052098
ID: 6287207
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
 
LVL 1

Expert Comment

by:ccaprar
ID: 6288561
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
 

Author Comment

by:tkirby052098
ID: 6290846
Any suggestions on how to do this? The docs are a little twisted...
0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6293144
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
 

Author Comment

by:tkirby052098
ID: 6306567
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
 

Author Comment

by:tkirby052098
ID: 6352699
After much consideration.... the easy way is usually best.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now