Creating CMFCDesktopAlertWnd Control in Visual C++ - 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.

Related

OpenCASCADE 7.6.0 not compiling with a .NET 6.0 class library with Visual Studio 2022 (Windows 10)

Steps to reproduce:
Install a version of Visual Studio (I used VS Community 2022). Install OpenCASCADE 7.6.0.
Create a C++ .NET CLR project using Visual Studio 2022 targeting .net6.0.
Change settings to include OpenCASCADE header and library files.
Edit the main header by replacing the code within it with below:
#pragma once
//for OCC graphic
#include <OpenGl_GraphicDriver.hxx>
//wrapper of pure C++ classes to ref classes
#include <NCollection_Haft.h>
namespace ClrClsLibDotNetCoreMwe {
public ref class Class1
{
// TODO: Add your methods for this class here.
};
}
Attempt to build.
Issue: The build fails with the following complain:
1>C:\OpenCASCADE-7.6.0-vc14-64\opencascade-7.6.0\inc\NCollection_DefaultHasher.hxx(34,1): error C2872: 'HashCode': ambiguous symbol
1>C:\OpenCASCADE-7.6.0-vc14-64\opencascade-7.6.0\inc\NCollection_DefaultHasher.hxx(34,1): message : could be 'HashCode'
1>C:\OpenCASCADE-7.6.0-vc14-64\opencascade-7.6.0\inc\NCollection_DefaultHasher.hxx(34,1): message : or 'System::HashCode'
What fixes the problem:
Either Targeting .NET Framework instead of .NET Core (/clr instead of /clr:netcore).
Or removing one of the headers.
Please see if there is a way where I can keep both the headers and target .NET Core?
I have looked around for a possible solution before posting this question here. A promising solution was to disable implicit usings. However, that didn't pan out.
I had the same problem.
In my case, the "using namespace System;" included in the header file. The text caused the problem.
Thanks!

pplawait.h / experimental/resumable for co_await doesn't work

I have installed on windows 10.0 visual studio 2017 (version 15.2)
I have migrating from VS2013 to VS2017 for my project (that include cpprestsdk) and change .then() method with co_await. I have read something on the web but actually i can't compile my solution anymore.
Cannot open include file pplawait.h
ignoring unknown opion '/await'
Suggestions?
Using coroutines with Visual Studio 2017 and an MFC solution into which I am using C++/WinRT with its async type functions, I have the following.
See as well this question and my answer which contains several examples of using coroutines with concurrency, std:async and C++/WinRT: C++11 threads to update MFC application windows. SendMessage(), PostMessage() required?
First of all settings in the solution Properties.
Configuration Properties -> General -> Platform Toolset Visual Studio 2017 (v141)
C/C++ -> All Options -> Additional Options -> /await
C/C++ -> All Options -> C++ Language Standard ISO C++17 Standard (/stdc++17)
And the source code for the actual function that I am using (C++/WinRT async function with co_await):
#include <pplawait.h>
#pragma comment(lib, "windowsapp")
#include "winrt/Windows.Foundation.h"
#include "winrt/Windows.Web.Syndication.h"
// . . . other code
// sample function using co_await to retrieve an RSS feed which may take
// a while so we want to start it and then continue when the results are
// received. we are using one of the async functions of C++/WinRT to retrieve
// the RSS feed text. We require a pointer to a CMainFrame object which has
// the function we need to output the text lines to a displayed window.
winrt::Windows::Foundation::IAsyncAction myTaskMain(CMainFrame *p)
{
winrt::Windows::Foundation::Uri uri(L"http://kennykerr.ca/feed");
winrt::Windows::Web::Syndication::SyndicationClient client;
winrt::Windows::Web::Syndication::SyndicationFeed feed = co_await client.RetrieveFeedAsync(uri);
// send the text strings of the RSS feed list to an MFC output window for
// display to the user.
for (winrt::Windows::Web::Syndication::SyndicationItem item : feed.Items())
{
winrt::hstring title = item.Title().Text();
p->SendMessageToOutputWnd(WM_APP, COutputWnd::OutputBuild, (LPARAM)title.c_str()); // print a string to an output window in the output pane.
}
}

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.

Native dll built with /CLR flag containing a managed class

I have a old native MFC/c++ dll I have managed to get it compiled with /CLR flag.
Now I have added a managed C++/CLI class to the dll within a namaspace.
The header file is below, and the cpp file only has #include for the header file.
The native dll is a huge dll project with lot of un managed code, but it has only one managed c++ class like below.
When I add that dll as a reference to a .net winforms project I don't see that namespace / class, in the object browser,
and I get compile error for not finding the namespace "ShashiTest"
I am using Visual studio 2008.
Native dlls in mixed mode can not be added as reference to a managed project ?
Or am I missing something
Please help.
#pragma once
#using<mscorlib.dll>
#using<system.windows.forms.dll>
// Class derived from Forms
using namespace System::Windows::Forms;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Windows::Forms;
namespace ShashiTest {
/// <summary>
/// Summary for test
/// </summary>
public ref class test
{
public:
test(void)
{
};
void ShowMessage()
{
MessageBox::Show("Hello World");
}
};
}
When I simplified my problem..I created a fresh MFC dll and added a manged C++ class to it (same class as above) . Compiled with /CLR flag.
When I add this dll to winforms project and run it i get
System.BadImageFormatException. Any clue ?
However I see the class and the name space and winform project compiles fine unlike the problem I have the above.
System.BadImageFormatException is usually caused by having an AnyCPU .NET application reference a DLL containing 32-bit native code. You get an error when running on a 64-bit platform, because the AnyCPU application runs as 64-bit, and can't load the DLL. The fix for this is (easy) to mark the application as x86-only or (hard) to provide both 32-bit and 64-bit versions of all DLLs containing native code.
Of course, you could have some other problem. Checking your DLL with Red Gate Reflector as suggested by #cdleonard in the comments is a great next step. Or ILSpy, which is free.

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