Using MFC and ATL libraries without Visual Studio - c++

My question is if I can use MFC and ATL libraries that come with PSDK Windows Server 2003 R2, without Visual Studio. I mean only at the command prompt with BCC32.exe or CL.exe?
I have found MFC and ATL libraries in the PSDK Windows Server 2003 R2 but I does not know if these libraries can be used without Visual Stuio IDE! What I need to do before using MFC and ATL at the command prompt?
Thanks!

If you have all the headers, and either the source code or compiled version of the rest, then yes it can be used without Visual Studio. Visual Studio is just an IDE that invokes the compiler etc. for you. With command line tools you do that job yourself.
Here's a minimal MFC program that you can try out:
#define WINVER 0x0500 // Windows 2000 and up.
#include <afxwin.h> // MFC core and standard components
typedef CFrameWnd MainWindow;
class App
: public CWinApp
{
private:
bool createTheMainWindow()
{
static char const title[] = "A general top level MFC window";
MainWindow* const pWnd =
new MainWindow;
if( !pWnd ) { return false; } // Pre-standard 'new' in MFC...
m_pMainWnd = pWnd;
pWnd->Create( NULL, title );
return true;
}
public:
virtual BOOL InitInstance()
{
CWinApp::InitInstance();
if( !createTheMainWindow() ) { return false; }
m_pMainWnd->ShowWindow( SW_SHOW );
m_pMainWnd->UpdateWindow();
return true;
}
};
App theApp;
Cheers & hth.,

No you can't, since some functionality relies on the Visual Studio compiler specific behavior. For example the way trampolines are used to map window handles to objects.
I'm assuming that you by "can be used without Visual Stuio IDE" you actually mean "with another compiler", since you mention the Borland compiler. Of course you can write C++ in a text editor and compile those files from the command line, not using the IDE, but what you want is using another compiler, right? (the IDE and the compiler are related but not the same).

Related

Visual studio 2013 DLL Project: Dialog form not displayed while running the DLL

I have a Visual Studio 2013 C++ DLL project.
The build DLL works fine, except one point: There is a lack of interactive form which should open when DLL starts & it should be responsive to/from the DLL
I had referred to this link
I tried:
Right click on Project-> Select Add->Resource->Dialog
After that, a blank Dialog box appeared in the Visual Studio tab and it was listed in the Resources folder
I thought simply compiling the project would make the empty Dialog box display while running the DLL.
But even the empty Dialog box is not displayed when I run the DLL
Am I missing something here?
Also, would C++ be sufficient to add functionality to the form/Dialog? Or, any other language like C#?
(So that I may add event handling kind of functionality to that)
I suggest that you could use MFC to add functionality to the form/Dialog.
Here are the steps:
Create a MFC DLL named 'CTestDll', and select Regular DLL using shared MFC DLL
Then select Add->Resource->Dialog
Add code in CTestDll.cpp
#include "CTestDlg.h"
extern "C" __declspec(dllexport) void Show()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CTestDlg test;
test.DoModal();
}
Create a MFC App for testing. You could call it through the button click event.
void CMFCApplication3Dlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
typedef void (WINAPI *TESTDLL)();
HINSTANCE hInstance = LoadLibrary(_T("CTestDll.dll")); //
if (hInstance != NULL)
{
TESTDLL TestShow = (TESTDLL)GetProcAddress(hInstance, "Show");
if (TestShow != NULL)
{
TestShow();
}
FreeLibrary(hInstance);
}
}

How to work on one SFML-C++ Project with different Visual Studio versions?

I'm working on a C++ project that requires SFML (free portable library) and some dependencies which I can copy onto a usb-stick/drive.
The issue is that I'm working with Visual Studio 2019 at home and very often at uni/work with Visual Studio 2013.
Whenever I try to run the project from one version on the other, exceptions and other errors are thrown around.
The only fix is creating a new project and annoyingly add each dependency & lib folder which I would absolutely 100% positively avoid at all cost...
How can I, aside from making two sepearte projects for each VS version, work on one project with different versions of Visual Studio?
I've tried using a portable codeblocks 17.02 copy but the IDE is not very pleasant to work with, especially since I own a Visual Studio Key and other students will review the code in Visual Studio as well.
This is just some simple window creation example:
#include <SFML\Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow Window;
Window.create(VideoMode(800, 800), "VS2013-VS2019Example");
while (Window.isOpen()) {
Event e;
while (Window.pollEvent(e)) {
if (e.type == Event::Closed) {
Window.close();
}
}
Window.clear();
Window.display();
}
}
I expected the program to create a new window, 800x800 with a title and no interception.
Starting the program will result in VS indicating that line 8, the line where window.create() is called, throws a Access violation reading location 0x000FFF exception at 0x0F15A5E3 (msvcp120.dll) in [project name].exe.

MetaTrader4 close (crash?) on deinit() + dll threading on WinXP Mode Virtual PC

