Win32 Application, Call custom class method before calling Windows stuff - c++

Context:
I am trying to create a class that is the first to be called in a WIN 32 application... That class has a Setup method... I will then invoke the Windows class taken from here:
https://msdn.microsoft.com/en-us/library/bb384843.aspx
header
class OSWindows : IOS
{
public:
void Setup() override;
};
cpp
#include "OSWindows.h"
void OSWindows::Setup()
{
// Calls the windows stuff...
}
Question
How do i stop the Win32 application from automatically running their WinMain method...
Note:
I'm trying to make a portable application... and thus don't want the app to just simply have the app call the windows stuff first.

In Visual Studio you can change the entry point at
Project Properties -> Configuration Properties -> Linker -> System -> SubSystem
If you want it to be int main change it to Console (/SUBSYSTEM:CONSOLE).
The default for Win32 Applications is Windows (/SUBSYSTEM:WINDOWS) which is WinMain.

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);
}
}

Can I add C++ DLL in a ASP MVC application

I am trying to add a reference to a DLL built using C++ in ASP.Net MVC 5 application.
I want to call functions in DLL from the app.
Yes you can. The key here is "platform invoke".
You need to create a class holding the DLL methods (Win32 in the sample)
You need to define the prototypes/signatures of the DLL methods, using the DllImport annotation
Now you can call those methods
This sample is taken from the Microsoft documentation:
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr MessageBox(int hWnd, String text,
String caption, uint type);
}
public class HelloWorld {
public static void Main() {
Win32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0);
}
}
Yes, any native or unmanaged DLL can be used with ASP MVC applications. The builds should be targeting the same Platform. If a C++ DLL has been built for x64 platform, then the application(web/console) importing it should also be built for the same configuration.
See the post from #bdongus for importing using DllImport. Also the servers should run on the same platform.

wxwidgets app in Visual Studio gives error "LNK2019 unresolved external symbol"

Im creating my first program with C++ and wxwidgets.
When I try to compile the project I get errors.
LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
LNK1120 1 unresolved externals
I have compiled the wxwidgets my self in Visual Studio.
After compiling I created a new C++ empty project in Visual Studio.
I went to configuration and added includes directories:
Configuration properties -> C/C++ -> General -> Additional include directories:
C:\Users\user\source\repos\wxWidgets\include; C:\Users\user\source\repos\wxWidgets\include\msvc
Configuration properties -> Linker -> Additional Library Directories:
C:\Users\user\source\repos\wxWidgets\lib\vc_lib
Then I added 2 classes, cApp and cMain.
cApp.h
#pragma once
#include "wx/wx.h"
#include "cMain.h"
class cApp : public wxApp
{
public:
cApp();
~cApp();
private:
cMain* m_frame1 = nullptr;
public:
virtual bool OnInit();
};
cApp.cpp
#include "cApp.h"
wxIMPLEMENT_APP(cApp);
cApp::cApp() {
}
cApp::~cApp() {
}
bool cApp::OnInit() {
m_frame1 = new cMain();
m_frame1->Show();
return true;
}
cMain.h
#pragma once
#include "wx/wx.h"
class cMain : public wxFrame
{
public:
cMain();
~cMain();
};
cMain.cpp
#include "cMain.h"
cMain::cMain() : wxFrame(nullptr, wxID_ANY, "MyProgram") {
}
cMain::~cMain() {
}
You have 2 problems (after the edit due to the comment below) the following problem:
You're building your application as a console mode application and not a GUI one. While it is possible to use wxWidgets from console applications too, this is probably not what you're trying to do, so ensure that the "Linker|System|SubSystem" option in the properties dialog of your project is set to "Windows".
You don't have wxIMPLEMENT_APP(cApp); macro in your code. Again, it is perfectly possible to avoid it, but this is probably not your goal here, so just add this line. This macro is what defines main or WinMain for your application, depending on the platform.
Your linker wants to find the main entry point (main is the default entry point). The wxIMPLEMENT_APP(cApp) macro on the Windows architecture has the WinMain entry point.
I have three quick solutions.
You can write a macro #define WinMain main before macro wxIMPLEMENT_APP(cApp)
You can tell the linker the name of the entry point(
In your case, the name of the entry point is WinMain).
project properties -> Configuration Properties -> Linker -> Advanced and on the right pane of the dialog is a place for the name of the entry point function.
Your Visual Studio project has a Console SubSystem installed (this SubSystem looks for the default main entry point). You must change the SubSystem value to WINDOWS (this SubSystem looks for the WinMain entry point).
Project properties -> Configuration Properties -> Linker -> System and then set the SubSystem property to Windows (/SUBSYSTEM:WINDOWS)

Using MFC and ATL libraries without Visual Studio

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

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.