Instantiating an ActiveX Object - c++

I have imported an ActiveX library into my project in Visual Studio 2008 using:
#import "TeeChart8.ocx" named_guids
Now I would like to create objects exposed by the ActiveX library. However, I am having trouble understanding the API.
There are two files that were created after I built the project with the #import, a .tli file and a .tlh file.
In the .tlh file there is the following line:
_COM_SMARTPTR_TYPEDEF(ITChart, __uuidof(ITChart));
I can see ITChart when I open the ActiveX library TeeChart8.ocx in the ITypeLib Viewer (Oleview). Also, if I type ITChartPtr->Invoke into my code, intellisense shows me that there are a whole bunch of parameters that need to be filled.
Essentially, I would like to know how to instantiate an ActiveX object and where I have to look to get the information I need?

Maybe not enough to create ActiveX function CoCreateInstance.
ActiveX must be correctly initialized (Theory can be found here ActiveX Controls Overviews and Tutorials :-)
The easiest way is to use CAxWindow (ATL Framework)
Here, collected various information on how to create ActiveX controls

When it's a simple COM object, you can use the following (assuming a coclass named TChart to go with the interface named ITChart):
ITChartPtr chart(__uuidof(TChart));
For more information on using the ITChartPtr type defined by the _COM_SMARTPTR_TYPEDEF macro in the .tlh file generated by the #import statement, see com_ptr_t.
If it's a full ActiveX control, there is more to it as Victor said in his answer.

Look up the function CoCreateInstance in your API docs.

Related

Using the #import directive on a microsoft shared dll

I do not have much experience working with COM and was wondering if anyone knew the minimum amount of information I have to provide to #import a dll into a cpp file (which will be used to make a dll). According to MDSN (http://msdn.microsoft.com/en-us/library/8etzzkb6(v=vs.80).aspx) I need to include a type library resource such as an .ocx file. Is this the case even for a Microsoft shared dll (e.g. mso.dll)? In addition, what are the other type library resources can I use?
From what I have gathered so far, I just need to provide enough information to the MIDL for it to form the interface to the dll in the right way (this seems to be the essence of COM).
The original link is dead. https://learn.microsoft.com/en-us/cpp/preprocessor/hash-import-directive-cpp?view=vs-2019 seems to be the new URL.
In the link that you listed a key sentence is "#import creates two header files that reconstruct the type library contents in C++ source code". When you #import MSO.DLL you are importing a type library to your project (not to your cpp file) and it creates the necessary COM interface definitions. So you don't need to provide additional information.
But there are probably other files you need to #import in order to use Office applications, depending on what you are trying to do.

How to use ActiveX dll in win32 C++ project visual studio 2010

Background: Vendor has provided me ActiveX Dll, no header files or .tlb file or anything such.
I am using Visual Stdio 2010 and using win32 C++.
The problem I am facing is how to load or reference dll?
I cannot reference the dll through solution explorer because it is unmanaged project.
When I try to use #import, it give me compile error saying XXX.tlb file not found. But I don't have .tlb type file.
The other option I tried was to open the dll with OLE viewer and copy the dll definitions and then paste in .idl extension file I created with Visual Studio. Then I executed midl on the idl file to create .h file but that did not help either. When I try use the classes, its gives me "abstract classes cannot be used or referenced" error.
There are other questions asked on the matter but not straight forward and the answers are not marked as answered or upvoted.
I want to know what are the different methods available to load the ActiveX dll in win32 C++ project in visual studio 2010 and which one should be preferred when? If there is a thread that actually addresses my issue please point me to that.
Thanks
If you are able to see the interface definitions using OLE View it means that the type library is embedded into the dll resources. So if you use #import "YourActiveX.dll" it should work.
You need
Register the COM (Active X) component in windows using regsvr32 : regsvr32 my_dll.dll
Then use COM to interact with the component. This is a good tutorial.

How to generate type library from unmanaged COM dll

