调用 COM 属性和方法
我正在尝试动态创建 COM 对象、调用 COM 方法并设置 COM 属性。 COM 类是一个 VB6 ActiveX DLL。该实现与此页面中的 VB6 代码完全相同 http://msdn.microsoft .com/en-us/library/ms973800.aspx。
简而言之,项目是 PhysServer
,类名为 Temperature
,它有两个属性 Celsius
和 Fahrenheit
和两个方法 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
而不是 SetField。
comObj 是一个运行时可调用的包装器,而摄氏温度将是其属性,而不是字段。
您也可能还需要指定
BindingFlags.Instance
标志。Try this:
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.