获取C#ActiveX / COM库通过JScript工作 [英] Getting C# ActiveX/COM Library to Work through JScript
问题描述
我检查过stackoverflow(和什么似乎像其他地方)。我想得到一个COM解决方案工作,以便jscript文件可以写为
var T = new ActiveXObject(MySimulator 。世界);
T.MyMethod();
它将在命令提示符处执行
cscript mytest.js
获取错误自动服务器无法创建对象。
在C#中,我遵循了各种建议,最新的界面是:
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual),Guid(EAA4976A-45C3-4BC5-BC0B-E474F4C3C83B)]
public interface IComMyReaderInterface
{
void MyFunction();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None),Guid(0D53A3E8-E51A-49C7-944E-E72A2064F9DD),ProgId .World)]
[ComDefaultInterface(typeof(IComMyReaderInterface))]
public class MyReader:IComMyReaderInterface
{
public MyReader()
{
。 ..
}
public void MyFunction()
{
...
}
...
} $非常感谢,只要让我知道是否需要更多信息。 class =h2_lin>解决方案我假设如下。您的开发环境可能是64位操作系统,您的C#DLL项目可能配置为使用任何CPU
作为平台目标进行编译。
选择 x86
或 x64
并编译项目。如果您使用 x86
,请使用32位版本的RegAsm.exe注册程序集:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe / codebase assembly.dll
然后使用32位版本的cscript.exe运行JavaScript测试:
C:\Windows \SysWOW64 \cscript.exe mytest.js
如果你使用 x64
将是:
C:\Windows \Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe / codebase程序集.dll
C:\Windows \System32\cscript.exe mytest.js
以下代码已使用上述说明进行验证。
C#
使用系统;
using System.Runtime.InteropServices;
namespace ComLibrary
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual),
Guid(EAA4976A-45C3-4BC5- BC0B-E474F4C3C83B)]
public interface IComMyReaderInterface
{
void MyFunction();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None),
Guid(0D53A3E8-E51A-49C7-944E-E72A2064F9DD
ProgId(MySimulator.World)]
[ComDefaultInterface(typeof(IComMyReaderInterface))]
public class MyReader:IComMyReaderInterface
{
public MyReader()
{
}
public void MyFunction()
{
Console.WriteLine(MyFunction called);
}
}
}
(mytest.js):
var T = new ActiveXObject(MySimulator.World);
T.MyFunction();
输出:
MyFunction调用
I have checked on stackoverflow (and what seems like everywhere else). I would like to get a COM solution working so that a jscript file can be written as
var T = new ActiveXObject("MySimulator.World");
T.MyMethod();
It would be executed at the command prompt by
cscript mytest.js
In this case, I get the error "Automation server can't create object".
In C#, I have followed various suggestions, with the latest interface being:
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83B")]
public interface IComMyReaderInterface
{
void MyFunction();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None), Guid("0D53A3E8-E51A-49C7-944E-E72A2064F9DD"), ProgId("MySimulator.World")]
[ComDefaultInterface(typeof(IComMyReaderInterface))]
public class MyReader : IComMyReaderInterface
{
public MyReader()
{
...
}
public void MyFunction()
{
...
}
...
}
Thanks and just let me know if more information is needed.
解决方案 I'd assume the following. Your development environment is probably a 64-bit OS and your C# DLL project is probably configured to compile with Any CPU
as Platform Target. Read on if that's the case.
Choose either x86
or x64
and compile the project. If you go with x86
, then register your assembly with the 32-bit version of RegAsm.exe:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe /codebase assembly.dll
Then run your JavaScript test with the 32-bit version of cscript.exe:
C:\Windows\SysWOW64\cscript.exe mytest.js
If you go with x64
, that would be:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe /codebase assembly.dll
C:\Windows\System32\cscript.exe mytest.js
[EDITED] The following code has been verified to work using the above instructions.
C#:
using System;
using System.Runtime.InteropServices;
namespace ComLibrary
{
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual),
Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83B")]
public interface IComMyReaderInterface
{
void MyFunction();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None),
Guid("0D53A3E8-E51A-49C7-944E-E72A2064F9DD"),
ProgId("MySimulator.World")]
[ComDefaultInterface(typeof(IComMyReaderInterface))]
public class MyReader : IComMyReaderInterface
{
public MyReader()
{
}
public void MyFunction()
{
Console.WriteLine("MyFunction called");
}
}
}
JavaScript (mytest.js):
var T = new ActiveXObject("MySimulator.World");
T.MyFunction();
Output:
MyFunction called
这篇关于获取C#ActiveX / COM库通过JScript工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!