Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / ATL

C# ATLCOM Interop code snipperts - Part 1

Rate me:
Please Sign up or sign in to vote.
4.66/5 (18 votes)
21 Dec 2008CPOL14 min read 51.4K   485   32  
Code snippets detailing Interop programming in C# and ATLCOM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Launcher
{
    public partial class InteropDemo1 : Form
    {
        #region Form Code
        int _NumbersCount;
        ATLSimpleObjectDemoLib.SomeClassClass _SomeClassObject = null;


        public InteropDemo1()
        {
            InitializeComponent();
            _NumbersCount = 10;
            _SomeClassObject = new ATLSimpleObjectDemoLib.SomeClassClass();
        }

        #endregion Form Code

        #region Single Dimensional Real Array
        private void btnRun_Click(object sender, EventArgs e)
        {
            try
            {
                float[] Numbers = new float[_NumbersCount];

                for (int inx = 0; inx < _NumbersCount; inx++)
                {
                    Numbers[inx] = inx;
                }
                _SomeClassObject.PutRealNumbers(_NumbersCount - 3, ref Numbers[2]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Launcher", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {

            }
            return;
        }

        private void btnGet_Click(object sender, EventArgs e)
        {

            float[] Numbers = new float[10];

            _SomeClassObject.GetRealNumbers(2, ref Numbers[4]);

            string str = "";
            for (int inx = 0; inx < 2; inx++)
            {
                str += Numbers[4 + inx].ToString() + "\n";
            }
            MessageBox.Show(str);
        }
        #endregion Single Dimensional Real Array

        #region Not Good Interop
        private void btnNotGoodInterop_Click(object sender, EventArgs e)
        {
            float[] Numbers = new float[_NumbersCount];

            for (int inx = 0; inx < _NumbersCount; inx++)
            {
                Numbers[inx] = inx;
            }
            _SomeClassObject.NotGoodInterop(2, ref Numbers[3]);
        }
        #endregion Not Good Interop

        #region Multi Dimensional Real Array
        private void btnPutMultiDimArray_Click(object sender, EventArgs e)
        {
            float[,] TwoDimNumbers = new float[_NumbersCount / 2, _NumbersCount / 3];

            for (int inx = 0; inx < _NumbersCount / 2; inx++)
            {
                for (int jnx = 0; jnx < _NumbersCount / 3; jnx++)
                {
                    TwoDimNumbers[inx, jnx] = inx * 10 + jnx;
                }
            }
            _SomeClassObject.PutMultiDimensionalArray(TwoDimNumbers);
        }

        private void btnGetMultiDimArray_Click(object sender, EventArgs e)
        {
            Array TwoDimNumbers;
            _SomeClassObject.GetMultiDimensionalArray(out TwoDimNumbers);
        }
        #endregion Multi Dimensional Real Array

        #region String Array
        private void btnPutStrings_Click(object sender, EventArgs e)
        {

            string[] Strings = new string[5];

            Strings[0] = "Code Project";
            Strings[1] = "N. Gopi Krishna";
            Strings[2] = "B.Tech (C.S.E)";
            Strings[3] = null;
            Strings[4] = "ngopikrishna81@yahoo.com";
            _SomeClassObject.PutStrings(Strings);
        }

        private void GetStrings_Click(object sender, EventArgs e)
        {
            string[] Strings = null;
            Array StringsArray = null;
            _SomeClassObject.GetStrings(out StringsArray);
            Strings = StringsArray as string[];
        }

        #endregion String Array

        #region Enum And Structure
        private void btnEnumAndStructure_Click(object sender, EventArgs e)
        {
            ATLSimpleObjectDemoLib.SData data = new ATLSimpleObjectDemoLib.SData();


            data.eEnumVal = ATLSimpleObjectDemoLib.MyEnum.Bad;
            data.Id = 0;
            data.Name = "Lee Van Cleef";


            _SomeClassObject.SampleEnumAndStruct(ATLSimpleObjectDemoLib.MyEnum.Bad, data);
        }
        #endregion Enum And Structure

        #region CStyle function
        [DllImport("CStyleDLL.dll")]
        public static unsafe extern void SamplePutFunction(int nArraySize, int* InputArray);

        [DllImport("CStyleDLL.dll")]
        public static unsafe extern void SamplePutFunction2(int nArraySize1, int nArraySize2, int** InputArray);


        [DllImport("CStyleDLL.dll")]
        public static unsafe extern void SampleGetFunction(long* nArraySize, int** InputArray);


        private void btnPutCStyleArray_Click(object sender, EventArgs e)
        {
            int[] IntArray = new int[100];
            unsafe          // <---- In 2008, this requires an explicit compiler option
            {               
                fixed (int* pArray = IntArray)  //  <--- fixes the pointer during GC.
                {
                    SamplePutFunction(IntArray.Length, pArray);
                }
            }
        }

        private void btnPutCStyleArray2_Click(object sender, EventArgs e)
        {
            //int nSize1 = 10,
            //    nSize2 = 20;
            //int[,] IntArray = new int[nSize1,nSize2];

            //unsafe
            //{
            //    fixed (int* ppArray = IntArray)
            //    {
            //        SamplePutFunction2(nSize1,nSize2, ppArray);
            //    }
            //}
        }

        #endregion CStyle function
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I am a C++ developer for the past 9 years. I have been tinkering with C# for the past 2,3 years. Professionally, I am a VC++ programmer in a software consultancy company for the past 6 ½ years.

Comments and Discussions