How to locate (& remove) MFC dependent code in a project? - visual-studio-2010

I've got a huge legacy project which uses MFC and COM. The idea being to migrate it to Linux (winelib doesn't work for this), I need to identify the portions of the code that use MFC. Strange thing is that the solution contains two DLL projects which inherit from CWinApp and instantiate it. Besides, I don't see the purpose of CWinApp in these DLL's because the actual GUI code is in a separate non-dll project.
Two questions:
1. Is there any tool which can help me locate the MFC specific code so that I can remove it? Already saw the Qt option.
2. Why is CWinApp instantiated (as below) in a DLL project which isn't doing any GUI work at all? Is it used for message passing? I don't see any such syntax though. Removing the CWinApp instantiation results in a pointer of another project not being initialized. Weird!
One of the DLL project's code goes like this:
#include "stdafx.h"
#include "resource.h"
#include <initguid.h>
#include "dlldatax.h"
//removed some project-specific includes for posting on SO
#ifdef _MERGE_PROXYSTUB
extern "C" HINSTANCE hProxyDll;
#endif
CComModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_MyManager, CMyManager)
//removed some other similar lines as the line above
END_OBJECT_MAP()
// Component Category Helper Functions
static HRESULT CreateComponentCategory( CATID catid, WCHAR* catDescription );
static HRESULT UnRegisterCategory( CATID catid );
class CMyClassWrapperApp : public CWinApp
{
public:
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMyClassWrapperApp, CWinApp)
END_MESSAGE_MAP()
CMyClassWrapperApp theApp;
BOOL CMyClassWrapperApp::InitInstance()
{
#ifdef _MERGE_PROXYSTUB
hProxyDll = m_hInstance;
#endif
_Module.Init(ObjectMap, m_hInstance, &LIBID_MyClassWrapperLib);
return CWinApp::InitInstance();
}
int CMyClassWrapperApp::ExitInstance()
{
_Module.Term();
return CWinApp::ExitInstance();
}

In your MFC project settings, change to Use of MFC to Use
Standard Windows Libraries. Then remove all includes starting with
'afx' from your StdAfx.h. The compilation errors will guide you to
all MFC specific parts!
CWinApp is there to allow your MFC executable to correctly use any MFC code in your DLL. The framework will initialize your DLL through this object. For non-MFC DLLs you would just use DllMain.
Regarding COM and no MFC, I would start by googling this: atl com codeproject

Related

Why am I getting a warning to include pch.h even though it's already included?

I'm trying to reproduce an issue, so I have created an empty MFC C++ application using the VS2019 wizard and a separate native Unit Test project.
Before adding the Unit Test project, the MFC application compiled and launched successfully.
The MFC application still compiles successfully, but the Unit Test project will not compile. I'm getting two errors:
E0035 #error directive: "include 'pch.h' before including this file for PCH"
C1189 #error: "include 'pch.h' before including this file for PCH"
However, the only file in the Unit Test project (UnitTest1.cpp) already includes pch.h at the top of the file:
#include "pch.h"
#include "CppUnitTest.h"
#include "../MFCApplication1/MFCApplication1.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
CMFCApplication1App app;
bool result = app.InitInstance();
Assert::IsTrue(result);
}
};
}
It seems to be telling me to do something that is already done.
What's going on here?
Try to include stdafx.h and remove pch.h, it resolves the issue
I had the same issue. I found that I could not include the MFC DLL project's default header file directly. In your example this would be #include "../MFCApplication1/MFCApplication1.h"
I eventually found this example MFC DLL project code: https://github.com/Microsoft/VCSamples/tree/master/VC2010Samples/MFC/advanced from a list of examples here.
Notice that in the example that the default header/implementation files created by the MFC DLL project creation wizard (in this example, DLLScreenCap.h) don't export or provide any additional functionality. An existing MFC DLL project that I work with did the same.
So I added a class to the MFC DLL project to be tested and exported a simple function from it, and tested this exported function from my unit test project after linking to the project under test.
Exported class and function look like this:
#pragma once
class __declspec(dllexport) MFCLibraryExports
{
public:
// tests returning 17 to test unit test library against an mfc project
int SampleExport();
};
In your example I know that you are trying to test an instanced app and my answer doesn't help with that, but I was able to confirm that I can test at least a function exported from an MFC DLL project using the MS Unit Test framework. I'm not sure if you are expected to be able to get access to the application from a unit test as in your example; I am not able to include that header directly.

