Search code examples
delphiautomationautocad

Delphi 7 Automation to control AutoCAD “Invalid Class String”


My application, written in Delphi 7, sends commands to AutoCAD with ActiveX. It works fine for most of my customers, but in some rare cases it cannot communicate with AutoCAD and reports the error Invalid Class String (AutoCAD application is running OK). The error pops up after this line:

AcadV := GetActiveOleObject('AutoCAD.Application');

I have searched this problem a lot, but I could not find a working solution.


Solution

  • The small console application below shows how to check than the name of an automation object is registered in the Windows Registry. Automation objects need to have a registry entry in order that you can invoke them by name as in AutoCAD.Application

    Note that the IsRegistered function below does not guarantee that you will be able to access the object using CreateOleObject/GetActiveOleObject However, if the expected name is not found there is very likely something amiss, which you may be able to fix by re-installing the automation objects software.

    program CheckRegConsole;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils,
      Windows,
      Registry;
    
    function IsRegistered(const ClassString : String) : Boolean;
    var
      Reg : TRegistry;
    begin
      Result := False;
      Reg := TRegistry.Create(Key_Read);
      try
        Reg.RootKey := HKey_Classes_Root;
        Result := Reg.KeyExists(ClassString);
      finally
        Reg.Free;
      end;
    end;
    
    var
      S : String;
    begin
      S := 'Word.Application';  //  MS Word
      S := 'AcroExch.PDDoc';    //  Adobe Acrobat Document
      if IsRegistered(S) then
        writeln(S + ' registered')
      else
        writeln(S + ' not registered');
      readln;
    end.
    

    Btw, in my (limited) experience, GetActiveOleObject does not always succeed when it ought to. So if it fails, it may be worthwhile trying CreateOleObject instead.

    There is a very good book for Delphi programmers called "Delphi COM Programming" by Eric Harmon, if you can get hold of a copy, and there are countless automation tutorials on the internet. See e.g. https://msdn.microsoft.com/en-us/library/windows/desktop/ms221375(v=vs.85).aspx for the MS take on the subject.