C# 未处理InvalidCastException,以便在VB6中使用C

C# 未处理InvalidCastException,以便在VB6中使用C,c#,com,vb6,C#,Com,Vb6,我试图实现的是慢慢地将旧的VB6代码模块化为C。 我在VB6中创建了一个测试,按下一个按钮,执行我的c表单,并传回一个字符串值。 我昨天让它工作,但不知怎么的,有些东西改变了,它不再工作了。下面是我的C代码,我有问题 #define USE_COM using System; using System.Linq; using System.Windows.Forms; namespace CMS_DB_Operator_Input { static class Program

我试图实现的是慢慢地将旧的VB6代码模块化为C。 我在VB6中创建了一个测试,按下一个按钮,执行我的c表单,并传回一个字符串值。 我昨天让它工作,但不知怎么的,有些东西改变了,它不再工作了。下面是我的C代码,我有问题

#define USE_COM

using System;
using System.Linq;
using System.Windows.Forms;

namespace CMS_DB_Operator_Input
{
    static class Program
    {
        /// <summary>
        /// Open the OperatorInput Form
        /// 
        /// Note: Will output the result to console out on success
        /// </summary>
        /// <param name="args">0: The Default Filter to Use</param>
        /// <returns>
        ///     0: Failure to Select
        ///     1: Successfully Selected an Item
        /// </returns>
        [STAThread]
        static int Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string displayName = (args.Length > 1) ? args[1] : null;
            string result = "";
            displayName = "CBU35PN42";
            //displayName = "";

#if USE_COM
            CMS_DB_GateKeeper.IGateKeeperUI api = new MS_DB_GateKeeper.GateKeeperUI();
            result = api.AskOperatorInput(displayName); // <- INVALID CAST EXCEPTION was unhandled
#else
            CMS_DB_GateKeeper.CMS_DB_GateKeeper_OperatorInput op = new CMS_DB_GateKeeper.CMS_DB_GateKeeper_OperatorInput(displayName);
            if (displayName == null)
            {
                Application.Run(op);
                if (op.SelectedDisplayName != null)
                {
                    result = op.GetXmlResult(op.SelectedDisplayName); // Success
                }
            }
            else
            {
                result = op.GetXmlResult(displayName);
            }
#endif
            Console.WriteLine(result);

            return 0; 
        }
    }
}

它说加载类型库时出错,您是否验证了类型库已注册?若要在C中使用VB6中的方法,您不需要使用COM。您是否已经尝试简单地将VB6 dll作为引用导入?我正以另一种方式使用它。我从VB6中提取了一些代码,并将其转换为C。但我仍然必须在VB6中使用这些代码。我假设它在我的c代码中有问题。当我注释出一行时,它执行得很顺利,但它没有使用我需要它使用的函数,因此我可以从VB6调用它。该错误表示尝试加载接口的注册类型库时出错,该接口的方法将通过封送调用。你换了伪装吗?你忘了注销旧图书馆了吗?可能现在已经删除了,或者你忘了注册新图书馆了吗?谢谢你,保罗!成功了!!天啊!我更新了GUID并重新注册了DLL,它现在可以工作了。我太兴奋了!