How to Use Structures or Arrays in Methods that are to be COM Visible

Category: c# language

Question

Al Willis on Thu, 09 Jul 2009 23:39:21


I have been able to successfully call my C# DLL from Delphi when using simple data types like double, int or string.  How do I pass complex objects like structures or arrays of structures?  I've made the DLL to have a COM wrapper.

The following code works:

namespace DwgLib
{
    [Guid("31D672CE-0BBF-4c82-AF1E-3DF873A3E738")]
    public interface DwgLib_Interface
    {
        [DispId(1)]
        void GetDrawingData(string inputFileName, string outputFileName);
        [DispId(2)]
        void InitializeDrawingServices();
        [DispId(3)]
        void DisposeDrawingServices();
        [DispId(4)]
        void WriteDrawingData(string modelDwgFullPath, string loopDwgFullPath, string dwgCommandsFileName, string resultsFileName);
    }

    // Events interface  
    [Guid("B9546BC7-32CE-47fe-8EE1-551500D85042"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface DwgLib_Events
    {
    }
    [Guid("B797BBE6-69B3-4251-964C-A21A953FE064"),
        ClassInterface(ClassInterfaceType.None),
        ComSourceInterfaces(typeof(DwgLib_Events))]

    public class Dwg : DwgLib_Interface
    {
        public void InitializeDrawingServices()
        {
        }

        public void DisposeDrawingServices()
        {
        }

        public void GetDrawingData(string inputFileName, string outputFileName) 
        {
        }

        public void WriteDrawingData(string modelDwgFullPath, string loopDwgFullPath, string dwgCommandsFileName, string resultsFileName)
        {
        }
    }
}

I would like to pass an array of the following structure as a parameter in one of the COM visible methods.

        struct dwgInsertRec
        {
            string blockName;
            string insertName;
            string insertHandle;
            string objectID;
            string layerName;
            double x;
            double y;
            double z;
            int qtyAttr;
            int qtyConstAttr;
            int qtyRows;
            int qtyColumns;
            string elementID;
        }

A variable declaration would look like this:  dwgInsertRec[] someArrayName;

I don't know where to put the struct definition to make this work.

Thanks,

Al



Replies

Martijn Bos on Tue, 23 Oct 2012 08:18:34


I'm not sure but can you not just make a public interface DwgInsertRec(dwgInsertRec[]){} that expects the struct as a variable?

Karmjit Singh on Tue, 23 Oct 2012 13:23:48


see if this helps

http://stackoverflow.com/questions/10716555/marshaling-an-array-of-c-sharp-structure-and-use-it-in-com-what-are-the-right-ma