Console opens when running GUI C++ application - visual-c++

I'm currently making a C++ GUI application, but I have the following problem. In the program I have one MyForm.cpp and one Myform.h (just one button). When the application starts the console and the form opens. Is this default? Or how can I disable it? The code in the main is:
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
int main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Namespace::MyForm form;
Application::Run(%form);
}
Hope that someone can help?

Add this in your .pro file :
ENTRY = mainCRTStartup
OR
in VS, right click on your project -> properties -> linker -> system
And select "Windows (/SUBSYSTEM:WINDOWS)" for SubSystem.

If you are using a CRT build and there is no WinMain function you can use this:
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")//hide console window
to hide the console.

Related

Crash on static variable initialization with MSVC 2013

Hi folks!
Recently I've upgraded my developing environment. Namely, I've moved from Qt 4.8.4 and MSVC 2010 to Qt 5.3.1 and MSVC 2013. The problem I've faced is that my application crashes on launch, and stack trace proves that the crash happens during the initialization of some static class fields.
See the following example:
// header file
class MyClass : QObject
Q_OBJECT
public:
...
private:
static const QString CLASS_NAME;
// *.cpp file
const QString MyClass::CLASS_NAME = MyClass::tr("FOO"); // crash when calling tr()
const QString MyClass::CLASS_NAME = QObject::tr("FOO"); // but this works normally
During debugging into Qt I've found that the MyClass::tr() method eventually calls QMetaObject::tr() and it appears that all the fields of the QMetaObject instance are NULL. The crash then occurs when referencing some of them.
Notable fact is that the crash is not reproduced on another machine with Ubuntu 14.04 and Qt 5.2.1.
Surely, I could just replace the MyClass name to the QObject one, but my project consists of 63 libraries so I'm afraid of possible translation conficts.
Well,
class QObject :
static QString tr ( const char * sourceText, const char * disambiguation = 0, int n = -1 )
tr is a static function, that means you cannot refer to the virtual methode table. (see C++ static virtual members?)
The problem is: you can overload the method, but the call to the base object is not called. Not sure how the macro Q_OBJECT is interfearing. But I think it will connect it later.
Did you verfiy if the resulting QString was translated using QObject::tr()?
Not quiet sure if this works. Need to test it.
Edit:
Checked it, indeed it only affect Qt 5.x, but please refer to http://qt-project.org/doc/qt-5/sourcebreaks.html
I remember they changed stuff in the translate api in Qt 5. Probably there could be the mess in some hidden code breaks.

how to add main method to Visual C++/CLR/Class Library?

Last 10 years I was only using C#/Java so sorry about my simple questions about c++.
Now I've to add one c++ project to my solution. I do not need to be it standalone application, I need it to do some work and to transfer result to my another c# project. So I've created "Visual C++ / CLR / Class Library".
By default such project doesn't contain too much code. Just this:
// CliProject.h file
#pragma once
using namespace System;
namespace CliProject {
public ref class Class1
{
// TODO: Add your methods for this class here.
};
}
// CliProject.cpp file
#include "stdafx.h"
#include "CliProject.h"
Now for debugging I want to add "main" method so I can launch my library as standalone application. How to do that? Should I create one another class or I should use existent classes?
Create a 'CLR Console Application' project with a reference to your library. Or even better, for debugging, use a unit test framework.

How do Right-Click and Menu-Bar menus work?

I've created simple application that monitors X11's _NET_CLIENT_LIST. It prints me info when window ( including conky,tint2,... ) opens or closes. It works fine, except when i create menu (RMB-click or front Menu-Bar) it won't print anything - that means they aren't new windows, but they can be drawn out of window they are created from, so what is it?
I'd like create my own context menu in my app and i don't want to use any toolkit ( GTK, QT,... ). So i need to know how do they work.
Adding another answer because the old one is for a different question entirely :)
Pop-up menus (whether RMB-activated or from a menu-bar) are perfectly normal X11 windows. The reason that you don't see them in your monitoring program is that you are monitoring changes caused by the window manager. Pop-up menus normally bypass the WM entirely, so WM doesn't know about them.
This is achieved by setting the override_redirect window attribute XSetWindowAttributes structure. Set it for your pop-up menus (and only for pop-up menus) and you should be all set.
Menus are not managed by a WM and don't have any WM-specific properties.
To watch windows, catch XMapNotify and XUnmapNotify events on the root window, using SubstructureNotifyMask. Here's a very simple program that does something:
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
int main () {
Display* d = XOpenDisplay(0);
if (!d) {
printf ("Oops...!\n");
exit (EXIT_FAILURE);
}
XSelectInput(d, DefaultRootWindow(d), SubstructureNotifyMask);
for(;;) {
XEvent e;
XNextEvent(d, &e);
if (e.type == MapNotify) {
printf ("Window %lx mapped!\n", e.xmap.window);
}
if (e.type == UnmapNotify) {
printf ("Window %lx unmapped!\n", e.xunmap.window);
}
}
}
It reports spurious Unmap events which can be simply ignored.
A more complete program should probably watch all events selected by SubstructureNotifyMask and SubstructureRedirectMask.

Use a supplied dll in Visual C++ project

I just bought a device that comes with a dll file. I want to use Visual C++ to program the device. How do I load the .dll file into my project?
A DLL is a library file that contains compiled program logic, just like an EXE. You can't execute it alone, but like an EXE file you can't just 'load' it into your project either.
You will need to use functions like Load Library to load the library, and then GetProcAddress to find a function you want to call.
Edit:
After you clarified your question in the comments you are trying to write a windows program instead of a program you run on your device.
I wrote some sample code to show you how to start:
#include <windows.h> // This is a windows header file. The functions I mentioned above are declared here
#include "mpusbapi.h" // This is the header file supplied. It declares the function prototypes that are defined in the DLL
int main(int argc, char* argv)
{
// Try to load the library
HMODULE mpbusDLL = NULL;
mpbusDLL = LoadLibrary(L"mpusbapi.dll");
if (mpbusDLL != NULL) {
// If the library could be loaded, then load the functions using GetProcAddress()
// Load the function 'MPUSBOpen' from the DLL
MPUSBOpen = (HANDLE(*)(DWORD, PCHAR, PCHAR, DWORD, DWORD)) GetProcAddress(mpbusDLL, "_MPUSBOpen");
...
MPUSBOpen(...);
}
}
This C code will load your libary and then attempt to load the function MPUSBOpen, which is implemented in your DLL.
You will need to load the other functions defined in your header file the same way (at least if you want to use them).

VC++: Using DLLs as "subprograms"

So I just began to try my hand at emulation after years of putting it off and not knowing where to start and I have managed to successfully write my first emulator! Now I am organizing my code in so that I can reuse the code to emulate other systems. I've been toying with the idea of having a shared frontend "platform handler" of sorts that I will compile as my executable whereas I will compile my emulated system code into dlls that the platform handler will use to identify what is available and instantiate from. This would allow me to separate my code into different projects and to leave the option open of using a bulkier front-end with more features or a streamlined "game only" and to share the same dlls between them rather than make two different solutions.
I know how to compile dlls vs executables but I don't know how to link the executable to the custom dll in such a way that I can instantiate a class from it. I'm not even sure what I'm trying to do is technically possible. Do the dll classes need to be static? I've never coded anything like this before or even done much with custom dlls so any help or ideas would be appreciated. I'm using Visual C++ 2010 by the way. Thanks in advance for any advice anyone may have.
You don't really have to do much different. Just export your classes from the dll like you do for functions. In your app, include the header and link to the generated lib like you usually do. See this page: http://msdn.microsoft.com/en-us/library/81h27t8c%28v=vs.80%29.aspx
Example.h
#ifdef DLL_EXPORT
#define EXPORT_API __declspec(dllexport)
#else
#define EXPORT_API __declspec(dllimport)
#endif
class EXPORT_API Example
{
public:
Example();
~Example();
int SomeMethod();
};
int EXPORT_API ExampleFuncion();
Example.cpp
#include "Example.h"
Example::Example()
{
// construct stuff
}
Example::~Example()
{
// destruct stuff
}
int Example::SomeMethod()
{
// do stuff
return 0;
}
int EXPORT_API ExampleFunction()
{
return 0;
}
In your dll project, define DLL_EXPORT and build. You will get a .lib and .dll output. In your main project where you will be using the dll you do not have to do anything except include the header and link against the .lib. Do not define the DLL_EXPORT symbol in your main project and be sure the .dll is somewhere your application can find it.
If you really want to get clever, this problem is screaming for the factory design pattern. If you design your interface well enough, you can have your dlls register their implementation with your application when they are loaded. You can extend forever without even rebuilding your main executable.

Resources