Com*_*r7G 8 c# com com-interop visual-studio-2010
我以为我知道怎么做,但显然不是这样,我会感激一些帮助!我无法让我的dll注册,所以我可以在VBS或其他地方实例化它.
我编写了以下示例类,选中"Make assembly COM Visible",选中"Register for COM Interop",然后构建它.当我尝试从VBS实例化它时,我得到"Activex组件无法创建对象"错误.
这是类代码:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
......这是VBS:
Dim a
Set a = CreateObject("Smurf.Pants")
msgbox("ok")
Run Code Online (Sandbox Code Playgroud)
我还需要做什么?
谢谢 :)
[编辑]
忘了提,在第一次失败后我尝试了REGSVR32和REGASM - 没有帮助!
[/编辑]
请注意,当我尝试REGSVR32时,我收到以下消息:
模块"C:...\Smurf.dll"已加载,但未找到入口点DllRegisterServer.确保"C:...\Smurf.dll"是有效的DLL或OCX文件,然后重试.
这有多大帮助?
这是代码的最新版本:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Tim*_*ine 10
这里可能会有一些不同的东西.首先,您需要在提升的命令提示符下使用带有/ codebase/tlb开关的regasm工具(假设是Windows Vista,7或Windows Server 2008).就像是:
regasm "Path to Smurf.dll" /codebase /tlb
Run Code Online (Sandbox Code Playgroud)
使用regasm注册dll后,您应该能够使用VBS,VBA或VB6调用它.
我能够使用VBA的早期绑定和后期绑定来调用该Explode方法.但是,当我尝试使用VBScript时,我收到了"ActiveX无法像你那样创建对象错误".
我正在使用Windows 7 64位运行,我记得在编译为32位dll并在64位操作系统上运行时会出现问题.一时兴起,我启动了一个命令提示符并输入:
C:\Windows\SysWow64\CScript.exe "Path to VBScript"
Run Code Online (Sandbox Code Playgroud)
结果是脚本正确运行并在屏幕上显示"Pop".
这是我使用的简化的C#代码以及VBScript文件的内容.
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
VBScript中:
Dim x
Set x = CreateObject("Smurf.Pants")
MsgBox (x.Explode(False))
Set x = Nothing
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10380 次 |
| 最近记录: |