- Feb 25, 2004
- 21,754
- 599
- 126
I don't know a lot about marshaling or automation, still reading and learning I guess.
I have a C++ MFC application that exposes a method that takes a variant* as a parameter. The variant* is then assigned to point to a safearray of strings. I've used some code in a C++ application from another developer that I mostly to understand. It just allocates a block of memory of sizeof VARIANT and assigns it to a LPVARIANT. Once it is passed it gets the parray and the strings can be copied out.
I've even gotten it working in C# but I don't entirely understand how marshaling works. In C# the method is exposed and calls for the a ref object instead of an LPVARIANT, which makes sense as the LPVARIANT doesn't exist as a managed type and I guess ref object is the closet you're going to get.
This MarshalAs attribute allows the string array to be used to get the desired values. I have to cast to object first of course before using it.
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)] string[] pvParameterToPass;
The thing I don't like about this is the MarshalAs attribute only seems to work on class members. I don't need this available to the whole class, I only need it for the scope of a method. Is there a different way to do this? The Marshal classes' methods don't really seem to offer exactly what I want.
Again, I'm kind of just fumbling with this. I think I need to do some reading on marshaling and automation.
I have a C++ MFC application that exposes a method that takes a variant* as a parameter. The variant* is then assigned to point to a safearray of strings. I've used some code in a C++ application from another developer that I mostly to understand. It just allocates a block of memory of sizeof VARIANT and assigns it to a LPVARIANT. Once it is passed it gets the parray and the strings can be copied out.
I've even gotten it working in C# but I don't entirely understand how marshaling works. In C# the method is exposed and calls for the a ref object instead of an LPVARIANT, which makes sense as the LPVARIANT doesn't exist as a managed type and I guess ref object is the closet you're going to get.
This MarshalAs attribute allows the string array to be used to get the desired values. I have to cast to object first of course before using it.
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)] string[] pvParameterToPass;
The thing I don't like about this is the MarshalAs attribute only seems to work on class members. I don't need this available to the whole class, I only need it for the scope of a method. Is there a different way to do this? The Marshal classes' methods don't really seem to offer exactly what I want.
Again, I'm kind of just fumbling with this. I think I need to do some reading on marshaling and automation.