'RegDeleteTree' not declared // Compilation error - c++

I'm trying to delete a windows registry key and all its subkeys, specifically the 'Open with SHCP' key (which I created) and all its subkeys and values. I have the code, but it throws me this error:
'RegDeleteTree' was not declared in this scope
Code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
HKEY hKey;
cout << "Deleting Tree:\n\n";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Classes\\*\\shell", 0, KEY_ALL_ACCESS, &hKey)== ERROR_SUCCESS)
{
cout << "Successfully opened key\n";
if(RegDeleteTree(hKey,"Open with SHCP") == ERROR_SUCCESS)
{
cout << "Successfully deleted the key\n";
}
else
{
cout << "Failed to delete the tree\n";
}
RegCloseKey(hKey);
}
else
{
cout << "Error, no tree available\n";
}
cin.get();
return 0;
}
I'm using Windows 7 and Dev-C++ 5.6.3.
Also, I'm able to use other functions like RegOpenKeyEx and RegCreateKeyEx.
What am I missing?

You need the SDK for Vista or later, and you need to set _WIN32_WINNT :
#define _WIN32_WINNT 0x0600
#include <Windows.h>
From the MSDN page for RegDeleteTree:
To compile an application that uses this function, define _WIN32_WINNT as 0x0600 or later. For more information, see Using the Windows Headers.

Related

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.

How to Enumerate Names of All Named Pipes in a Process?

I need to open a certain named pipe so I can fuzz test it, however my test code does not have access to the same data used to generate the name of the named pipe. However I can recognize the name of the pipe and then use that name to open up the pipe for fuzzing.
I used this forum post to start enumerating names of the handles on the system:
http://forum.sysinternals.com/howto-enumerate-handles_topic18892.html
However it seems that won't work with named pipes for some reason.
TL;DR: What API(s) do I need to use to list the names of all named pipes in the current process on Windows?
This will enumerate all named pipes in the system, or at the very least put you a step in the right direction.
This works in MinGW when built with -fpermissive. It should work with similar settings in MSVC.
#ifndef _WIN32_WINNT
// Windows XP
#define _WIN32_WINNT 0x0501
#endif
#include <Windows.h>
#include <Psapi.h>
// mycreatepipeex.c is at http://www.davehart.net/remote/PipeEx.c
// I created a simple header based on that.
#include "mycreatepipeex.h"
#include <iostream>
#include <cstdio>
#include <errno.h>
void EnumeratePipes()
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
#define TARGET_PREFIX "//./pipe/"
const char *target = TARGET_PREFIX "*";
memset(&FindFileData, 0, sizeof(FindFileData));
hFind = FindFirstFileA(target, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
std::cerr << "FindFirstFileA() failed: " << GetLastError() << std::endl;
return;
}
else
{
do
{
std::cout << "Pipe: " << TARGET_PREFIX << FindFileData.cFileName << std::endl;
}
while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
#undef TARGET_PREFIX
return;
}
int main(int argc, char**argv)
{
HANDLE read = INVALID_HANDLE_VALUE;
HANDLE write = INVALID_HANDLE_VALUE;
unsigned char pipe_name[MAX_PATH+1];
BOOL success = MyCreatePipeEx(&read, &write, NULL, 0, 0, 0, pipe_name);
EnumeratePipes();
if ( success == FALSE )
{
std::cerr << "MyCreatePipeEx() failed: " << GetLastError() << std::endl;
return 1;
}
FILE *f = fopen((const char*)pipe_name, "rwb");
if ( f == NULL )
{
std::cerr << "fopen(\"" << pipe_name << "\") failed: " << (int)errno << std::endl;
}
CloseHandle(read);
CloseHandle(write);
return 0;
}

Obtaining the windows desktop path

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

What's wrong with this GetDefaultAudioEndpoint program?

