COM Class factory error when trying to instantiate an RGBColorClass

3685
9
03-23-2011 08:46 AM
DaviKucharski
New Contributor II
I have the following code trying to change the selectioncolor property of one of my layers. When it tries the line in red I get the below error. Please help.

Dim pLD1 As ESRI.ArcGIS.Carto.ILayerDescription = mLayerDescs.Element(2)

Dim color As ESRI.ArcGIS.Display.RgbColor = New ESRI.ArcGIS.Display.RgbColorClass
color.Blue = 210
color.Red = 0
color.Green = 0

pLD1.SelectionColor = color

ERROR: Retrieving the COM class factory for component with CLSID {7EE9C496-D123-11D0-8383-080009B996CC} failed due to the following error: 80040154.
0 Kudos
9 Replies
NeilClemmons
Regular Contributor III
You're mixing usage of the managed .NET class and the COM class.  You may want to read the section on Interoperating with COM in the developer help for an explanation.

Dim color As ESRI.ArcGIS.Display.RgbColor = New ESRI.ArcGIS.Display.RgbColorClass

Use this (interface type w/ COM class)
Dim color As ESRI.ArcGIS.Display.IRgbColor = New ESRI.ArcGIS.Display.RgbColor

or this (managed .NET class)
Dim color As ESRI.ArcGIS.Display.RgbColorClass = New ESRI.ArcGIS.Display.RgbColorClass
0 Kudos
erkansahin
New Contributor
Hi,
my project was produced at vs 2008,arcgis 9.3 and i update it to vs 2010,arcgis 10.1 but this row  "IRgpColorClass x=new RgbColoClass "  throw exception COM Class factory error when trying to instantiate an RGBColorClass.
i try this codes

Use this (interface type w/ COM class)
Dim color As ESRI.ArcGIS.Display.IRgbColor = New ESRI.ArcGIS.Display.RgbColor

or this (managed .NET class)
Dim color As ESRI.ArcGIS.Display.RgbColorClass = New ESRI.ArcGIS.Display.RgbColorClass

but thats not work.i change x64 to x86 try alot of way but i cant resolve this problem.anybody can help me?
0 Kudos
GwenRoyakkers
New Contributor III

Have you found a solution for this problem?

I have just ported a visual studio 2010 project to 2012 and now I have the same error.

The code was not changed but i can not compile because the dll won't register.

Microsoft .NET Framework Assembly Registration Utility version 4.0.30319.18408

for Microsoft .NET Framework version 4.0.30319.18408

Copyright (C) Microsoft Corporation.  All rights reserved.

RegAsm : warning RA0000 : Registering an unsigned assembly with /codebase can ca

use your assembly to interfere with other applications that may be installed on

the same computer. The /codebase switch is intended to be used only with signed

assemblies. Please give your assembly a strong name and re-register it.

RegAsm : error RA0000 : An error occurred inside the user defined Register/Unreg

ister functions: System.TypeInitializationException: The type initializer for 'K

ragtenUtilityToolBar.MeasureAngleBetweenTool' threw an exception. ---> System.Ru

ntime.InteropServices.COMException: Retrieving the COM class factory for compone

nt with CLSID {7EE9C496-D123-11D0-8383-080009B996CC} failed due to the following

error: 80040111 ClassFactory cannot supply requested class (Exception from HRES

ULT: 0x80040111 (CLASS_E_CLASSNOTAVAILABLE)).

The stacktrace points me to the row where I initialize a RgbColorClass.

This code did compile and register for COM Interop  in visual studio 2010

Any ideas?

Thanks

0 Kudos
nicogis
MVP Frequent Contributor

have you set project x86? have you target framowork 3.5 or 4.0 (see ArcObjects Help for .NET developers  for 4.0) ?

0 Kudos
GwenRoyakkers
New Contributor III

Yes, I had.

But I found the solution to the problem (in my situation).

The RgbColorClass was in a private static field of the the COM Registered class.

I initialized it immediately to a new object with braces.

"private static IRgbColor lineColor = new RgbColorClass() { Red = 255, Green = 217, Blue = 128, UseWindowsDithering = true };"

This is the row of code that did compile, but didn't register.

I Changed it to:

"private IRgbColor lineColor = null;"

And I initialized it in the constructor of the BaseTool:

"lineColor = new RgbColorClass() { Red = 255, Green = 217, Blue = 128, UseWindowsDithering = true };"

Now it did register correctly.

I have absolutely no idea why it didn't register when I initialized the static variable on declaration.

This Tool did compile and register on Arcgis 10.1 with a visual studio 2010 project.

