Obtaining the windows desktop path - c++

This is my code:
#include <Windows.h>
#include <ShlObj.h>
#include <iostream>
using namespace std;
int main()
{
LPTSTR myPath = NULL;
SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);
if(myPath != NULL)
cout << "It returns something" << endl;
else
cout << "It returns nothing" << endl;
system("PAUSE");
return 0;
}
But myPath returns nothing. I just want to obtain the Desktop path. I'm on Windows 7 64 bits.

You need to give it room to put the data into:
T_CHAR myPath[ MAX_PATH ];
SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);

Related

CryptUnprotectData function returning encrypted value

I have been trying to wrap my head around how to use the CryptUnprotectData function in c++, but I can seem to get it correct. As of now, the function doesn't seem to be doing anything (the value returned is the value given. When I use GetLastError(), I get an error code of 87 (invalid parameter).
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string data = ("data I want to decode");
cout << "Encrypted data: " << data << endl;
DATA_BLOB DataOut;
BYTE* pbDataOutput = (BYTE*)data.data();
DataOut.pbData = pbDataOutput;
DataOut.cbData = strlen((char*)pbDataOutput);
DATA_BLOB DataVerify;
LPWSTR pDescrOut = NULL;
if (CryptUnprotectData(&DataOut, NULL, NULL, NULL, NULL,0,&DataVerify)){
cout << "decrypted data: " << DataVerify.pbData << endl;
LocalFree(DataVerify.pbData);
}
else{
cout << "Decryption error: ";
cout << GetLastError() << endl;
}
return 0;
}
I have tried copying the code from the documentation here and whittling it down until it just decrypts a string, but I get the same error.

Trouble printing DIDEVICEINSTANCE tszProductName to Windows Console

I've been following a DirectInput tutorial and am having trouble printing a list of DirectInput device product names to a Windows console.
I'm using VS19's C++ compiler and am compiling with UNICODE defined. Since tszProductName is of type TCHAR which resolves to WCHAR I followed this stack overflow answer to allow the windows console to print unicode while also avoiding mixing wcout with cout in the same program.
Before I made those changes nothing was printed. Now the console prints '쳌' repeatedly for each device name. I tried switching from Unicode to Multibyte and going back to cout to no avail. Here is the code in question.
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#pragma comment (lib,"dinput8.lib")
#pragma comment (lib,"dxguid.lib")
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <locale.h>
#include <clocale>
#include <io.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
IDirectInput8* dev;
std::vector<LPDIRECTINPUTDEVICE8> gameControllers;
BOOL CALLBACK enumGameControllers(LPCDIDEVICEINSTANCE devInst, LPVOID pvRef) {
LPDIRECTINPUTDEVICE8 gameController;
if (FAILED(dev->CreateDevice(devInst->guidInstance, &gameController, NULL)))
return DIENUM_CONTINUE;
else {
gameControllers.push_back(gameController);
return DIENUM_CONTINUE;
}
}
int wmain(int argc, wchar_t* argv[]) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
char* a = setlocale(LC_ALL, "en-US.UTF8");
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
CONSOLE_FONT_INFOEX fontInfo;
fontInfo.cbSize = sizeof(fontInfo);
fontInfo.FontFamily = 20;
fontInfo.FontWeight = 400;
fontInfo.nFont = 0;
const wchar_t myFont[] = L"Lucida Console";
fontInfo.dwFontSize = { 8, 16 };
std::copy(myFont, myFont + _countof(myFont), fontInfo.FaceName);
SetCurrentConsoleFontEx(hConsole, false, &fontInfo);
if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&dev, NULL))) {
std::wcout << L"Critical error: Unable to create the main DirectInput 8 COM object!\n";
return 1;
}
if (FAILED(dev->EnumDevices(DI8DEVCLASS_GAMECTRL, &enumGameControllers, NULL, DIEDFL_ATTACHEDONLY))) {
std::wcout << L"Critical error: Unable to enumerate input devices!\n";
return 1;
}
if (gameControllers.empty()) {
std::wcout << L"No peripherals found\n";
return 1;
}
int count = 1;
std::wcout << L"Number of devices: " << gameControllers.size() << std::endl;
for (LPDIRECTINPUTDEVICE8 i : gameControllers) {
DIDEVICEINSTANCE deviceInfo;
i->GetDeviceInfo(&deviceInfo);
std::wcout << L"Device Number " << count << L": ";
std::wcout << deviceInfo.tszProductName << std::endl;
if (FAILED(i->SetCooperativeLevel(GetConsoleWindow(), DISCL_BACKGROUND | DISCL_EXCLUSIVE))) {
std::wcout << L"Cooperative level could not be set\n";
return 1;
}
++count;
}
return 0;
}
Thanks in advance for any suggestions and/or solutions.
DIDEVICEINSTANCE deviceInfo;
i->GetDeviceInfo(&deviceInfo);
Problems with this code are that:
DIDEVICEINSTANCE::dwSize is not initialized as required, and
the return value of IDirectInputDevice8::GetDeviceInfo is not checked for errors.
Change to the following, instead.
DIDEVICEINSTANCE deviceInfo = { sizeof(DIDEVICEINSTANCE) };
if(i->GetDeviceInfo(&deviceInfo) != DI_OK) { /* call failed, handle error */ }

