Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 17th, 2006, 02:49 AM
Registered User
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to use COM Interop in C#

I have a requirement to use a dll written in Visual C++ 6.0 in my C# application. In order to establish the link between my application and the dll, I have written a ATL COM Component in Visual C++.NET [visual studio .NET version 8]. This COM component is referenced in my C# application. The COM component statically links with the 6.0 dll.

I am facing problems in passing data from C# to the dll through the COM component.

1. The dll exposes a function as:
__declspec(dllexport) int __stdcall ProblemFunction(int param1, BYTE param2, BYTE param3, BYTE *param4, DWORD *param5);

2. In the COM wrapper component, I have declared an interface method as:
[id(25), helpstring("method ProblemFunc")] HRESULT ProblemFunc([in] SHORT param1, [in] BYTE param2, [in] BYTE param3, [in, out] BYTE* param4, [in, out] LONG* param5, [out, retval] BYTE* param6);

where, param6 is used to return the value returned by dll function to the C# client application.

The implementation of this interface function is given below:
Wrapper::ProblemFunc(SHORT param1, BYTE param2, BYTE param3, BYTE* param4, LONG* param5, BYTE* param6)
    *param6 = ProblemFunction(param1, param2, param3, param4, (DWORD*)param5);
    return S_OK;

3. In the C# client application, I am calling the COM function as:
byte returnValue = DllWrapperObject.ProblemFunc(value1, (byte)value2, (byte)value3, ref value4, ref value5);

Here, I am facing problems passing value4 and value5 because:
a. For value4 I need to pass a structure.
b. For value5 I need to pass the size of the structure being passed in value4.

The structure to be passed is as required by the dll. The C++ structure is:
typedef struct
    DWORD var1;
    DWORD var2;

        DWORD var3;
        char var4[16];
    DWORD var5;
    } var6[3];

I wrote the structure's equivalent in C# as a class as I had to create an array. The C# implementation of the above given structure is:
    internal class InnerStruct
        long var3;
        char[] var4 = new char[16];
        long var5;

        internal static int GetSizeOfClass()
            return (sizeof(long) + sizeof(char) * 16 + sizeof(long));

    internal class TestStruct
        long var1;
        long var2;
        InnerStruct[] var6 = new InnerStruct[3];

        internal static int GetSizeOfClass()
            return (sizeof(long) * 2 + InnerStruct.GetSizeOfClass() * 3);

1. Is there any better way to define the structure equivalent in C#?
2. How can I get the size of the structure/class for passing it to the dll?
3. How to pass the handle of the structure/class object from C# to the dll so that it can fill it up and return it to the client app for future usage?

Nataraj S. Narayana
Senior Engineer [Windows]

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# and Interop.ShockwaveFlashObjects TheBigOnion C# 0 December 19th, 2006 12:27 PM
com interop CenT C# 1 October 6th, 2006 08:45 AM
Deploying C# COM Interop wizardjagon C# 4 February 1st, 2006 04:37 AM
Interop Problem Yehuda General .NET 0 July 1st, 2004 08:57 AM
Interop Troubles Yehuda Pro VB.NET 2002/2003 0 June 29th, 2004 01:26 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.