Build for Mypintool sample for x64 using visual studio 2012 fails, if "windows.h" is included

I am trying to build The Mypintool sample that comes with pin distribution for x64 architecture.
I am using pin3.0 (build 76991) and Visual Studio 2012. The build is successful if I have not included windows.h.
But if I include window.h (in a separate namespace) like this:-
namespace WD {
#include "Windows.h"
}
Then the build gives the error :-
C:\Program Files (x86)\Windows Kits\8.0\Include\um\winnt.h(3486): error C2888: '_CONTEXT::<unnamed-tag>' : symbol cannot be defined within namespace 'WD'
C:\Program Files (x86)\Windows Kits\8.0\Include\um\winnt.h(3488): error C2888: '_CONTEXT::<unnamed-tag>::<unnamed-tag>' : symbol cannot be defined within namespace 'WD'
Also, I am able to build the tool for win32 with windows.h included without any issue. Also, I have compared the build settings for win32 and x64 and I could not find any discrepancy.
Any help is appreciated.
It is unclear to me if you have a windows application that makes use of "Pin" or a "Pin" application that needs to call some Windows APIs - or a hybrid where massive use of both APIs exists in a single program.
Nonetheless, the Windows SDK is quite large, and is designed (mostly) to work with C, or with a subset of C++ compatible with C so it cannot be expected to work if wrapped in a namespace.
So, your only effective way to deal with a conflict in headers, is to avoid it by never including the "pin" or "windows" headers in the same cpp file. You need to partition the parts of your program that call windows, and that call "pin" into separate cpp files.
Create a bridging header file that defines classes and functions that use only C++ declarations. As it makes no use of either pin, or windows, this file can be #included by both sides of your project. Of course, depending on what your application is attempting to achieve this may be difficult so you might have to engage in some heavy duty type erasure.
Something like this:
// pin.cpp
#include <intel/pin.h>
#include "bridge.h"
void method(){
Window* wnd = Window::Create();
wnd.Show();
}
.
// bridge.h
class Window {
public:
static Window* Create();
virtual void Show()=0;
};
.
// WindowsImpl.cpp
#include <windows.h>
#include "bridge.h"
class WindowImpl : public Window {
HWND hwnd;
public:
bool Create(){
hwnd = CreateWindowEx(...);
return hwnd != NULL;
}
void Show() override {
ShowWindow(hwnd,...);
}
};
Window* Window::Create(){
WindowImpl* newWnd = new WindowImpl();
newWnd->Create();
return newWnd;
}

C++/CX Header file can't find Microsoft namespace

I have a header file with the following code:
Microsoft::WRL:ComPtr<ID3D11Device2> m_device;
inside a class definition. Visual Studio 2013 is saying that Microsoft is not a namespace, if I take the code and cut it out and put it in another class in another file unchanged it works just fine!
Any ideas?
Philip
EDIT: All of a sudden (without me having changed anything) Intelissense now accepts Microsoft::WRL::ComPtry as valid but when I compile it still gives me errors that it does not exists.
You need to
#include <wrl.h>
or
#include <wrl/client.h>
To get Microsoft::WRL::ComPtr in your module.
When you say "Visual Studio 2013 is saying that Microsoft is not a namespace" do you mean you get a compiler error or is just Intellisense? When dealing with headers, Intellisense can get a bit out of sync until you build again. For example:
//Test.h
class A { Microsoft::WRL::ComPtr<T> a; };
//Test.cpp
#include <wrl/client.h>
#include "Test.h"
If you just added the #include <wrl/client.h> to the Test.cpp, Intellisense might not know yet it is in scope for the header. It's perfectly valid C++ already, but a better practice is to include in your headers the ones it needs like:
//Test.h
#pragma once
#include <wrl/client.h>
class A { Microsoft::WRL::ComPtr<T> a; };
The other way this sync issue can manifest itself is if you are doing:
//Test.h
class A { Microsoft::WRL::ComPtr<T> a; };
//Test.cpp
#include "pch.h"
#include "Test.h"
//pch.h
#include <wrl/client.h>
Again, fully valid C++ that will build. Intellisense knows it works when you build, but might not until then.
Note: WRL is traditional C++ and is not using C++/CX language extensions. They both exist to make it easier to consume WinRT APIs from C++, and you will see the Microsoft::WRL::ComPtr used inside C++/CX applications when dealing with non-WinRT COM APIs like Direct3D. And you can mix C++/CX with WRL in the same application taking advantage of the fact that you can use reinterpret_cast<> between C++/CX ref ^ and ABI COM pointers. You can use Microsoft::WRL::ComPtr in old-school Windows desktop apps on Windows 7 or Windows Vista too.
With all that said, WRL and C++/CX are two distinct things.
Update: For consuming Windows Runtime APIs, you can also use C++/WinRT which is also 'standard' C++ without any need for the C++/CX extensions. See Microsoft Docs. You can use Microsoft::WRL::ComPtr for C++/WinRT applications, or you can use their variant wrl::com_ptr