ReadProcessMemory CheatEngine Value

I want to get a value from a Proccess, here it's a simple test with "Calculator".
First, I get the address with CheatEngine. Secondly I put it in ReadProcessMemory.
But ReadProcessMemory return 0, I think I miss something, I've found something with BaseAddress, but I still have bad results. Google is out of results for me, so I ask you!
#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int value;
DWORD pid;
HWND hwnd = FindWindow(NULL,"Calculatrice");
if(!hwnd)
{
cout << "Window not found!";
}
else
{
GetWindowThreadProcessId(hwnd,&pid);
HANDLE phandle = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
if(!phandle)
{
cout <<"Could not get handle!";
}
else
{
cout << ReadProcessMemory(phandle,(LPVOID)0xC71657E900,&value,sizeof(value),0) << endl;
cout << value;
getch();
return 0;
}
}
}
Resolved ! When the address is too big, because it's a 64bit address, the program change to a 32bit address, that's why it don't work.
So, in Visual Studio, set to x64.

Print all std::locale names (Windows)

My program checks for uppercase letters in German language.
#include <iostream>
#include <boost/algorithm/string/classification.hpp>
#include <boost/locale.hpp>
using namespace std;
int main()
{
locale::global(locale("Germany_german"));
//locale::global(locale("de_DE.UTF-8")); //Also tried "de_DE.UTF-8", but does not work
string str1 = "über";
cout << boolalpha << any_of(str1.begin(), str1.end(), boost::algorithm::is_upper()) << endl;
string str2 = "Ää";
cout << boolalpha << any_of(str2.begin(), str2.end(), boost::algorithm::is_upper()) << endl;
return 0;
}
program crashes with error on console
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
I don't know what that exact locale string is, "de_DE.UTF-8" doesn't work as well.
Is there any way I can get exact locale name strings for all locales supported by OS. May be there is a list somewhere in header files, but I don't see anything <locale> header.
I wrote a program to print all supported locale names.
#include <Windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ostream>
#include <iterator>
using namespace std;
vector<wstring> locals;
BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
locals.push_back(pStr);
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALL, NULL, NULL);
for (vector<wstring>::const_iterator str = locals.begin(); str != locals.end(); ++str)
wcout << *str << endl;
wcout << "Total " << locals.size() << " locals found." << endl;
return 0;
}
Works great.
...
de
de-AT
de-CH
de-DE
de-DE_phoneb
de-LI
de-LU
...
Total 429 locals found.
#user1 The following might do the same as your elegant code. I can't test it because of the C1189 compiler error.
#include <Winnls.h>
#include <iostream>
#include <ostream>
using namespace std;
int size = 0;
BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam) {
size++;
wcout << *pStr << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALL, NULL, NULL);
wcout << "Total " << size << " locales found." << endl;
return 0;
}

C++ get current file running exe

So, I've now fought with this for 2 days, still the same error.
I've been on over 300 results with google, still same FAILURE. It shows up as HEX all the time or it doesn't work at all.
This is not using any external librarys and no .net framework.
100% non-dependent.
I've tried over 30 methods.
TCHAR szExeFileName[MAX_PATH];
GetModuleFileName(NULL, szExeFileName, MAX_PATH);
^ Doesn't work; returns hex.
The code is in a void.
#include "SharedHeader.h"
#include <Psapi.h>
#include "CommandLine_Pres.h"
#include <TlHelp32.h>
using namespace std;
void filePath()
{
// Figure out file path of current file
char cCurrentPath[FILENAME_MAX];
if(!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
{
cout << "error" << endl;
}
cCurrentPath[sizeof(cCurrentPath) -1] = '\0';
cout << cCurrentPath << endl;
// Get process id, filename
//cout << GetCommandLine();
int procId = GetCurrentProcessId();
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
/*
DOES NOT WORK BELOW [debug]
HANDLE Handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,GetCurrentProcessId());
if(Handle)
{
TCHAR Buffer[MAX_PATH];
if(GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
{
}
else
{
}
CloseHandle(Handle);
}*/
}
To get the address of the current running process you can use:
#include <iostream>
int main(int argc, char** argv)
{
std::cout << argv[0] << std::endl;
getchar();
return 0;
}
or:
#include <iostream>
#include <windows.h>
int main()
{
char szExeFileName[MAX_PATH];
GetModuleFileName(NULL, szExeFileName, MAX_PATH);
std::cout << szExeFileName;
getchar();
return 0;
}