Re: What types should I use in this COM Interface
- From: Jeroen Mostert <jmostert@xxxxxxxxx>
- Date: Fri, 29 Feb 2008 19:50:42 +0100
Oolis Kraprin wrote:
Hi, I am having to write a COM wrapper dll to enable the use of a c++
library in a c# .net app, and have a few questions regarding what
types to use in the interface.
I used to program quite a bit of COM, but a couple of years of c# .net
has made me stupid.
Anyway, I need to pass (from c# to the com component) a HWND window
handle, and 2 strings. The function needs to deliver back to c# 2
lists or arrays of strings.
My guess at the moment is something like:
ULONG WindowHandle
There are plenty of ways to marshal an HWND, but the easiest and still bit-safe way is to use a HANDLE as the native type (and cast between HWND and HANDLE on the C++ end).
BSTR FirstStringSAFEARRAY. The IDL could look like this:
BSTR SecondString
VARIANT* FirstReturnedStringArray or List
VARIANT* SecondReturnedStringArray or List
and I probably need to return 2 ints indicating the length of the 2
lists right, or is there some type for representing a list of array of
strings that contains the count?
[uuid(...)]
library Test {
[object, uuid(...)]
interface ITest : IUnknown {
HRESULT Foo([in] HANDLE hwnd, [in] BSTR firstString, [in] BSTR secondString, [out] SAFEARRAY(BSTR)* firstArray, [out] SAFEARRAY(BSTR)* secondArray);
}
}
Put this in a type library and let tlbimp.exe do its magic, and it should produce something that looks a lot like this without the need for any marshalling:
void Foo(IntPtr hwnd, string firstString, string secondString, out string[] firstArray, out string[] secondArray);
Using SAFEARRAYs in C++ is a bit of a chore, but the ATL helper clas CComSafeArray goes a long way towards easing the pain.
--
J.
.
- References:
- What types should I use in this COM Interface
- From: Oolis Kraprin
- What types should I use in this COM Interface
- Prev by Date: Return .NET array of objects to COM
- Previous by thread: What types should I use in this COM Interface
- Next by thread: Return .NET array of objects to COM
- Index(es):
Relevant Pages
|