How to get ATL support into an existing Windows application - c++

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

Related

Adding ATL Simple Object to MFC project

I created a new MFC project and tried to add ATL Simple Object. VS give an error "the project need to add ATL support"
So I tried:
1 - add ATL Support, as it is, and got "framework already exists", because there is one framework.h in the MFC project;
2 - Add ATL Support with the name framework2. I copied the content of framework2.h to framework.h;
3 - I create a new MFC project, changed the name of framework.h to framework2.h and added ATL Support has framework. Then copied the content of original framework to the new.
No meter what, keep receiving "the project need to add ATL support" message when I try to add ATL Simple Object. Do I need to do something else to add ATL support?
I already create a COM port using an ATL project, but I need to make it using a MFC project to give support to an old MFC program.
I'm using Visual Studio 2019 Community. Already checked the installed packages and didn't find any missing.
installed packages

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.

What type of Visual Studio project should I use?

I am going to be making a project with OpenCV and (probably) Qt for GUI and I was just wondering what kind of project I should create in visual studio? Would it be the Win32 Project or Win32 Console Application or just an Empty Project? A lot of them are quite obvious as I am not making a Dll or makefile but do not know the difference between the others. Thanks in advance.
Which type of project you decide to pick in Visual Studio depends what will best fits your needs. Since I do not know your exact needs, the best answer I can give is explain the difference between the types of Project Options and give examples when you would choose to use the given project:
Win32 Project: A Win32 Project is one of the options Visual Studio provides to you, and if chosen will provide the user with template code that generates a "window", that window being just like any other window (browser window, folder window, etc), but one that your program controls.
Examples: Making a calculator, making an application that needs a window with buttons for the user to communicate with the program, etc.
Win32 Console Application: In this option, Visual Studio provides a Command Prompt interface where the user can input data and also where output can appear. Basically it's the black window where you can input commands and receive information of what is happening in your program.
Examples: When testing out code (it makes debugging easier in some cases), when the program really doesn't need to be too elaborate, etc.
There are other options available, thou these two were mentioned in your post and are probably the most popular among developers.
After installing Qt and maybe a Qt-VisualStudio-Plugin you have some new options when creating a New Project:
You should choose Qt Application in most cases. Not sure if you can use a GUI with Qt Console Application, but you'll get a terminal/console in that case.
After choosing project name, you can easily choose the Qt Modules you'll need. This adds them to your project settings, so you don't have to add the manually (but I guess you can do so later if you need more modules).
The project will create a .ui file which you can open/edit with QtDesigner. The project will perform all the moc and uic compile steps automatically and you don't have to add those things manually.
I think this is the easiest way to use the combination of Qt and Visual Studio.
Adding OpenCV to Visual Studio is easy:
Just add the include directories and the correct OpenCV libraries.
The question whether to use a consolse application or not depends on your needs.
Personally, I like printing development output to a console, but maybe you don't want that in your final project ;)

Visual Studio: can't add ATL class to ATL project?

So, I'm creating an ATL project in visual studio 2008 using the wizard (recommended here - How to create ActiveX DLL in Visual C++ - and elsewhere)
But I can't add any classes to my project. VS returns the following error:
atl classes can only be added to mfc exe and mfc regular dll projects or projects with full atl support
...the latter of which you'd think an atl project would have, right?
Can anyone offer a fix?
(I've tried both ticking and unticking 'Support COM+ 1.0' when creating the project - I'm not quite sure what to do with this option).
Add "new item..." instead of Add "class...". Then you can select one of the ATL options.
Interesting that your link: "How to create ActiveX DLL in Visual C++" doesn't have any information about creating ATL project :)
Anyway: File - New - Project - ATL - ATL Project - Next - Next - Finish. ATL project is ready.
Project - Add Class - ATL - ATL Simple Object - Add - Fill class name - Finish. ATL class is added.
I discovered this thread on msdn and tried all suggested solutions. The only one that worked was identifying 'isAtlProject()' in the javascript and setting it to return true. That got me to the next dialog.
However, having created the class, the 'Add Method' wizard would then silently fail to add all of the required code to the interface, header and cpp files. I made another thread about this. Turns out the reason for the latter bug was that intellisense was manually disabled (see other thread for details) :- to anyone reading this in future with the same problem, if you fix intellisense first maybe you won't have to bodge the javascript? Give it a go at least!
If you did not create project as "ATL Project".
Make sure atlbase.h is included in stdafx.h then
Add .cpp file (any name) to your project with the following code:
// ATLMod.cpp
/* This is all that is needed to satisfy VS full ATL support requirement
when adding ATL items with the wizards.
Just has to be present in the project. Compilation not required.
Can set "Exclude from build" property to Yes.
*/
#include "stdafx.h"
// ATLMod.h : Declaration of copy of DllMain.h
class CATLProject1Module : public ATL::CAtlDllModuleT< CATLProject1Module >
{
public:
} _AtlModule;

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".