Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2008

Tip: Looking for answers? Try searching our database.

What types should I use in this COM Interface

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oolis Kraprin - 29 Feb 2008 08:48 GMT
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
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?

I am looking for whatever makes the most sense and requires the
minimal amount of mucking aroung with marshalling etc.

Thanks a lot
Jeroen Mostert - 29 Feb 2008 18:50 GMT
> 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
[quoted text clipped - 9 lines]
> 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
[quoted text clipped - 4 lines]
> 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.

Signature

J.

Oolis Kraprin - 03 Mar 2008 07:47 GMT
Thanks a lot for the reply.

I ended up with something like this:
HRESULT Address([in] ULONG windowHandle, [in] BSTR firstString, [in]
BSTR secondString, [out] VARIANT *firstArray, [out] VARIANT
*secondArray);

The ulong seems to work ok for the window handle, but I might change
it to HANDLE. I managed to specify SAFEARRAYs in the IDL but had
problems with using them in the c++ file function definition. I ended
using safearrays by wrapping them in the variants.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.