Solved

MIDL and variable  length structures

Posted on 2001-07-14
11
868 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
Comment Utility
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
Comment Utility
Key words: variable length.
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
Comment Utility
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
Comment Utility
Listening...
0
 
LVL 1

Expert Comment

by:ccaprar
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:tkirby052098
Comment Utility
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
Comment Utility
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
Comment Utility
Any suggestions on how to do this? The docs are a little twisted...
0
 
LVL 1

Expert Comment

by:ccaprar
Comment Utility
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
Comment Utility
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
Comment Utility
After much consideration.... the easy way is usually best.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

10 Experts available now in Live!

Get 1:1 Help Now