Now on Arcgis 10.2.2 and visual studio 2012 I had to move the initialization to the constructor.

The reason why it was a static var is because the tool always used the same color to show a line.

(it doesn't change at runtime.)

Does anyone have a clue why I had to change this in the code?

It took me quite a while to figure this out, and because I don't know why, I might have the same problem on a different object in the future.

I hope this might help someone else.

Thanks

0 Kudos
nicogis
MVP Frequent Contributor

The reason why it was a static var is because the tool always used the same color to show a line

do you need create new instances of tool?

Have you same error if you inizialize your field in a ctor static ?

class TestColor

    {

        public static IRgbColor lineColor = null;

        static TestColor()

        {

            lineColor = new RgbColorClass() { Red = 255, Green = 217, Blue = 128, UseWindowsDithering = true };

        }

        public TestColor(){}

    }

0 Kudos
GwenRoyakkers
New Contributor III

I did solve it in the previous post by doing what you say.

However I will also post the code that compiles, but can't be registered.

I Changed it to:

"private static IRgbColor lineColor = null;"

And I initialized it in the constructor of the BaseTool:

"lineColor = new RgbColorClass() { Red = 255, Green = 217, Blue = 128, UseWindowsDithering = true };"

I'm still curious why this code doesn't register (It does compile correctly)

[Guid("cadf1659-3e05-4509-a668-12537f2ffea8")]

    [ClassInterface(ClassInterfaceType.None)]

    [ProgId("KragtenUtilityToolBar.MeasureAngleTool")]

    public sealed class MeasureAngleTool : BaseTool

    {

        #region COM Registration Function(s)

        [ComRegisterFunction()]

        [ComVisible(false)]

        static void RegisterFunction(Type registerType)

        {

            // Required for ArcGIS Component Category Registrar support

            ArcGISCategoryRegistration(registerType);

            //

            // TODO: Add any COM registration code here

            //

        }

        [ComUnregisterFunction()]

        [ComVisible(false)]

        static void UnregisterFunction(Type registerType)

        {

            // Required for ArcGIS Component Category Registrar support

            ArcGISCategoryUnregistration(registerType);

            //

            // TODO: Add any COM unregistration code here

            //

        }

        #region ArcGIS Component Category Registrar generated code

        /// <summary>

        /// Required method for ArcGIS Component Category registration -

        /// Do not modify the contents of this method with the code editor.

        /// </summary>

        private static void ArcGISCategoryRegistration(Type registerType)

        {

            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);

            MxCommands.Register(regKey);

        }

        /// <summary>

        /// Required method for ArcGIS Component Category unregistration -

        /// Do not modify the contents of this method with the code editor.

        /// </summary>

        private static void ArcGISCategoryUnregistration(Type registerType)

        {

            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);

            MxCommands.Unregister(regKey);

        }

        #endregion

        #endregion

        private IApplication m_application;

        private bool m_inUse;

        private ILineSymbol m_lineSymbol;

        private IPolyline m_polyline;

        private ITextSymbol m_textSymbol;

        private IPoint m_startPoint;

        private IPoint m_textPoint;

        private double m_angleSave;

        private double m_distance;

        private static IRgbColor LijnKleur = new RgbColorClass() { Red = 255, Green = 217, Blue = 128, UseWindowsDithering = true };

        private static IRgbColor TextKleur = new RgbColorClass() { Red = 68, Green = 118, Blue = 255, UseWindowsDithering = true };

        public MeasureAngleTool()

        {

            //

            // TODO: Define values for the public properties

            //

            base.m_category = "Kragten Utility Toolbar"; //localizable text

            base.m_caption = "Measure Angle";  //localizable text

            base.m_message = "...";  //localizable text

            base.m_toolTip = "...";  //localizable text

            base.m_name = "Kragten_KragtenUtilityToolBarAddIn_MeasureAngleTool";   //unique id, non-localizable (e.g. "MyCategory_ArcMapTool")

            try

            {

                //

                // TODO: change resource name if necessary

                //

                string bitmapResourceName = GetType().Name + ".bmp";

                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);

                base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");

            }

            catch (Exception ex)

            {

                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");

            }

          

        }

// Other BaseTool implementation...
0 Kudos
nicogis
MVP Frequent Contributor

A type with static variables with initializer expressions, but no static constructors, can exhibit subtle differences in the timing of the type initializer execution

set it in ctor static so you sure the initialization. however it hasn't sense if you don't need instance new MeasureAngleTool

GwenRoyakkers
New Contributor III

Thanks!

I didn't know this.

This is something I need to keep in mind when designing/porting other tools.

0 Kudos