E_NOINTERFACE fail to cast IGeoDataset to ISaveAs

862
4
07-05-2012 09:27 AM
GraceCai
New Contributor
Hi All,

I'm new to ArcObjects and C# and currently I'm trying to take an IRasterBand object and write it to file. The IRasterBand has been cast to IGeoDataset before this:
ISaveAs saveAs = (ISaveAs)EucDistRas; //this is the line that causes the failure
saveAs.SaveAs(targetName, targetWorkspace, "GRID");


The error I'm getting is as follows:

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Geodatabase.ISaveAs'. This operation failed because the QueryInterface call on the COM component for the interface with IID'{C7A9E97-597E-4A69-A872-29CB60B5E0DB}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

This procedure seems pretty standard. I've seen ESRI help docs that uses this code. I've read that dll registration might cause E_NOINTERFACE errors. So I've unregistered and then re-registered the ESRI.ArcGIS.Geodatabase dll (which ISaveAs resides in). But it doesn't seem to help. If anyone has any advice that would be much appreciated!

Thanks :]

Grace
0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor
Although you can cast an IRasterBand as an IGeodataset, only MosiacRasters, Rasters, RasterDatasets, and RendererRasters can implement ISaveAs. Can you use IRasterBand::Copy instead?
0 Kudos
GraceCai
New Contributor
Oh thank you very much! I've tried .Copy on the IRasterBand but I got the error E_FAIL

System.Runtime.InteropService.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component. at ESRI.ArcGIS.DataSourcesRaster.IRasterBand.Copy(String copyName, IWorkspace copyWorkspace)...

I'm not sure where to go with that error message... :confused:
0 Kudos
KenBuja
MVP Esteemed Contributor
Can you post your code?
0 Kudos
GraceCai
New Contributor
Here's my code :]

String targetWorkspacePath = @"O:\EPScratch\GeomaticsGrace\NTR\TestOutput.gdb";
String targetName = "testOut";
IWorkspace targetWorkspace = (IWorkspace)OpenFeatureWorkspace(targetWorkspacePath);

IRasterBand EucDistRas = CreateDistanceOpEucDistanceFullDistanceRaster(FClass as IGeoDataset);
EucDistRas.Copy(targetName, targetWorkspace);



IFeatureWorkspace OpenFeatureWorkspace(string filePath)
        {
            IWorkspaceFactory WorkspaceFactory = new FileGDBWorkspaceFactoryClass();
            IWorkspace Workspace = WorkspaceFactory.OpenFromFile(filePath, 0);
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)Workspace; // Explict Cast
            return featureWorkspace;
        }


    public IRasterBand CreateDistanceOpEucDistanceFullDistanceRaster(IGeoDataset geoDataset)
    {
        try
        {
            if (geoDataset is IRaster | geoDataset is IRasterDataset | geoDataset is IRasterBand | geoDataset is IRasterDescriptor | geoDataset is IFeatureClass)
            {

                // Create the RasterDistanceOp object   
                IDistanceOp distanceOp = new RasterDistanceOpClass();
                object euc_maxDistance = Type.Missing;
                object object_missing2 = Type.Missing;

                IGeoDataset geoDataset_result = distanceOp.EucDistanceFull(geoDataset, true, false, false, ref euc_maxDistance, ref object_missing2);
                
                // Access the Euclidean distance raster from the output generated
                IRasterBandCollection rasterBandCollection = (IRasterBandCollection)geoDataset_result; // Explicit Cast
                IRasterBand rasterBand = rasterBandCollection.Item(0);
                IGeoDataset geoDataset_output = (IGeoDataset)rasterBand; // Explicit Cast

                return rasterBand;
            }
            else
            {

                //Invalid type of GeoDataset for this process
                MessageBox.Show("CreateDistanceOpEucDistanceFullDistanceRaster Error: invalid input");
                return null;
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
            return null;
        }

    }
0 Kudos