Function-like macro or templates for auto buffer allocation

H

He Shiming

Hi All,

In my COM (component object model) server DLL I created, there are these
methods that require buffer allocation. They look like this:

class CComClass : public IClass { // just for demonstration
public:
HRESULT Method (long lParam1, long lParam2, unsigned char* pBufferOut,
long * plSizeOut);
};

To use these methods, I need to call them twice to actually execute the
function. First call, I set pBufferOut to NULL, and the method will return
the size requirement of the buffer in plSizeOut. Using that information, I
proceed with buffer allocation on the client side, and then call the method
again using the allocated buffer pointer. From what I understand, this is a
standard way for these kind of functions.

My problem is, even by using vector<unsigned char> as the buffer, the code
looks very ugly. It requires two calls, and I have to check for errors
before and after allocation. So I wonder if I could create a macro or a
template to automate the two calls, so that it becomes:

vector<unsigned char> buffer;
AUTOFUNC(spCC, Method, lparam1, lparam2, buffer);

There's one catch though, these methods have variable number of arguments.
Only the last two are the same, pBufferOut and plSizeOut. But I'm not sure
how I could use that to my advantage.

Can I put parenthesized arguments into a macro? Can I use templates in this
situation?

Thanks in advance,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top