调用 COM 属性和方法

发布于 2024-10-03 20:36:07 字数 2152 浏览 0 评论 0原文

我正在尝试动态创建 COM 对象、调用 COM 方法并设置 COM 属性。 COM 类是一个 VB6 ActiveX DLL。该实现与此页面中的 VB6 代码完全相同 http://msdn.microsoft .com/en-us/library/ms973800.aspx

简而言之,项目是 PhysServer,类名为 Temperature,它有两个属性 CelsiusFahrenheit 和两个方法 GetCelsius()GetFahrenheit()

我已经运行 regsvr32 来将 ActiveX DLL 注册到计算机。 ProgID 是PhysServer.Temperature

我有三个代码块

代码块 1(有效)

Option Explicit Off
Option Strict Off
...
Dim objType = Type.GetTypeFromProgID("PhysServer.Temperature")
Dim comObj = Activator.CreateInstance(objType)
comObj.Celsius = 100
Dim f As Double = comObj.GetFahrenheit()
Console.WriteLine(f) ' shows 212

代码块 2(有效)

Option Explicit On
Option Strict On
...
Dim objType = Type.GetTypeFromProgID("PhysServer.Temperature")
Dim comObj = Activator.CreateInstance(objType)
Microsoft.VisualBasic.CallByName(comObj, "Celsius", CallType.Let, 100)
Dim f As Double = CDbl(Microsoft.VisualBasic.CallByName(comObj, "GetFahrenheit", CallType.Method, Nothing))
Console.WriteLine(f) ' shows 212

代码块 3(无效)

Option Explicit On
Option Strict On
...
Dim objType = Type.GetTypeFromProgID("PhysServer.Temperature")
Dim comObj = Activator.CreateInstance(objType)
Dim f As Double = CDbl(objType.InvokeMember("GetFahrenheit", Reflection.BindingFlags.InvokeMethod, Nothing, comObj, Nothing))
Console.WriteLine(f) ' shows the default value of GetFahrenheit '
objType.InvokeMember("Celsius", Reflection.BindingFlags.SetField Or Reflection.BindingFlags.InvokeMethod, Nothing, comObj, New Object() {100}) ' throws exception: Number of parameters specified does not match the expected number '
f = CDbl(objType.InvokeMember("GetFahrenheit", Reflection.BindingFlags.InvokeMethod, Nothing, comObj, Nothing))
Console.WriteLine(f)

我明白代码块 1 和代码块 2。但是,如何像代码块 3 一样使用反射设置 COM 对象?由于某些原因,我无法使用代码块 1 和代码块 2。所以唯一的方法是代码块 3...但它不起作用。

有谁知道代码块3的解决方案吗?谢谢!

I am trying to dynamically create COM object, call COM method and set COM properties. The COM class is a VB6 ActiveX DLL. The implementation is exactly equal to the VB6 code from this page http://msdn.microsoft.com/en-us/library/ms973800.aspx.

In short words, the project is PhysServer and the class name is Temperature which has two properties Celsius and Fahrenheit and two methods GetCelsius() and GetFahrenheit().

I have already run regsvr32 to register the ActiveX DLL to the computer. The ProgID is PhysServer.Temperature.

I have three block of codes

Code Block 1 (works)

Option Explicit Off
Option Strict Off
...
Dim objType = Type.GetTypeFromProgID("PhysServer.Temperature")
Dim comObj = Activator.CreateInstance(objType)
comObj.Celsius = 100
Dim f As Double = comObj.GetFahrenheit()
Console.WriteLine(f) ' shows 212

Code Block 2 (works)

Option Explicit On
Option Strict On
...
Dim objType = Type.GetTypeFromProgID("PhysServer.Temperature")
Dim comObj = Activator.CreateInstance(objType)
Microsoft.VisualBasic.CallByName(comObj, "Celsius", CallType.Let, 100)
Dim f As Double = CDbl(Microsoft.VisualBasic.CallByName(comObj, "GetFahrenheit", CallType.Method, Nothing))
Console.WriteLine(f) ' shows 212

Code Block 3 (doesn't work)

Option Explicit On
Option Strict On
...
Dim objType = Type.GetTypeFromProgID("PhysServer.Temperature")
Dim comObj = Activator.CreateInstance(objType)
Dim f As Double = CDbl(objType.InvokeMember("GetFahrenheit", Reflection.BindingFlags.InvokeMethod, Nothing, comObj, Nothing))
Console.WriteLine(f) ' shows the default value of GetFahrenheit '
objType.InvokeMember("Celsius", Reflection.BindingFlags.SetField Or Reflection.BindingFlags.InvokeMethod, Nothing, comObj, New Object() {100}) ' throws exception: Number of parameters specified does not match the expected number '
f = CDbl(objType.InvokeMember("GetFahrenheit", Reflection.BindingFlags.InvokeMethod, Nothing, comObj, Nothing))
Console.WriteLine(f)

I understand Code Block 1 and Code Block 2. However, how could I use set a COM object using reflection like Code Block 3? By some reasons, I cannot use Code Block 1 and Code Block 2. So the only way is Code Block 3... but it doesn't work.

Does anyone know the solution of Code Block 3? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2024-10-10 20:36:08

试试这个:

objType.InvokeMember("Celsius", Reflection.BindingFlags.SetProperty Or ...

而不是 SetField。

comObj 是一个运行时可调用的包装器,而摄氏温度将是其属性,而不是字段。

您也可能还需要指定 BindingFlags.Instance 标志。

Try this:

objType.InvokeMember("Celsius", Reflection.BindingFlags.SetProperty Or ...

instead of SetField.

comObj is a Runtime-Callable Wrapper, and Celsius will be a Property thereof, not a field.

Its also possible you may need to specify the BindingFlags.Instance flag as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文