동적으로 COM 개체를 만들고 COM 메서드를 호출하고 COM 속성을 설정하려고합니다. COM 클래스는 VB6 ActiveX DLL입니다. 구현은이 페이지의 VB6 코드와 정확히 같습니다. http://msdn.microsoft.com/en-us/library/ms973800.aspx. 짧은 말로COM 속성 및 메서드 호출
이 프로젝트는 PhysServer
이며, 클래스 이름은 두 가지 속성 Celsius
및 Fahrenheit
및 두 가지 방법 GetCelsius()
및 GetFahrenheit()
있다 Temperature
입니다.
나는 이미 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의 해결책을 알고 있습니까? 감사!
RCW가 아니며 후기 바인딩을 사용하고 있습니다. 주님은 이유를 알고 계십니다. –
감사합니다 ... 인터넷에서 해결책을 찾았습니다. 내 대답을 읽어주세요. –
@ 한스 패 탄트 : IDispatch를 사용해도 여전히 RCW입니다. –