Solved Injected DLL crashing at CreateThread

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

snorble

Newbie
Full Member
Jan 30, 2016
9
52
0
Hello all, I am trying to do a simple DLL injection into notepad.exe. I am able to inject a DLL and open a message box, that works fine. But when I call CreateThread from DllMain, the notepad.exe process crashes. I am on Windows 7 64-bit, and notepad.exe is a 64-bit process.

Here is the code for the DLL I am compiling. I compile it as Release x64.
C++:
// DLLMenu2.cpp
#include <Windows.h>

//extern int Main();

DWORD WINAPI Main_Thread (LPVOID lpParam)
{
	::MessageBox(0, "The injection worked", "Injected MsgBox", 0);
	return true;
	//Main();
	//return S_OK;
}


BOOL WINAPI DllMain(HMODULE hmodule, DWORD dwreason, LPVOID lpreserved)
{
	if (dwreason == DLL_PROCESS_ATTACH)
	{
		//::MessageBox(0, "The injection worked", "Injected MsgBox", 0);
		CreateThread(0, 0x1000, &Main_Thread, 0, 0, NULL);
	}
}
Here is the injector code I am using, and I compile it as Release x64 in a console app.

C++:
// Injector2b.cpp

#include <Windows.h>
#include <iostream>
#include <string>

using namespace std;

bool Inject(const string & dll_path, int pid)
{
	HANDLE hprocess, hthread;
	LPVOID memory_region;
	bool status;
	FARPROC load_library;

	cout << "Opening the target process...";
	hprocess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
	if (hprocess == NULL)
	{
		cout << "OpenProcess() failed" << endl;
		return false;
	}
	else
		cout << "OpenProcess() success" << endl;

	cout << "Allocating memory in the target process...";
	memory_region = VirtualAllocEx(hprocess, 0, dll_path.size(), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
	if (memory_region == NULL)
	{
		cout << "VirtualAllocEx() failure" << endl;
		return false;
	}
	else
		cout << "VirtualAllocEx() success" << endl;

	cout << "Writing to the allocated memory in the target process...";
	status = WriteProcessMemory(hprocess, memory_region, (LPCVOID)dll_path.c_str(), dll_path.size(), 0);
	if (status == NULL)
	{
		cout << "WriteProcessMemory() failed" << endl;
		return false;
	}
	else
		cout << "WriteProcessMemory() success" << endl;

	HMODULE kernel32_module_handle = GetModuleHandle("Kernel32");
	load_library = GetProcAddress(kernel32_module_handle, "LoadLibraryA");
	cout << "Creating remote thread in target process...";
	hthread = CreateRemoteThread(hprocess, 0, 0, (LPTHREAD_START_ROUTINE)load_library, memory_region, 0, 0);
	if (hthread == NULL)
	{
		cout << "CreateRemoteThread() failed" << endl;
		return false;
	}
	else
		cout << "CreateRemoteThread() success" << endl;

	CloseHandle(hprocess);
	return true;
}

int main()
{
	string dll_path = "C:\\Users\\Dell\\Documents\\Visual Studio 2015\\Projects\\DLLMenu2\\x64\\Release\\DLLMenu2.dll";
	int pid;

	cout << "Enter PID: ";
	cin >> pid;
	
	if (Inject(dll_path, pid))
		cout << "Inject() success" << endl;
	else
		cout << "Inject() failed" << endl;

	cin.get();
	return 0;
}
 

snorble

Newbie
Full Member
Jan 30, 2016
9
52
0
Thank you so much mambda! It looks like not returning TRUE in DllMain was the problem. Such a small detail, but made my morning. Thanks again!
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods