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

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.

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 can I extract a .tlb from a .exe

I have a COM application that I want to use, but I only have the .exe from that app. Is there a way to extract the .tlb from that .exe?
I tried with tlbexp filename.exe from Visual Studio Command Prompt, but I get an error and I don't know why: TlbExp : error TX0000 : Exception of type 'System.OutOfMemoryException' was thrown.
I am using Visual Studio 2012 with C++.
Edit:
The .exe is in C++, later I saw that tlbexp is for .NET. This is why it didn't work, sorry for the confusion.
I still need to find a way to extract the .tlb from C++ .exe.
I have used this utility https://www.nirsoft.net/utils/resources_extract.html Just check the type libraries option.
It does now work on all cases but it works for many dlls and exe

Compiling and Running MFC Applications with Visual Studio 2008?

I was wondering if anyone can provide a detailed explanation on how I can compile and execute a C++ MFC application in Visual Studio 2008 given that I already have a .h and .cpp file only.
I couldn't find a decent and up to date explanation anywhere and when I try to create a plain MFC project in VS08 I get a bunch of xxx.h xxxDoc.h xxxView.h files, I try and replace those files with my own .cpp and .h files but i get a ton of errors when I run it.
So just to summarize, how do I compile and run my MFC project's .h and .cpp files in Visual Studio 2008 and create an .exe?
Thanks in advance.
See here: Creating an MFC Application
This tutorial may help you as well: Introduction to MFC Applications
It depends on what you are trying to do, but deleting the files the wizard creates is not a good idea. They are there because you told it to put them there (by selecting MFC-SDI/MDI), so they are necessary for the basic initialization and such.
If you don't need the Doc/View architecture, you can create a Dialog Based application. Or, you can even use an MDI or SDI app, and not use the Doc and the View, but if you want to delete them you'll have to make sure there are no references to them. What you cannot delete is the "MyProject.cpp" (where the CWinApp derived class lays), which does the initialization of the application.
Another option is to create a Console application with MFC support. To do so, you have to select "Win32 console application" in the wizard and then check "Add headers for MFC".

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.

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