I have a native library built with Visual Studio 2012. Required project configuration properties:
General / Platform Toolset = Visual Studio 2012 - Windows XP (v110_xp)
C/C++ / Code Generation / Runtime Library = Multi-threaded (/MT)
ExpertSample.cpp:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mutex>
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved) {
return(TRUE);
}
#define MT4_EXPFUNC __declspec(dllexport)
static std::mutex mutex;
MT4_EXPFUNC void __stdcall libInit() {
std::lock_guard<std::mutex> guard(mutex);
OutputDebugStringA("libInit");
}
MT4_EXPFUNC void __stdcall libDeInit() {
std::lock_guard<std::mutex> guard(mutex);
OutputDebugStringA("libDeInit");
}
ExpertSampleTest.mq4:
#import "ExpertSample.dll"
void libInit();
void libDeInit();
#import
int init() {
libInit();
return(0);
}
int deinit() {
libDeInit();
return(0);
}
int start() {
// libDeInit();
return(0);
}
Testing it on Windows 7 (x64) with MetaTrader build 451 (also for build 438) works normally.
Testing it in the WinXP Mode Virtual PC (running inside the same Windows 7), when the EA is removed from the chart, the whole terminal is closed. There is no crash report dialog, or anything in the log files.
If I call libDeInit() from start() it works fine.
If I remove the lock guard from libDeInit(), it works fine.
The above code is just a subset of a larger project. In that project, when the library was built with all kind of debug information and a lot of outputs, the problem seemed to happen less often.
Does anybody know how to fix it, or whether it's a MetaTrader 4 or WinXP Mode Virtual PC issue?
The C++ code itself looks good. Possible issues I see:
If the DLL is loaded but the constructor for the mutex is not called, that would explain your problem. That would be a problem in the environment loading the DLL.
Check the calling convention. Is it stdcall or cdecl? Using the wrong one can cause all kinds of issues, maybe even no issues under some circumstances.
Try replacing the mutex with a function returning a reference to a mutex, which is placed as function-static instance inside that function. If that helps, I'd say that it proves that the constructor is not called.
You don't need DllMain(), I'd only use it to call DisableThreadLibraryCalls().

How can I get MFC support apart from creating MFC application using App Wizard?

I know that when we use App Wizard for creating an MFC application in VC++, the wizard automatically adds the required libs to the project.
I want to create an MFC application manually. How to do that?
When you create a new MFC application, you will find this code in the precompiled header:
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
which is how the MFC header files get included. Even if you try to create a new Win32 Console Application and ask the wizard to include MFC support, you will again find these lines in the precompiled header:
#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
So if you want to create a console application that just somehow uses the MFC classes, then just create a new empty project, go to its properties and in General change Use of MFC from Use Standard Windows Libraries to Use MFC in a Static Library. Then you just need to include these headers and you are ready to go. ;)
Example:
#include <iostream>
#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
int main()
{
CByteArray a;
a.Add(7);
std::cout << (int)a[0];
}
You can create an MFC app manually, there are a lot of dependencies and fussing around. But it can be fun to do. Here is a small tutorial.
Create the following HelloMFC file:
#include <afxwin.h>
class HelloApplication : public CWinApp
{
public:
virtual BOOL InitInstance();
};
HelloApplication HelloApp;
class HelloWindow : public CFrameWnd
{
CButton* m_pHelloButton;
public:
HelloWindow();
};
BOOL HelloApplication::InitInstance()
{
m_pMainWnd = new HelloWindow();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
HelloWindow::HelloWindow()
{
Create(NULL,
"Hello World!",
WS_OVERLAPPEDWINDOW|WS_HSCROLL,
CRect(0,0,140,80));
m_pHelloButton = new CButton();
m_pHelloButton->Create("Hello World!",WS_CHILD|WS_VISIBLE,CRect(20,20,120,40),this,1);
}
To Compile this at the command line, there's are all the libraries that need to be linked. You will notice that there is no WinMain or main in the code above. MFC burried its main in the libraries. It is defined in appmodul.cpp which you can find in your MFC\SRC directory.
Anyway, here is how you can compile the above code:
cl.exe hellomfc.cpp /EHsc /I atlmfc\include /I Includes /I
Includes\Winsdk atlmfc\lib\amd64\nafxcw.lib Libs\libcmt.lib Libs\Ke
rnel32.Lib Libs\User32.Lib Libs\Gdi32.Lib Libs\MSImg32.Lib
Libs\ComDlg32.Lib Lib s\WinSpool.Lib Libs\AdvAPI32.Lib
Libs\Shell32.Lib Libs\ComCtl32.Lib Libs\ShLwApi .Lib Libs\Uuid.lib
atlmfc\lib\amd64\atls.lib Libs\Ole32.Lib Libs\OleAut32.Lib Li
bs\oldnames.lib Libs\WS2_32.Lib Libs\MsWSock.Lib Libs\OleAcc.Lib
Libs\comsuppw.l ib Libs\GdiPlus.lib Libs\Imm32.Lib Libs\WinMM.Lib
Libs\MsXml2.Lib Libs\OleDlg.L ib Libs\Urlmon.Lib
/link/SUBSYSTEM:WINDOWS
Note: the above obviously depends on the specific location of your library files, but those are the required libraries.

Creating CMFCDesktopAlertWnd Control in Visual C++

MSDN contains an example for creating a desktop alert window:
http://msdn.microsoft.com/en-us/library/bb983515.aspx
The sample code starts with the following declaration.
CMFCDesktopAlertWnd* pPopup = new CMFCDesktopAlertWnd;
When I use it in my code, the compiler complains
'CMFCDesktopAlertWnd' : no appropriate default constructor available
This is the complete source code of my application.
(I created an empty Win32 project in Visual Studio and set
the Use MFC in a Shared DLL option on the Property | General page.)
#include <afxwin.h>
#include <afxDesktopAlertDialog.h>
class Notifier : public CWinApp
{
public:
virtual BOOL InitInstance();
};
BOOL Notifier::InitInstance()
{
CMFCDesktopAlertWnd* pPopup = new CMFCDesktopAlertWnd;
return TRUE;
}
Notifier myApp;
What am I doing wrong?
The effect is the same in VS Express 2008 and the full version of VS 2010.
Are you using VS 2008 SP1? (The service pack is important)
Are you including the proper headers in stdafx.h? More specifically, you need
#include < afxext.h >
#include < afxcontrolbars.h >
to be able to use MFC Next (feature pack) classes.
It won't work on VS Express anyway because that doesn't include MFC.