开发者

COM returns type that does not implement any interface

I need to automate some tasks in Adobe InDesign CS3 from a .NET 4.0 application. I've added a reference to the InDesign Type Library using the Add Reference dialog in Visual Studio. It genereates an interop assembly, which correctly includes all of the interfaces and types declared in the type library. I haven't installed any Adobe SDK, as the type library was available in Visual Studio without installing anything but Adobe InDesign CS3.

The interesting types in the interop assembly for me right now is the interfaces _Application and Application, and the class ApplicationClass. Here is the definition of them, so you can see the relationship between them:

public interface _Application
{
    // Lots of properties and methods
}

public interface Application : _Application
{
    // Empty
}

public class ApplicationClass : _Application, Application
{
    // The same properties and methods as declared in _Application
}

I try to instantiate the COM object like this:

Type oType = Type.GetTypeFromProgID("InDesign.Application.CS3");
if (oType != null)
{
    object instance = Activator.CreateInstance(oType);
}

This code succeeds. I get an instance, but of a type called __ComObject. From what I know, this is completely normal.

Now, here's where the fun begins. For this instance to be usable to me, I should cast it to the correct interface. From other examples on the net, and from the documentation available here, I can see I should cast it to the Application interface. But if I do that, I get a nasty InvalidCastException saying that the type System.__ComObject doesn't support this interface. I get the same exception if I try to cast it to ApplicationClass or the _Application interface.

I thought I was perhaps using an incorrect interface, so I tried implementing the utility function listed here. This function loops through all the interfaces declared in the interop assembly and queries the IUnknown interface if it implements the interface.

When I use that function, it returns null, meaning that the instance I get back from CreateInstance supports none of the interfaces in the interop assembly. Surely, that can't be right!?

However, if I inspect the instance variable with the Visual Studio Debugger, there's something called "Dynamic View". If I expand that, it lists all the properties for the object and all the properties match the properties from the ApplicationClass class and the _Application interface. So I tried using Type.InvokeMember and that works:

oType.InvokeMember("DoScript", BindingFlags.InvokeMethod,开发者_StackOverflow中文版 null, instance, oArguments, CultureInfo.InvariantCulture);

This actually works, but it would be extremely cumbersome to interact with the COM object like this, and I need to do alot of interaction with the COM object, so this is not really usable. I guess I could make a wrapper for the COM objects, but that kind of defeats the purpose of the interop assembly, and I don't want to create 700+ wrapper classes.

I've searched alot, and I've found tutorials and examples of using the InDesign COM object, but they all just cast the instance returned to the Application interface, but as explained, this doesn't work in my case.

I've also tried the following code instead of the code above:

InDesign.Application app = new InDesign.Application();
app.Activate();

The first line succeeds and I get an instance of ApplicationClass, but when it tries to execute the second line I get a InvalidCastException stating that ApplicationClass cannot be converted to the interface _Application.

I'm really cornered up here, and not sure what to try next. I really hope someone more experienced with COM and .NET has an idea of what I could be doing wrong.

Thanks in advance, and sorry for such a long post.


You have to use a Runtime Callable Wrapper

The method you need is this one

Try this :

    Type oType = Type.GetTypeFromProgID("InDesign.Application.CS3");
    if (oType != null)
    {
        object instance = Activator.CreateInstance(oType);// or any other way you can get it
        Application app = 
            (Application)System.Runtime.InteropServices.Marshal.CreateWrapperOfType(instance, typeof(ApplicationClass));
    }


@Seb solution is the correct one, but while working on it, I came across error "Source object cannot be converted to the destination type since it does not support all the required interfaces." (as @MikeKeskinov wrote it in comments).

To get rid of that error, you need to register COM objects by doing these steps (already explained here) - this is for InDesign CC2021 v16.0 (it's similar for other versions)

  • Close all instances of InDesign using Task Manager
  • Go to C:\ProgramData\Adobe\InDesign\Version 16.0\en_US\Scripting Support\16.0 and rename tlb file to tlb.bak (or something similar, it doesn't matter)
  • Open command window (CMD) as administrator
  • Go to InDesign EXE folder C:\Program Files\Adobe\Adobe InDesign 2021
  • Start command indesign.exe -type
  • Reference newly created tlb file to project

And after that it should work. Hope I helped to someone!


I didn't work with COM recently but as far I remember (and understood your problem) you can not cast ComObject like this:

   Application app = (Application)comObject;

but you should use as operator:

    Application = comObject as Application;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