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. Advertisements

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,765
    inrakeshworld
    Jul 27, 2007
  2. JKop
    Replies:
    3
    Views:
    690
  3. Ken
    Replies:
    24
    Views:
    4,338
    Ben Bacarisse
    Nov 30, 2006
  4. BigMan
    Replies:
    1
    Views:
    565
  5. chris
    Replies:
    6
    Views:
    1,214
    chris
    Oct 28, 2005
  6. Patrick Kowalzick
    Replies:
    5
    Views:
    743
    Patrick Kowalzick
    Mar 14, 2006
  7. recover
    Replies:
    2
    Views:
    1,067
    recover
    Jul 25, 2006
  8. , India

    inline function vs function-like macro

    , India, Mar 6, 2007, in forum: C Programming
    Replies:
    2
    Views:
    847
    Flash Gordon
    Mar 6, 2007
Loading...