揭露DLL为COM Interop [英] Expose dll for COM Interop
问题描述
我想我知道如何做到这一点,但显然不是,所以我会很感激一些帮助!
我不能让我的DLL来让我注册实例可以在一个VBS,或其他地方。
我写了下面的示例类,检查使装配COM可见,选中注册为COM Interop,然后建造它。
当我尝试从VBS实例化它,我得到了ActiveX组件不能创建对象错误
这是类代码:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
命名空间的Smurf
{
公共类裤
{
公共字符串爆炸(布尔大声)
{
字符串结果;
如果(大声)
结果=砰;
,否则
结果=啪;
返回结果;
}
}
}
...这是VBS的:
暗淡了
将一个=的CreateObject(Smurf.Pants)
MSGBOX(OK)
我需要做的做什么?
感谢:)
忘了提,第一次失败后,我尝试REGSVR32和REGASM - !没有帮助
[/编辑]
需要注意的是,当我尝试REGSVR32,我得到这个消息:
模块C:... \Smurf.dll已加载,但入境 - 点中的DllRegisterServer没有被发现。
确保C:... \Smurf.dll是一个有效的DLL或OCX文件,然后再试
如何有用的是??
这是代码的最新版本:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;使用System.Runtime.InteropServices
;
命名空间的Smurf
{
[的Guid(EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F)]
公共接口IPants
{
〔DISPID(1)]
串爆炸(布尔响亮的);
}
[的Guid(7BD20046-DF8C-44A6-8F6B-687FAA26FA71),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)
公共接口IPantsEvents
{
串爆炸(布尔响亮的);
}
[标记有ComVisible特性(真)]
[的Guid(0D53A3E8-E51A-49C7-944E-E72A2064F938),
ClassInterface(ClassInterfaceType.None)
ComSourceInterfaces(typeof运算(IPantsEvents))]
公共类裤子:IPants
{
公共裤(){}
[标记有ComVisible特性(真)]
[ComRegisterFunction()]
公共静态无效的DllRegisterServer(字符串键){}
[标记有ComVisible特性(真)]
[ComUnregisterFunction()]
公共静态无效的DllUnregisterServer (字符串键){}
[标记有ComVisible特性(真)]
公共字符串爆炸(布尔大声)
{
字符串结果;
如果(大声)
结果=砰;
,否则
结果=啪;
返回结果;
}
}
}
有可能是在打了几个不同的东西在这里。首先,你需要从提升的命令提示符(假设Windows Vista中,7或Windows Server 2008)使用带/代码库/ TLB开关regasm工具。是这样的:
regasm路径Smurf.dll/代码库/ TLB
一旦您使用regasm注册的DLL你的应的可以它用VBS,VBA或VB6调用。
我能够使用早期绑定和后期绑定从VBA调用爆炸
方法。然而,当我从VBScript试过我收到了像你一样的ActiveX不能创建对象错误。
我在Windows上运行7 64位,和我回顾,编译到32位的DLL时,并在64位操作系统上运行他们这可能会导致问题。一时兴起,我启动了一个命令提示符,然后输入:
C:\Windows\SysWow64\CScript.exe 路径为VBScript
结果是该脚本运行正确,并显示在屏幕上流行。
下面是我使用,以及在VBScript文件中的内容有所简化C#代码。
命名空间的Smurf
{
[的Guid(EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F)]
公共接口IPants
{
串爆炸(布尔响亮的);
}
[的Guid(7BD20046-DF8C-44A6-8F6B-687FAA26FA71),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)
公共接口IPantsEvents
{
串爆炸(布尔响亮的);
}
[标记有ComVisible特性(真)]
[的Guid(0D53A3E8-E51A-49C7-944E-E72A2064F938)]
[ClassInterface(ClassInterfaceType.None) ]
[ComSourceInterfaces(typeof运算(IPantsEvents))]
公共类裤子:IPants
{
[标记有ComVisible特性(真)]
公共字符串爆炸(布尔大声)
{
字符串结果;
如果(大声)
结果=砰;
,否则
结果=啪;
返回结果;
}
}
}
VBScript中:
昏暗X
将X =的CreateObject(Smurf.Pants)
MSGBOX(x.Explode(假))
集合X =什么
I thought I knew how to do this, but obviously not so I'd appreciate some help! I can't get my dll to register so I can instantiate it in a VBS, or elsewhere.
I wrote the following sample class, checked "Make assembly COM Visible", checked "Register for COM Interop", then built it. When I try to instantiate it from VBS I get the "Activex component can't create object" error.
This is the class code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Smurf
{
public class Pants
{
public string Explode(bool Loud)
{
string result;
if (Loud)
result = "BANG";
else
result = "pop";
return result;
}
}
}
...and this is the VBS:
Dim a
Set a = CreateObject("Smurf.Pants")
msgbox("ok")
What else do I need to do?
Thanks :)
[edit]
Forgot to mention, after the first failure I tried REGSVR32 and REGASM - no help!
[/edit]
Note that when I try REGSVR32, I get this message:
The Module "C:...\Smurf.dll" was loaded but the entry-point DllRegisterServer was not found. Make sure that "C:...\Smurf.dll" is a valid DLL or OCX file and then try again.
How helpful is that??
This is the latest version of the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Smurf
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
public interface IPants
{
[DispId(1)]
string Explode(bool Loud);
}
[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IPantsEvents
{
string Explode(bool Loud);
}
[ComVisible(true)]
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IPantsEvents))]
public class Pants : IPants
{
public Pants() { }
[ComVisible(true)]
[ComRegisterFunction()]
public static void DllRegisterServer(string key) { }
[ComVisible(true)]
[ComUnregisterFunction()]
public static void DllUnregisterServer(string key) { }
[ComVisible(true)]
public string Explode(bool Loud)
{
string result;
if (Loud)
result = "BANG";
else
result = "pop";
return result;
}
}
}
There could be a few different things at play here. First, you'll want to use the regasm tool with the /codebase /tlb switch from an elevated command prompt (assuming Windows Vista, 7 or Windows Server 2008). Something like:
regasm "Path to Smurf.dll" /codebase /tlb
Once you have registered the dll using regasm you should be able to invoke it using VBS, VBA or VB6.
I was able to use early binding and late binding from VBA to call the Explode
method. However, when I tried from VBScript I received the "ActiveX can't create object error as you did."
I'm running on Windows 7 64 bit, and I recalled that this can cause problems when compiling to 32 bit dlls and running them on 64 bit operating systems. On a whim, I fired up a command prompt and entered:
C:\Windows\SysWow64\CScript.exe "Path to VBScript"
The result was that the script ran correctly and displayed "Pop" on screen.
Here's the somewhat simplified C# code I used as well as the contents of the VBScript file.
namespace Smurf
{
[Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
public interface IPants
{
string Explode(bool Loud);
}
[Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IPantsEvents
{
string Explode(bool Loud);
}
[ComVisible(true)]
[Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938")]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IPantsEvents))]
public class Pants : IPants
{
[ComVisible(true)]
public string Explode(bool Loud)
{
string result;
if (Loud)
result = "BANG";
else
result = "pop";
return result;
}
}
}
VBScript:
Dim x
Set x = CreateObject("Smurf.Pants")
MsgBox (x.Explode(False))
Set x = Nothing
这篇关于揭露DLL为COM Interop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!