Embarcadero Builder C++ XE5 data execution prevention compiler - c++

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

Related

Win32 API Sample Code gives Compiling error "error C2065: 'HINSTANCE': undeclared identifier"

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.

InitCommonControlsEx() function not recognised by compiler

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

Visual Studio 2013 C++ Hello World Program Unexpected Debug Result

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.

How to create an cpp program that when executed it doesn't show any windows

I'm running this code, it's a simple code to lock Windows 8 screen on a Dell that has a button that can be assigned to a .exe, but it shows a cmd window before locking, how can I launch the .exe without showing the window?
Compiling using Visual Studio Dev Command Prompt
Command Line:
cl lockscreen.cpp
Code:
#include <string>
#include <Windows.h>
using namespace std;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int cmdShow)
{
system("rundll32 user32.dll,LockWorkStation");
return 0;
}
Link against user32.dll (user32.lib actually, the import library, but you should already be doing that if this is a stock win32 project) and just invoke LockWorkstation directly. If you must, LoadLibrary() + GetProcAddress() + etc.. There is no need for you to invoke a rundll call for this to work.
Basically I agree with WhozCraig, but if there is a good reason to make the call via an external exe, try calling it with start /b.
You don't need to compile any code at all. Just connect your special keyboard button to
rundll32.exe user32.dll,LockWorkStation

visual studio weird behavior

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?