LNK2005 errors despite matching runtime library (/MD)

I am attempting to use the OFELI library in a CLI/C++ solution containing both C# and C++ projects, under VS2010. In short, the UI is written in WPF, and computations are done in CLR-enabled C++.
I downloaded the OFELI source, converted the solution to VS2010 format, and was able to successfully build the static lib. However, upon linking to the lib (ofeli-debug.lib) a slew of LNK2005 errors result;
error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (?_Getgloballocale#locale#std##CAPAV_Locimp#12#XZ) already defined in msvcprtd.lib(MSVCP100D.dll)
I took care to insure that the lib was built with the /MDd setting, just as the project is. If I set the project to ignore msvcprtd.lib, I end up with a multitude of LNK2019 errors, as I'd expect.
Through experimentation, I've found that I can successfully link and use the ofeli lib from a non-CLR C++ test project, but not from a CLR enabled project (even if ofeli is compiled with CLR support enabled).
A simple code snippet which fails with many LNK2005 errors in my CLR project is below.
#include "stdafx.h"
#include <OFELI.h>
using namespace OFELI;
int _tmain(int argc, _TCHAR* argv[])
{
Mesh(10, 1); // Instance an object from the OFELI namespace
return 0;
}
What am I missing?

#using, #include, and 'assembly references' -- what are they and how do they relate?

I'm wondering how Visual Studio, other IDE's, and any other sort of circumstances (ie. no IDE at all) handle bringing in code from outside.
At first I thought #includes were the only way to do this, by either placing the assembly files in the designated directory for Visual Studio assembly files and then using the <> format to bring them in, or by putting the assembly files in the project directory and using the "" format to bring them in (that is, <> and "" respectively). But now I come up to the example at the end of this post with the #using directive (which, to note, is different than just the 'using' directive without the '#', for namespaces). Also I've come across adding assembly references in visual studio from within the 'configuration properties' dialogue.
So, would someone set me straight on all the in's and out's of adding assembly files and other code to a given project?
--The following is the example that has confused me-->
I have this section in my book that states:
"...The figure combines C++ 2008 code
with legacy C and native C++ code. It
also presents the two assembly
reference files you'll use most often
with C++ 2008, along with their
associated namespaces. Unlike when
you use Visual Studio to develop a
project, the assembly reference files
aren't included by default when you
code a single source file. Because of
that, you must code #using directives
for these files. ..."
#include <stdio.h>
#include <iostream>
#using <system.dll>
#using <system.windows.forms.dll>
// Associated namespace directives
using namespace std;
using namespace System;
using namespace System::Windows::Forms;
void main()
{
printf( "Hello, Earth\n"); // from stdio.h
cout << "Hello, Mars\n"; // from iostream
Console::WriteLine("Hello, Jupiter"); // from system.dll
MessageBox::Show ("Hello, Saturn"); // from system.windows.forms.dll
}
This is not native C++ (usually just referred to as C++), but C++/CLI, which is actually a .NET language designed to ease interacting between native and managed code, and as such can use both. It is, however, definitely not C++, despite an intentionally strong resemblance. Assemblies are .NET managed code repositories. You use the #using command to use them. #include is for native C++ headers. You should also be able to add managed references (that is, #using but done throughout for you) from the project's properties.
In native C++, then you must #include headers, and if appropriate, link to .lib files (or use GetProcAddress manually), and Visual Studio also offers #import for COM libraries. C++/CLI also offers #using for bringing in managed assemblies.
void main()
{
printf( "Hello, Earth\n"); // C native code
cout << "Hello, Mars\n"; // C++/CLI's wrapper on C++ Standard
Console::WriteLine("Hello, Jupiter"); // .NET managed code
MessageBox::Show ("Hello, Saturn"); // A thin wrapper on WinAPI
}
If you don't already know both C++ and .NET code, and/or you're not trying to link the two together, it's not recommended to use C++/CLI.

Resources