Passing VARIANT and VARIANT* to/from DotNet RRS feed

  • 問題


    I have a customer using Excel2000; and they want to pass an array of VARIANT data types (pass by VARIANT*) to a DotNet C# middleware, which also needs to pass VARIANT / VARIANT* back into C space.


    So what is the best way to pass VARIANT / VARIANT* between C# and C?  Your help is much appreciated.




    2008年10月22日 上午 06:52


  • *Marshal the C VARIANT* to object[] in managed C#

    The managed client uses the RCW to consume the native COM server. By default the tool TlbImp does not know what the COM Server mean by VARIANT*, so it will be marshaled as object. Since we want the VARIANT* to represent a VARIANT array, I think we can customize the RCW to marshal the data as IntPtr type. With the IntPtr variable in C#, we can call Marshal. GetObjectsForNativeVariants() function to get an array of C# objects.


    To customize the RCW, please refer this document

    And you can get more information about the GetObjectsForNativeVariants() function in this document


    *Marshal the object[] back to VARIANT* in C

    We can put all elements in the object[] array into an instance of CollectionBase type, then the CCW will automatically marshal the CollectionBase instance to an VARIANT* type variable.



    2008年11月2日 上午 06:31