Re: What types should I use in this COM Interface



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 FirstString
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?

SAFEARRAY. The IDL could look like this:

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



Relevant Pages

  • TOC of Python Cookbook now online (was Re: author index for Python Cookbook 2?)
    ... Processing a String One Character at a Time ... Finding a File on the Python Search Path ... Constructing Lists with List Comprehensions ... Looping over Items and Their Indices in a Sequence ...
    (comp.lang.python)
  • VirtualAllocEx returns a bad pointer in some processes
    ... (ByVal hWnd As IntPtr, _ ... (ByVal hWndParent As IntPtr, _ ... ByVal lParam As String) As Integer ... Dim aihWnds As ArrayList ...
    (microsoft.public.vc.mfc)
  • Virtual memory C++ problem
    ... (ByVal hWnd As IntPtr, _ ... (ByVal hWndParent As IntPtr, _ ... ByVal lParam As String) As Integer ... Dim aihWnds As ArrayList ...
    (microsoft.public.win32.programmer.kernel)
  • ANN: MeObjects Library for Delphi
    ... object type small and powerful. ... the Object instance can use the ClassType method return the ... Especially for lists of pointers to dynamically allocated memory. ... {Summary Adds Ansi String and correspondent object to a list. ...
    (borland.public.delphi.thirdpartytools.general)
  • Filling a grid with Sharepoint data
    ... /// Retrieves a Lists web service proxy with default settings. ... string strComputerName = System.Net.Dns.GetHostName.ToLower; ... XmlDocument xmlDoc, ... XmlElement elementMethod, ...
    (microsoft.public.dotnet.framework.windowsforms)