CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Posts
    3

    HOW to convert an interface instance to SAFEARRAY

    The idea is to create some add-on in a program using COM functions interface exist in his own API.

    The particular type which I try to implement is a generic one , let say MyClass, and is declared like below:
    //MyClassPtr declaration;
    _COM_SMARTPTR_TYPEDEF(MyClass, __uuidof(IDispatch));

    //The class itself
    struct __declspec(uuid("5e772660-389b-11ce-ba48-080036250302"))
    MyClass : IDispatch
    {
    ...
    // other functions...
    ...
    IDispatchPtr Function1;
    __declspec(property(get=GetArea,put=PutArea))
    IDispatchPtr Function2;
    __declspec(property(get=GetSize))
    ...
    }

    I create an instance of MyClass using:
    MyClassPtr pMyInstance;

    This pMyInsatance have to be provided to another API function but in form of a SAFEARRAY!This function can look like below:

    FinalFunction(SAFEARRAY * * pMyClassInstances, .. , ..)

    So I have to convert my variable MyClass (can be more than one in my code of course, but I want to limit only to one instance for now) to this SAFEARRAY .. How can I do this?

    I try:
    SAFEARRAYBOUND rgsabound[1];
    rgsabound[0].lLbound = 0;
    rgsabound[0].cElements = 1;
    long a=0;

    SAFEARRAY * vMyClassArray;
    vMyClassArray.vt = VT_VARIANT|VT_ARRAY;
    vMyClassArray= SafeArrayCreate(VT_VARIANT, 1, rgsabound);
    SafeArrayPutElement(vMyClassArray .parray, &a, &(pMyInstance));

    Finally I put my class instance as an argument for:
    FinalFunction(pMyInstance, .. , ..)

    It doesn't seem to work, probably because argument pMyInstance can not be express as a VARIANT declarated type.
    The class I want to work with - MyClass- which exist in a .tlh file, is part of the API interface, so it is not exactly a user define type, but more an interface define type. I mention this becose I try to work with VT_RECORD type in SafeArrayCreate(...) instead of VT_VARIANT type, but this imply an idl function declaration for my type, and other additional stuff

    Any ideea ??

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: HOW to convert an interface instance to SAFEARRAY

    Your variants should be of type VT_DISPATCH and you store your object's IDispatch pointers in it

  3. #3
    Join Date
    Mar 2004
    Posts
    3

    Re: HOW to convert an interface instance to SAFEARRAY

    Hi kirants!
    Thank you for your time to answer to my problem!

    I'm new user with COM conversions and very confusing.. Please could you detailed your answer a little bit more ? I just ask for a start!
    Kind regards!
    Ioan

  4. #4
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: HOW to convert an interface instance to SAFEARRAY

    Did you try something like this ?

    Code:
    SAFEARRAY* PrepareSafeArrayForMyClass(MyClassPtr pMyInstance)
    {
        SAFEARRAYBOUND rgsabound[1];
        rgsabound[0].lLbound = 0;
        rgsabound[0].cElements = 1;
        long a=0;
    
       SAFEARRAY * vMyClassArray;
       vMyClassArray= SafeArrayCreate(VT_VARIANT, 1, rgsabound);
       IDispatch* pDisp = NULL;
       pMyInstance->QueryInterface(IID_IDispatch, (void**)&pDisp);
       VARIANT oVar;
       VariantInit(&oVar);
       oVar.vt = VT_DISPATCH;
       oVar.pdispVal = pDisp;
       SafeArrayPutElement(vMyClassArray, &a, &oVar);
    
       return vMyClassArray;
    }

  5. #5
    Join Date
    Mar 2004
    Posts
    3

    Re: HOW to convert an interface instance to SAFEARRAY

    Thank you very much kirants! :-)
    I hope this code to work!

    Kind regards!
    Ioan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured