【发布时间】: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