Click here to Skip to main content
15,885,546 members
Articles / Mobile Apps / Windows Mobile

Bringing DCOM remoting functionality to Windows CE and .NET CF2.0

Rate me:
Please Sign up or sign in to vote.
4.93/5 (11 votes)
17 Apr 2006CPOL14 min read 96.9K   513   40  
This article shows how to use DCOM on Windows CE 5.0. We will add full DCOM rich error information, and implement a DCOM interface between a Windows XP .NET 2.0 client and Windows CE DCOM server. With this code, it is possible to code .NET remoting alike functionality through DCOM interop.
////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2006 Werner Willemsens
//
////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ComError
{
    class ComError
    {
        [System.Runtime.InteropServices.DllImport("oleaut32.dll")]
        private static extern uint GetErrorInfo(uint dwReserved, /* IErrorInfo** */ref IntPtr ppErrorInfo);

        [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
        public struct ErrorInfo
        {
//          public uint m_cRefs;
            public Guid m_guid;
            public string m_bstrSource;
            public string m_bstrDescription;
            public string m_bstrHelpFile;
            public uint m_dwHelpContext;
        };

        public static System.Runtime.InteropServices.COMException GetRichErrorInfo(System.Runtime.InteropServices.COMException error)
        {
            System.Runtime.InteropServices.COMException error2;
#if WindowsCE
            if ((error.ErrorCode & 0x0000FFFF) > 0x01FF)
            {
                uint HRESULT;
                ErrorInfo err = new ErrorInfo();
                IntPtr ptr = new IntPtr(0);

                HRESULT = GetErrorInfo(0, ref ptr);

                if (HRESULT == 0)
                {
                    // Guid
                    IntPtr pAddressGuid = new IntPtr((int)ptr.ToInt32() + 12);
                    byte[] bGuid = new byte[16];
                    Marshal.Copy(pAddressGuid, bGuid, 0, 16);

                    err.m_guid = new Guid(bGuid);

                    // HelpContext
                    IntPtr pAddressHelpContext = new IntPtr((int)ptr.ToInt32() + 40);
                    int[] iHelpContext = new int[1];
                    Marshal.Copy(pAddressHelpContext, iHelpContext, 0, 1);

                    err.m_dwHelpContext = (uint)iHelpContext[0];

                    // Description
                    IntPtr ppAddressDescription = new IntPtr((int)ptr.ToInt32() + 32);
                    int[] iAddressDescription = new int[1];
                    Marshal.Copy(ppAddressDescription, iAddressDescription, 0, 1);

                    if (iAddressDescription[0] != 0)
                    {
                        IntPtr pAddressDescriptionCount = new IntPtr((int)iAddressDescription[0] - 4);
                        int[] AddressDescriptionCount = new int[1];
                        Marshal.Copy(pAddressDescriptionCount, AddressDescriptionCount, 0, 1);

                        IntPtr pAddressDescription = new IntPtr((int)iAddressDescription[0]);
                        char[] aDescription = new char[AddressDescriptionCount[0]];
                        Marshal.Copy(pAddressDescription, aDescription, 0, AddressDescriptionCount[0] - 1);

                        err.m_bstrDescription = new string(aDescription);
                    }
                    else
                        err.m_bstrDescription = null;

                    // Source
                    IntPtr ppAddressSource = new IntPtr((int)ptr.ToInt32() + 28);
                    int[] iAddressSource = new int[1];
                    Marshal.Copy(ppAddressSource, iAddressSource, 0, 1);

                    if (iAddressSource[0] != 0)
                    {
                        IntPtr pAddressSourceCount = new IntPtr((int)iAddressSource[0] - 4);
                        int[] AddressSourceCount = new int[1];
                        Marshal.Copy(pAddressSourceCount, AddressSourceCount, 0, 1);

                        IntPtr pAddressSource = new IntPtr((int)iAddressSource[0]);
                        char[] aSource = new char[AddressSourceCount[0]];
                        Marshal.Copy(pAddressSource, aSource, 0, AddressSourceCount[0] - 1);

                        err.m_bstrSource = new string(aSource);
                    }
                    else
                        err.m_bstrSource = null;

                    // HelpFile
                    IntPtr ppAddressHelpFile = new IntPtr((int)ptr.ToInt32() + 36);
                    int[] iAddressHelpFile = new int[1];
                    Marshal.Copy(ppAddressHelpFile, iAddressHelpFile, 0, 1);

                    if (iAddressHelpFile[0] != 0)
                    {
                        IntPtr pAddressHelpFileCount = new IntPtr((int)iAddressHelpFile[0] - 4);
                        int[] AddressHelpFileCount = new int[1];
                        Marshal.Copy(pAddressHelpFileCount, AddressHelpFileCount, 0, 1);

                        IntPtr pAddressHelpFile = new IntPtr((int)iAddressHelpFile[0]);
                        char[] aHelpFile = new char[AddressHelpFileCount[0]];
                        Marshal.Copy(pAddressHelpFile, aHelpFile, 0, AddressHelpFileCount[0] - 1);

                        err.m_bstrHelpFile = new string(aHelpFile);
                    }
                    else
                        err.m_bstrHelpFile = null;
                }

                error2 = new System.Runtime.InteropServices.COMException(err.m_bstrDescription, error.ErrorCode);
            }
            else
#endif
            {
                error2 = error;
            }

            return error2;
        }
    }
}

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
Team Leader
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions