visual studio weird behavior - c++

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?

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.

Hide console before console appear C++

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

Embarcadero Builder C++ XE5 data execution prevention compiler

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

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