?
Solved

MIDL and variable  length structures

Posted on 2001-07-14
11
Medium Priority
?
887 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 

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

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!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

752 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