How to create ActiveX DLL in Visual C++ - 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

Related

How to add C++ dll project to Visual Basic references?

I am using Visual Studio 2015 Community Edition and I followed Microsoft's tutorial on creating implicit dll's here https://msdn.microsoft.com/en-us/library/ms235636.aspx and I referenced the dll successfully in a C++ console application (I did it just to see if it would work).
When I tried adding the dll project to the references for a Visual Basic Windows Form Application, I got an error saying "A reference to 'DLL Project Name' could not be added." After some research, I think it's because VB targets the .NET framework while the C++ dll targets Windows, but that's all I managed to figure out. I would greatly appreciate any help on reconciling this, or setting up some solution that involves a C++ dll and a GUI project that uses the dll (I just chose VB for the GUI since it's really quick and easy to set up).
Note that both the DLL project and the Visual Basic project are in the same solution.
Your tutorial won't help you invoke the code from .NET and you are correct in assuming it to be a .NET framework inter-op. issue. You can use p-invoke to import and call the DLL or create a COM wrapper around your DLL and register it in the GAC then reference it via a COM CreateObject call.
Other possibilities are to convert it to a C++/CLI .NET C++ DLL.
References:
P-Invoke
COM
-UPDATE-
I suppose I should also mention that if you target Universal Windows Platform (UWP), it also provides a clean binding across .NET and C++ and hides some of the busy COM wire-up.
A C++ DLL should be added to your *.NET application with a post-build event "xcopy" command like this one:
xcopy "$(SolutionDir)DLL\$(ConfigurationName)"\*.dll "$(TargetDir)"*.* /Y
from your selected project go to Project-->Properties-->Compile-->Build Events-->Post-build event command line
Enter the appropriate copy command.
In the above example, several C++ DLLs were collected in a "DLL" folder in the Solutions Directory. They all get copied to the application folder every time the application is built. There were DEBUG and RELEASE versions of those DLLs in separate sub-folders.
As other posters noted, you also must have correct p-invoke declarations for your C++ DLLs in your *.NET code.

How to get ATL support into an existing Windows application

I'm building an application using Qt 5.3.1 in Visual Studio 2012. I also want to use a hardware library which requires me to add a simple ATL object to the project. This can be done by using a Visual Studio wizard. The wizard complains that my project is neither an MFC executable nor an MFC DLL with full ATL support.
My question is: How can I add this support to my application? In the project properties I configured the project to link to the ATL and use the MFC. It did not work. Both statically and dynamically.
If there's another solution in order to add a simple ATL object to the project, please let me know.
The wizard which adds ATL support works on source code of the C++ project, including both checking if the current code is already ATL project, whether the project is okay for adding ATL support to, and code modification per se.
If the wizard "does not like" something in your project it displays an error which basically means that the wizard does know how to safely modify your source code. It does not however mean that adding ATL support is impossible. And enabling an option in project settings is insufficient since source code needs some explicit initialization stuff.
The best you can do to add ATL support without thinking too much about it, is to create a new empty project that matches the project type you currently have, e.g. MFC application. Then take a snapshot of source code, then add ATL support using the wizard. Then compare changes and duplicate them on your real project. The same applies to next step of adding ATL Simple Object using Visual Studio wizard.
Some relevant links (even though the method above looks the easiest to me):
How To Add ATL Support to an MFC EXE
Adding ATL support to existing mfc application
Add automation support to MFC DLL

Test case for utility classes in the DLL

I have a DLL project in visual studio. I have lot of nice classes inside it that helps the dll. Most of these classes are not exported and used internally by the dll to accomplish the task.
Here is the deal, with visual studio test case project, I was able to test only the exported functions in DLL and not the individual classes.
Can you suggest any way/workaround to unit test all the individual classes in a dll project ?
There is no solid way, so any nicer way is appreciable.
Thanks in advance.
Put them in a utility dll, make them exported and test that or leave private (to the dll) and test them via the exported functions that use them.
One approach could be to build the project into a static library (.lib) instead of a DLL, and link the test project to that library. Doing so would allow the test project to access all of the classes in the library.
Of course, if you use this approach you would need to create a new, separate DLL that linked to the static library to perform the DLL exporting that's currently in place. This would likely require you to create wrapper classes/functions in the DLL that simply forward their invocations to the corresponding classes/functions in the static library.
I am answering my own question...
I managed to add them as existing item in a separate visual studio project and was able to build the exe. And I used google test framework to execute my tests. This way, I didn't change any code and at the same time I was able to test all the utility classes as an independent entity.
Thanks all

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.

Instantiating an ActiveX Object

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.