【问题标题】:C# P/Invoke embedded VB6 DLL resource: ActiveXObject "Unable to find an entry point named 'FunctionName' in DLL 'DLLName'."C# P/Invoke 嵌入式 VB6 DLL 资源:ActiveXObject “无法在 DLL 'DLLName' 中找到名为 'FunctionName' 的入口点。”
【发布时间】:2012-04-26 23:07:57
【问题描述】:

我正在尝试为 VB6 DLL 创建 C# 包装器 DLL,然后在网页中将该包装器用作 ActiveXObject,但在调用 ClassTesting() 时出现此错误:

无法在 DLL 'VB6DLL' 中找到名为 'ClassTest' 的入口点。

应用程序将 DLL 导出到临时目录,然后将其加载到内存中。 DLL的结构可以描述为:

VB6DLL.dll -> 公共类“VB6.cls” -> 公共函数“ClassTest()”。

C#代码如下:

namespace SystemDeviceDriver
{
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IDeviceDriver
    {
        [DispId(1)]
        string ClassTesting();
    }

    [Guid("655EE123-0996-4c70-B6BD-7CA8849799C7")]
    [ComSourceInterfaces(typeof(IDeviceDriver))]
    public class DeviceDriver : IDeviceDriver
    {

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern IntPtr LoadLibrary(string lpFileName);

        [DllImport("VB6DLL", CharSet = CharSet.Unicode)]
        static extern string ClassTest();

        public DeviceDriver()
        {
            //Write the VB6DLL to a temp directory
            string dirName = Path.Combine(Path.GetTempPath(), "SystemDeviceDriver." + Assembly.GetExecutingAssembly().GetName().Version.ToString());
            if (!Directory.Exists(dirName))
            {
                Directory.CreateDirectory(dirName);
            }
            string dllPath = Path.Combine(dirName, "VB6DLL.dll");
            File.WriteAllBytes(dllPath, SystemDeviceDriver.Properties.Resources.VB6DLL);

            //Load the library into memory
            IntPtr h = LoadLibrary(dllPath);
            Debug.Assert(h != IntPtr.Zero, "Unable to load library " + dllPath);
        }

        public string ClassTesting()
        {
            return ClassTest();
        }
    }
}

【问题讨论】:

  • 您将 P/Invoke 与 COM 混淆了。
  • 我是否正确阅读了这个问题? VB6 DLL已经是一个 ActiveX 对象!直接使用!为什么需要包装器?
  • 对不起,我的困惑。我正在尝试将 VB6 com 对象包装在 C# com 对象中,因为 VB6 com 对象与系统上的驱动程序存在某种冲突。

标签: c# dll vb6 wrapper activexobject


【解决方案1】:

DllImport / P/Invoke 函数用于包含“旧 C 风格”的 dll 文件,因此从库中导出的简单函数

这里列出了调用方法,对于哪些函数类型是可能的: http://msdn.microsoft.com/de-de/library/system.runtime.interopservices.callingconvention.aspx

COM 完全不同,见:http://en.wikipedia.org/wiki/Component_Object_Model

COM dll 通常导出的唯一函数是 DllRegisterServer、DllUnregisterServer 您可以先使用 P/Invoke 函数来调用该函数。 COM dll 文件在注册表中注册自己。那么应该可以创建一个 COM 对象。

【讨论】:

  • +1 用于解释。但我不确定尝试在运行时注册 COM DLL 的好处。为什么不手动或在安装脚本中注册 COM DLL,然后继续实现一些实际功能?
猜你喜欢
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2017-08-31
  • 1970-01-01
相关资源
最近更新 更多