简体   繁体   English

CreateInstance无法正常工作

[英]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. 因此,如果我是第一次循环浏览以下代码段,则程序INCA将启动,并且能够使用该程序的API函数。

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. 但是...同时关闭INCA并再次运行此代码时,即使之后手动启动INCA ,也没有任何反应,并且我无法访问API。

    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? 我是否需要重新分配或释放com对象?

Close the api before creating a new instance (see comments of the question for details). 在创建新实例之前,请关闭api(有关详细信息,请参阅问题的注释)。

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;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM