I am trying to call InitCommonControlsEx() in the main entry of a window program, and although I included the header and linked to the ComCtl32.Lib, for some reason the compiler underlines that function as undefined. Following is the code...
#include <CommCtrl.h>
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
INITCOMMONCONTROLSEX initControls;
initControls.dwSize = sizeof(INITCOMMONCONTROLSEX);
initControls.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx(&initControls);
}
I know that the inclusion of the is doing something, because if I delete the inclusion of that header, then the compiler doesn't recognise the class INITCOMMONCONTROLSEX.
I am really confused. I went into the CommCtrl.h file and found that many sections were underlined in red giving warnings by the compiler, including any reference to WINAPI. Could it be that I have a broken header? I know the functions declaration is in there, it's in the following picture...
I've been googling for hours and can't figure it out. There's not a DLL file I also need, is there? As I said I've already linked the ComCtl32.Lib, sometimes referred to as comctl32.lib (I don't think the capitals matter). Furthermore, on my computer I have 4 instances of this ComCtl32.Lib, two are for 64bit and two are for x86. But, my feeling is that the problem is it just can't find declaration of that function from that header. Thank you.
Edit: Thanks to Harry Johnson I fixed the problem. I had no idea that including standard header files requires that you include other header files before them. In this case the compiler didn't recognise anything because windows.h wasn't included BEFORE CommCtrl.h. An obvious mistake for anyone who knows what they're doing. You can delete this question entirely. I expected that when including a header that windows provides that all relevant dependencies are included in that header.
If you see LNK2019 in Visual Studio 2022, add the #pragma line.
#include <CommCtrl.h>
#pragma comment( lib, "comctl32.lib" )
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
INITCOMMONCONTROLSEX initControls;
initControls.dwSize = sizeof(INITCOMMONCONTROLSEX);
initControls.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx(&initControls);
}
Related
I was following the guide on Microsoft about Windows programming https://learn.microsoft.com/en-us/windows/win32/learnwin32/learn-to-program-for-windows and after learning the concepts and the naming conventions I got to the coding part.
this the code I copied from the guide.
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);
but when I try to compile this it gives me an error about WINAPI and HINSTANCE undefined
I know that windows.h contains child files that I think can't be included individually but when I try to include windef.h after windows.h as it contains the typedef of WINAPI it gives alot of other errors but the WINAPI error is gone.
I am using VS2017 version 15.9.36 With Windows 10 SDK (10.0.17134.0)
Visual Studio installed packages
I am so new to windows programming and idk what am I doing wrong I hope I provided enough info.
How to run a C++ code, without console ?
ShowWindow(GetConsoleWindow(), SW_HIDE); hide the window, but after it apear.
Before run the program, can I put a line witch hide completly console ?
You can set this pragma inside the file where main method is located, on top of your header files includes:
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
This can also be done with linker options:
/SUBSYSTEM:windows
/ENTRY:mainCRTStartup
As alternative, in VS, change the project subsystem to Windows (/SUBSYSTEM:WINDOWS) in Project Properties-Linker-System-Subsystem. If you do that, use the WinMain signature instead of main signature:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
// Your code here.
return 0;
}
Hope this isn't an obvious issue. I've recently run in exceptions due to a lack of Data Execution Prevention (DEP) support in our 32-bit exe on a Windows 2008 R2 server. Adding the exe to the DEP exclusion list, solved the issue as a workaround.
I would like to compile with support for DEP, but can't find any indication on how to do this in Builder XE5 c++. Is this possible? I have found some vague suggestions for Delphi, but nothing definitive.
Any ideas?
AFAIK, C++Builder doesn't have the same DEP options that Delphi has. You will have to either
use an external PE editor to modify the PE flags of your compiled EXE file.
call SetProcessDEPPolicy() at runtime, such as at the top of your main()/Winmain() function:
void EnableDEP()
{
const DWORD PROCESS_DEP_ENABLE = 0x00000001;
typedef BOOL WINAPI (*LP_SPDEPP)(DWORD);
LP_SPDEPP SetProcessDEPPolicy = (LP_SPDEPP) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "SetProcessDEPPolicy");
if (SetProcessDEPPolicy != NULL)
SetProcessDEPPolicy(PROCESS_DEP_ENABLE);
}
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
EnableDEP();
...
}
First of all, sorry if the title is not very well chosen, but I don't know how to deal with this code showing something strange when compiled as C using Visual Studio 2013.
The code is:
#include <windows.h>
#include <stdafx.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Hello World!", "Note", MB_OK);
return 0;
}
And, the result is :
I have MS Visual Studio 2013 Original Genuine from MSDN.
Also, I'm not Chinese. The interface of IDE is in English.
I'm not entirely sure how you got that to compile but you're most likely in the wrong character set for that project.
Try:
MessageBox(NULL, L"Hello World!", L"Note", MB_OK);
If you're not intending to work in that character set you should change it as well.
Right-click on the project and go to Properties.
Configuration.
General.
Character set.
I am making a win32 application using visual studio 2008.
When ever i compile my code which generates a simple dialog, all of the dialogs text show in Chinese language. I have not set Chinese anywhere. can some one elaborate what the issue is?
My code is
#include <windows.h>
int WINAPI WinMain(
HINSTANCE nowInstance,
HINSTANCE prevInstance,
LPSTR ipCmdLine,
int nCmdShow
)
{
MessageBox(NULL,"My First Program","Our University",MB_OK);
return 0;
}
It sounds like you're mixing Unicode and ANSI.
Have you tried
MessageBox(NULL, _T("My First Program"), _T("Our University"), MB_OK);
And does that give you the expected results?