Function-like macro or templates for auto buffer allocation

Discussion in 'C++' started by He Shiming, Aug 1, 2006.

  1. He Shiming

    He Shiming Guest

    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,
    --
    He Shiming
     
    He Shiming, Aug 1, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?V2FyYW4=?=

    Auto-Suggested Textbox like google auto suggest

    =?Utf-8?B?V2FyYW4=?=, Apr 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    8,569
    inrakeshworld
    Jul 27, 2007
  2. JKop
    Replies:
    3
    Views:
    520
  3. Patrick Kowalzick
    Replies:
    5
    Views:
    500
    Patrick Kowalzick
    Mar 14, 2006
  4. recover
    Replies:
    2
    Views:
    856
    recover
    Jul 25, 2006
  5. linkswanted
    Replies:
    1
    Views:
    991
Loading...

Share This Page