Problem with ArcObjects and Visual studio 2010 c#

5823
6
10-27-2011 01:56 AM
HusseinSayed
New Contributor
I'm getting started with ArcObject and i'm trying to build a console application that reads mdb file and shows it's contents but i have been working in it for a long time and i don't know what is the problem that i'm facing i'm using
-windows server 2008 sp2
-VS 2010 C#
-ArcGis 10.0 licence Type:ArcInfo
-ArcSDK 10
My code is as following
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
namespace DesktopConsoleApplication1
{
    class Program
    {
        [STAThread()]
        static void Main(string[] args)
        {

            IWorkspace iws;
            iws = AccessWorkspaceFromPath(@"C:\GD\Test.mdb");
        }
        public static IWorkspace AccessWorkspaceFromPath(String path)
        {
            //ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);

            IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ArcInfoWorkspaceFactoryClass();
            // Get FeatureClass to be set as datasource
            IWorkspace workspace = workspaceFactory.OpenFromFile(path, 0);
            return workspace;
        }
        
    }
}

when i us this code it give this error :
Retrieving the COM class factory for component with CLSID {1D887452-D9F2-11D1-AA81-00C04FA33A15} failed due to the following error: 80040111.

and when activating this line
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
it gives me this error :
Error HRESULT E_FAIL has been returned from a call to a COM component.

Please any one can help me to solve this problem.
Thank you.
0 Kudos
6 Replies
NeilClemmons
Regular Contributor III
Not only do you have to bind to a product runtime you also have to check out a license.  This should occur before you use any ArcObjects.  In your code you are declaring an IWorkspace instance before the call to Bind.  You need to move the call to Bind and add the code to check out the license.  Also, you are trying to open an Access personal geodatabase but your code is creating an instance of an ArcInfoWorkspaceFactory.  You need to be using an AccessWorkspaceFactory.  The workspace factory is a singleton object.  You shouldn't be using New to create the instance.  Instead, you should be using the Activator class.
0 Kudos
HusseinSayed
New Contributor
Not only do you have to bind to a product runtime you also have to check out a license.  This should occur before you use any ArcObjects.  In your code you are declaring an IWorkspace instance before the call to Bind.  You need to move the call to Bind and add the code to check out the license.  Also, you are trying to open an Access personal geodatabase but your code is creating an instance of an ArcInfoWorkspaceFactory.  You need to be using an AccessWorkspaceFactory.  The workspace factory is a singleton object.  You shouldn't be using New to create the instance.  Instead, you should be using the Activator class.


If you mean that the code be like this
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
namespace DesktopConsoleApplication1
{
    class Program
    {
        [STAThread()]
        static void Main(string[] args)
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
            IWorkspace iws;
            iws = AccessWorkspaceFromPath(@"C:\GD\Test.mdb");
        }
        public static IWorkspace AccessWorkspaceFromPath(String path)
        {
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
                (factoryType);
            IWorkspace workspace = workspaceFactory.OpenFromFile(path, 0);
            return workspace;
        }

    }
}

,i also try it before and it gave me this error:

System.Runtime.InteropServices.COMException was unhandled
  Message=Exception from HRESULT: 0x80040213
  Source=ESRI.ArcGIS.Geodatabase
  ErrorCode=-2147220973
  StackTrace:
       at ESRI.ArcGIS.Geodatabase.IWorkspaceFactory.OpenFromFile(String fileName, Int32 hWnd)
       at DesktopConsoleApplication1.Program.AccessWorkspaceFromPath(String path) in
..\DesktopConsoleApplication1\Program.cs:line 22
       at DesktopConsoleApplication1.Program.Main(String[] args) in

..\DesktopConsoleApplication1\DesktopConsoleApplication1\Program.cs:line 15
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: .
0 Kudos
NeilClemmons
Regular Contributor III
You need to check out a license after the call to Bind and before using any ArcObjects.
0 Kudos
HusseinSayed
New Contributor
You need to check out a license after the call to Bind and before using any ArcObjects.


I made some modifications on the code and it also give me this error :
System.Runtime.InteropServices.COMException was unhandled
  Message=Exception from HRESULT: 0x80040228
  Source=ESRI.ArcGIS.Geodatabase
  ErrorCode=-2147220952
  StackTrace:
       at ESRI.ArcGIS.Geodatabase.IWorkspaceFactory.OpenFromFile(String fileName, Int32 hWnd)
       at DesktopConsoleApplication1.Program.AccessWorkspaceFromPath(String path) in ..\DesktopConsoleApplication1\DesktopConsoleApplication1\Program.cs:line 27
       at DesktopConsoleApplication1.Program.Main(String[] args) in ..\DesktopConsoleApplication1\DesktopConsoleApplication1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

The modified code is as below :
using System;
using ESRI.ArcGIS.Geodatabase;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
namespace DesktopConsoleApplication1
{
    class Program
    {
        [STAThread()]
        static void Main(string[] args)
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
            IAoInitialize aoInit = new AoInitializeClass();
            aoInit.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
            IWorkspace iws;

            iws = AccessWorkspaceFromPath("Test.mdb");
            aoInit.Shutdown();
        }
        public static IWorkspace AccessWorkspaceFromPath(String path)
        {
            if (File.Exists(path)){
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory");
            IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
                (factoryType);
            IWorkspace workspace = workspaceFactory.OpenFromFile(path, 0);
            return workspace;
            }
            return null;
        }

    }
}
0 Kudos
NeilClemmons
Regular Contributor III
You should be checking the result of the call to Initialize to make sure the license is checked out and exit the program if it is not.  Also, you need to pass the full path to open the workspace and not just the file name.
0 Kudos
HusseinSayed
New Contributor
You should be checking the result of the call to Initialize to make sure the license is checked out and exit the program if it is not.  Also, you need to pass the full path to open the workspace and not just the file name.


For the file name i'm putting it in the Debug folder
but for the checking of the result of the call to Initialize i didn't understand it and don't know what to write.
0 Kudos