Answered by:
Get Methods from OCX

Question
-
Hi,
I have to invoke the method from the OCX. I am new to OCX and keep on learning about OCX.But i have knowledge about dll .I can listed the class,methods and parameters from dll using reflection . But in case of OCX how to get the list methods and how to invoke those methods inside OCX?
SaranRamWednesday, October 13, 2010 7:13 AM
Answers
-
You get access to the classes and methods exposed by an OCX by examining the corresponding type library. By convention, true OCXs will have the type library embedded as a resource. You can then use tlbimp.exe, from the .NET SDK, to generate a managed wrapper assembly for the OCX.
In order to use TLBIMP with an OCX, you need to know the resource ID for the embedded type library. You can determine the resource ID by opening the OCX file in Visual Studio(or Visual C++ - you need to have the native resource editor, which managed only products like Visual C# Express are lacking). Once you've opened the OCX, examing the resources and find the resource ID corresponding to the resource of type Type Library.
You can then generate the managed wrapper assembly (also known as an Interop Assembly) by using
tlbimp.exe MyComponent.ocx\1
...but replace 1 with the actual resource ID. You can now use all of the managed tools that you're familiar with to explore the classes an objects exposed by the managed assembly.
At runtime, you have a couple of options - either use the interop assembly for strong-typed, early-bound access to the OCX, or, if you're using VS2010/C#4, you can use the new 'dynamic' support to get late-bound access to the OCX with no runtime requirement for the interop assembly.
If your OCX does not have an embedded type library, and there is no separately supplied type library, you won't be able to use this technique.
-cd Mark the best replies as answers!- Proposed as answer by Mike Dos ZhangModerator Friday, October 15, 2010 3:54 AM
- Marked as answer by Mike Dos ZhangModerator Monday, October 18, 2010 12:31 PM
Wednesday, October 13, 2010 4:21 PM
All replies
-
An OCX is just the same as a dll, just the filename is modified.
If you like, you can rename the file back to .dll and use it as you're used to handle COM dll's.
Wednesday, October 13, 2010 10:31 AM -
You get access to the classes and methods exposed by an OCX by examining the corresponding type library. By convention, true OCXs will have the type library embedded as a resource. You can then use tlbimp.exe, from the .NET SDK, to generate a managed wrapper assembly for the OCX.
In order to use TLBIMP with an OCX, you need to know the resource ID for the embedded type library. You can determine the resource ID by opening the OCX file in Visual Studio(or Visual C++ - you need to have the native resource editor, which managed only products like Visual C# Express are lacking). Once you've opened the OCX, examing the resources and find the resource ID corresponding to the resource of type Type Library.
You can then generate the managed wrapper assembly (also known as an Interop Assembly) by using
tlbimp.exe MyComponent.ocx\1
...but replace 1 with the actual resource ID. You can now use all of the managed tools that you're familiar with to explore the classes an objects exposed by the managed assembly.
At runtime, you have a couple of options - either use the interop assembly for strong-typed, early-bound access to the OCX, or, if you're using VS2010/C#4, you can use the new 'dynamic' support to get late-bound access to the OCX with no runtime requirement for the interop assembly.
If your OCX does not have an embedded type library, and there is no separately supplied type library, you won't be able to use this technique.
-cd Mark the best replies as answers!- Proposed as answer by Mike Dos ZhangModerator Friday, October 15, 2010 3:54 AM
- Marked as answer by Mike Dos ZhangModerator Monday, October 18, 2010 12:31 PM
Wednesday, October 13, 2010 4:21 PM