I have to use third party, unmanaged COM dll into my .NET application. I need to modify this assembly to produce a custom RCW. In order to Edit Interop Assembly I need the type library of the particular assembly. Can any one explain me that how to How to generate type library from unmanaged COM dll?
There is no option in regsvr32 to generate type library.
Thank you,
Best Regards,
Robo.
You need the OLE-COM Object Viewer, available as part of whatever is the latest Windows SDK. Then you can go to File->View Type Lib and save the IDL to a text file. Then you can use MIDL (also part of the Windows SDK) to regenerate a TLB and header file. Something like this should do for basic cases:
midl /out c:\temp /header MyHeader.h MyIDLFile.idl
If all you're trying to do is create an Interop Assembly from a native dll (and the native DLL embeds the TLB as a resource), you can just call tlbimp directly on the dll:
tlbimp Foo.dll /out:Interop.Foo.dll
Which will generate Interop.Foo.dll. You can then use ildasm to modify the IL:
ildasm Interop.Foo.dll /out=Foo.il
If all you have is that COM DLL, you can't generate a type library. A type library describes the COM interfaces implemented. But an unmanaged COM DLL merely needs to expose DllGetClassObject. This only gets you an IClassFactory, which lets you create new objects if you knwo the correct type up front.
If the typelib is embedded in the DLL Resources and the TLB file itself is what is required then 3rd party software can extract it (though, as others have pointed out, this may not be the most desirable option).
E.g. using Resource Hacker:
Open the DLL file.
Navigate to the TYPELIB\1\1033 (or whatever) node in the tree view.
From the menu, choose Action -> Save Resource as binary file...
Chose a file name and give it the .TLB extension.
You can now reference that .TLB file and build without requiring the original DLL, e.g.
#import "test.tlb" named_guids
Visual Studio IDE can directly extract binary resources from non-managed .exe and .dll files.
If the type library is saved as a binary resource in a non-managed COM DLL (e.g. one built using VS native C++ compiler), you can extract it as follows:
Open the .dll file in the VS resource editor (the default editor when opening executables).
Navigate to the type library resource ("TYPELIB", then 1) within the resource tree.
Right-click on the type library resource and select export. That brings up a "Save File As" dialog box.
In the "Save File As" box, change the filename from the default (typically, bin1.bin) to something like MyLibrary.tlb and click Ok.
Confirm by opening the exported .tlb file with OleView.exe (results should look identical to those you see by opening the .dll with the OleView.exe).
To extract type libraries from managed DLLs (e.g., the ones built using C#), VS includes the tool Tlbexp.exe (run it from the VS command prompt):
https://msdn.microsoft.com/en-us/library/hfzzah2c(v=vs.110).aspx

How to create ActiveX DLL in Visual C++

Is there a tutorial/reference for creating an ActiveX DLL in Visual Studio 2008 C++ ?
I've got a DLL built with the DLLRegisterServer/UnregisterServer, and it's registered, but I'm having a little trouble figuring out what name to use to reference it (from a vbscript) and how to make sure my functions are exported correctly. Do I have to put my functions in a special class?
Thanks!
There are a lot of details to get right. Best thing to do is to use ATL and the built-in ATL object wizard. It auto-generates a bunch of files so that the IDL, type library, registration script, class wrapper and event proxies are all done correctly.
The component's ProgID is normally defined in the project's .rgs file.
+1 to nobugz
how to make sure my functions are exported correctly
Visual Studio contains OleView.exe. You can open your dll from it and see the list of props and methods.
As for tutorial, check here: ATL Concepts

How can I statically link my MFC extension DLL?

I have an MFC extension DLL that I've written. It contains derived classes of MFC controls (e.g. replacements for CButton etc) and also contains bitmap resources.
I wanted to create a static library - mainly because I do not want the code seperated into a DLL. However, I found that even though it is possible to add resources to a static library, I couldn't get the Class Wizard to work. As a result I created a DLL instead and now I want to find a way of statically linking my DLL into my application.
Can anybody help please? I am still using MSVC++ 6.0.
To clarify - what I would really like to know is how to create static .LIB file that contains resources, and classes via the Class Wizard.
Update: I got around this by creating a seperate (common) .rc file that I include in my main applications. Works a treat.