Here's a very simple program using the function:
#include <windows.h>
#include <tchar.h>
#include <atlstr.h>
#include <mmdeviceapi.h>
#include <devicetopology.h>
#include <functiondiscoverykeys.h>
#include <iostream>
using namespace std;
int main()
{
HRESULT hr;
CComPtr<IMMDeviceEnumerator> pMMDeviceEnumerator;
pMMDeviceEnumerator->GetDefaultAudioEndpoint(eCapture, eMultimedia, 0);
//cout << hr;
return 0;
}
When I try to run this, I get the following error:
Debug Assertion Failed!
Program: ...
File: c:\program files\microsoft visual studio 8\vc\atlmfc\include\atlcomcli.h
Line: 154
Expression: p!=0
What's wrong with this? I'm just now trying to learn how to use this function. Thanks!
EDIT:
I've changed the program to this:
//#include <windows.h>
//#include <tchar.h>
#include <atlstr.h>
#include <mmdeviceapi.h>
//#include <devicetopology.h>
//#include <functiondiscoverykeys.h>
#include <iostream>
using namespace std;
// helper class to CoInitialize/CoUninitialize
class CCoInitialize {
private:
HRESULT m_hr;
public:
CCoInitialize(PVOID pReserved, HRESULT &hr)
: m_hr(E_UNEXPECTED) { hr = m_hr = CoInitialize(pReserved); }
~CCoInitialize() { if (SUCCEEDED(m_hr)) { CoUninitialize(); } }
};
int main()
{
CComPtr<IMMDeviceEnumerator> pMMDeviceEnumerator;
HRESULT hr = pMMDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
if (FAILED(hr)) {
cout << "failed" << endl;
return __LINE__;
}
CCoInitialize ci(NULL, hr);
pMMDeviceEnumerator->GetDefaultAudioEndpoint(eCapture, eMultimedia, 0);
//cout << hr;
return 0;
}
When I run it, I get the output of "failed". What's happening?
EDIT:
Alright, now I've changed the code enough to get it running all the way through without any failures. i.e.,
HRESULT hr = S_OK;
cout << hr;
// initialize COM
CCoInitialize ci(NULL, hr);
if (FAILED(hr)) {
cout << "failed1" << endl;
return __LINE__;
}
cout << hr;
// get enumerator
CComPtr<IMMDeviceEnumerator> pMMDeviceEnumerator;
hr = pMMDeviceEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
if (FAILED(hr)) {
cout << "failed2" << endl;
return __LINE__;
}
cout << hr;
// get default render/capture endpoints
CComPtr<IMMDevice> pRenderEndpoint;
hr = pMMDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pRenderEndpoint);
if (FAILED(hr)) {
cout << "failed3" << endl;
return __LINE__;
}
cout << hr;
return 0;
Some of the trouble I was having earlier with this example (see comments on the answers) was fixed just by removing some of the code. But as I run this new body of the main() function, I get the output "0000", meaning that cout << hr always evaluates to "0". Is this a good thing? What info can I get about the default device now? hr. and hr-> don't really bring up any menus, so I'm kind of in the dark. Thanks!
pMMDeviceEnumerator variable holds a pointer, which is NULL. When you try to call an interface method on this pointer, -> operator checks this nullness and issues an assertion failure.
Windows SDK samples show how to use this function and API, check them under: \Samples\multimedia\audio, e.g. osd sample.
This sample is a Win32-based application that demonstrates the use of the Vista APIs for monitoring the default audio output device and
its current volume setting. The sample is written in C++.
OSD does not run on earlier versions of Windows, including Windows XP, Windows 2000, Windows Me, and Windows 98.
UPD: Things in main one needs to reach the GetDefaultAudioEndpoint API call - Sample: find out if your default audio playback and audio capture devices are on the same hardware.

How to find out if a folder exists and how to create a folder?

I'm trying to create a folder if it doesn't exist. I'm using Windows and I am not interested on my code working in other platforms.
Never mind, I found the solution. I was just having a inclusion problem. The answer is:
#include <io.h> // For access().
#include <sys/types.h> // For stat().
#include <sys/stat.h> // For stat().
#include <iostream>
#include <string>
using namespace std;
string strPath;
cout << "Enter directory to check: ";
cin >> strPath;
if ( access( strPath.c_str(), 0 ) == 0 )
{
struct stat status;
stat( strPath.c_str(), &status );
if ( status.st_mode & S_IFDIR )
{
cout << "The directory exists." << endl;
}
else
{
cout << "The path you entered is a file." << endl;
}
}
else
{
cout << "Path doesn't exist." << endl;
}
The POSIX-compatible call is mkdir. It silently fails when the directory already exists.
If you are using the Windows API, then CreateDirectory is more appropriate.
Use boost::filesystem::exists to check if file exists.
boost::filesystem::create_directories does just that: Give it a path, and it will create all missing directories in that path.