想做一个训练软件,实现的功能是给游戏提供一个接口,训练软件就可以外加载游戏并且可以控制游戏,现在想把游戏做成dll文件(如dodger.dll),公共接口Myinterface也做成dll文件(Myinterface.dll),那应该怎么在程序中通过接口将游戏加载进去呢?并显示游戏窗口??

解决方案 »

  1.   

    regsvr32  注册一下要用到的dll
    然后项目添加引用此dll/
    C# 里面调用里面的方法之类的 实现加载。显示窗口。
      

  2.   

    dll可以返回一个窗口句柄,然后你用api函数 SetParent  设置窗体的parent为你的程序,具体用时具体分析吧
      

  3.   

    如果DLL是C#编写的,直接添加引用;
    如果DLL是C++写的,用DllImport方式加载;
      

  4.   

    下面是加载dll文件的程序:
    private MyInterFace.InterFace iObj;//定义一个MyInterFace实例
    Assembly assembly = Assembly.LoadFrom(str + ".dll");
                Type type = assembly.GetType(str + ".Form1");
                try
                {
                    object obj = Activator.CreateInstance(type, new Object[]{300,400} );
                    this.iObj = (MyInterFace.InterFace)obj;
                    //iObj.InitializeGraphics();
                }
                catch (System.Reflection.TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
                //反馈刺激中的函数——初始化引擎;
                iObj.InitializeGraphics();
                iObj.myShow();
    在没对所加载的dll文件进行注册时,运行到object obj = Activator.CreateInstance(type, new Object[]{300,400}的时候,程序就卡死在那了,但是也没给出错误提示,注册dll文件之后还是出现相同的情况,就不知道到底是哪出问题了
      

  5.   


    dll文件都是用C#编写的,也加入引用了,但还是运行到object obj = Activator.CreateInstance(type, new Object[]{300,400}这个地方程序就不往下运行了
      

  6.   

    在附上程序:private Thread m_GameThread;
    private void startGameProcess()
    {
    this.m_GameThread = new Thread(new ThreadStart(this.gameStart));
    this.m_GameThread.Start();

    }
    private GameInterFace my_GameIF; /// <summary>
    /// 游戏调用的函数
    /// 关于游戏的调用,这里采用的方法是,线程启动一个窗口,窗口通过调用接口,
    /// 实现对dll的动态加载和调用。
    /// 这是由于线程启动Application.Run方法中,Run的对象必须是一个窗口,不能是一个接口;
    /// </summary>
    private void gameStart()
    {
    string str = "Classroom"; //加载游戏dll的名称,这里直接输入,事实上,这里需要通过数据库读出;

    this.my_GameIF = new GameInterFace();
    this.my_GameIF.Start(str, 200, 300); //30,50 dll Form出现的位置x y坐标,这里直接输入事实上,从数据库中读出;
    Application.Run(this.my_GameIF);
    }
    public void Start(string str, int x, int y)
    {
    Assembly assembly = Assembly.LoadFrom(str + ".dll");
    Type type = assembly.GetType(str + ".Form1");
    // try
    // {
    object obj = Activator.CreateInstance(type, new Object[]{x, y}); this.iObj = (MyInterFace.InterFace)obj;
    // }
    // catch(System.Reflection.TargetInvocationException ex) 
    // {
    // throw ex.InnerException;
    // }
    //反馈刺激中的函数——初始化引擎;
    iObj.InitializeGraphics();
    iObj.myShow();
    }
    在object obj = Activator.CreateInstance(type, new Object[]{x, y})这个报错,错误提示为“未处理的“System.Reflection.TargetInvocationException”类型的异常出现在 mscorlib.dll 中。其他信息: 调用的目标发生了异常。
    ”找了好久都没有找到原因,
    请各位高手帮帮忙