Search code examples
c#objectcomcreateinstance

CreateInstance doesn't work as expected


I`m currently working on a little program and my programming skills are not the best, but it works already pretty good, except this part.

I managed to start another program from my executable with the appending code. So if I loop through the following code snippet for the first time the program INCA will start and I am able to use the programm's API-functions.

But... when INCA is closed meanwhile and I run this code again nothing happens and I can't access the API, even if I start INCA manually afterwards.

    public bool Init()
    {
        var type = Type.GetTypeFromProgID( "Inca.Inca" );

        if ( type == null )
            return false;

        _inca = Activator.CreateInstance( type );

       return _inca != null;
    }

What am I missing?? Do I need to reassign or release the com object?


Solution

  • Close the api before creating a new instance (see comments of the question for details).

    public bool Init()
    {
        if ( _inca != null )
            _inca.Close();
        var type = Type.GetTypeFromProgID( "Inca.Inca" );
    
        if ( type == null )
            return false;
    
        _inca = Activator.CreateInstance( type );
    
        return _inca != null;
     }