Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Help] Weird Dll error O.o I hate you c++

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
PsychicSymphony
Newbie cheater
Reputation: 0

Joined: 17 May 2011
Posts: 13
Location: Ontario

PostPosted: Thu May 19, 2011 8:00 pm    Post subject: [Help] Weird Dll error O.o I hate you c++ Reply with quote

I am trying to inject this DLL into a program using a remote thread and I am just testing injecting it through my custom injector.

When I try to inject my DLL it fails to load. I used Dependency Walker to check the dll for any problems with the DLL I get these 2 errors.

Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.

I dont exactly know what they mean but anyone know how to fix.

Oh yea the code.

Code for injector:
[code]
#include <windows.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

void Inject(HANDLE hProcess, const char* dllname, const char* funcname) {
HMODULE kernel32 = NULL;

FARPROC loadlibrary = NULL;
FARPROC getprocaddress = NULL;
FARPROC exitprocess = NULL;
FARPROC exitthread = NULL;
FARPROC freelibraryandexitthread = NULL;

LPBYTE workspace = NULL;
DWORD workspaceIndex = 0;

LPVOID codecaveAddress = NULL;
DWORD dwCodecaveAddress = 0;

char injectDllName[MAX_PATH + 1] = {0};
char injectFuncName[MAX_PATH + 1] = {0};
char injectError0[MAX_PATH + 1] = {0};
char injectError1[MAX_PATH + 1] = {0};
char injectError2[MAX_PATH + 1] = {0};
char user32Name[MAX_PATH + 1] = {0};
char msgboxName[MAX_PATH + 1] = {0};

DWORD user32NameAddr = 0;
DWORD user32Addr = 0;
DWORD msgboxNameAddr = 0;
DWORD msgboxAddr = 0;
DWORD dllAddr = 0;
DWORD dllNameAddr = 0;
DWORD funcNameAddr = 0;
DWORD error0Addr = 0;
DWORD error1Addr = 0;
DWORD error2Addr = 0;

DWORD codecaveExecAddr = 0;

HANDLE hThread = NULL;

DWORD dwTmpSize = 0;

DWORD oldProtect = 0;
DWORD bytesRet = 0;

kernel32 = LoadLibrary("kernel32.dll");

loadlibrary = GetProcAddress(kernel32, "LoadLibraryA");
getprocaddress = GetProcAddress(kernel32, "GetProcAddress");
exitprocess = GetProcAddress(kernel32, "ExitProcess");
exitthread = GetProcAddress(kernel32, "ExitThread");
freelibraryandexitthread = GetProcAddress(kernel32, "FreeLibraryAndExitThread");

_snprintf(injectDllName, MAX_PATH, "%s", dllname);
_snprintf(injectFuncName, MAX_PATH, "%s", funcname);
_snprintf(user32Name, MAX_PATH, "user32.dll");
_snprintf(msgboxName, MAX_PATH, "MessageBoxA");

_snprintf(injectError0, MAX_PATH, "Error");
_snprintf(injectError1, MAX_PATH, "Sorry it kinda failed to load :/ %s", injectDllName);
_snprintf(injectError2, MAX_PATH, "Couldnt start the thread idk Razz %s", injectFuncName);

workspace = (LPBYTE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 1024);

codecaveAddress = VirtualAllocEx(hProcess, 0, 1024, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
dwCodecaveAddress = PtrToUlong(codecaveAddress);

user32Addr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = 0;
memcpy(workspace + workspaceIndex, &dwTmpSize, 4);
workspaceIndex += 4;

msgboxAddr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = 0;
memcpy(workspace + workspaceIndex, &dwTmpSize, 4);
workspaceIndex += 4;

dllAddr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = 0;
memcpy(workspace + workspaceIndex, &dwTmpSize, 4);
workspaceIndex += 4;

user32NameAddr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = (DWORD)strlen(user32Name) + 1;
memcpy(workspace + workspaceIndex, user32Name, dwTmpSize);
workspaceIndex += dwTmpSize;

msgboxNameAddr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = (DWORD)strlen(msgboxName) + 1;
memcpy(workspace + workspaceIndex, msgboxName, dwTmpSize);
workspaceIndex += dwTmpSize;

dllNameAddr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = (DWORD)strlen(injectDllName) + 1;
memcpy(workspace + workspaceIndex, injectDllName, dwTmpSize);
workspaceIndex += dwTmpSize;

funcNameAddr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = (DWORD)strlen(injectFuncName) + 1;
memcpy(workspace + workspaceIndex, injectFuncName, dwTmpSize);
workspaceIndex += dwTmpSize;

error0Addr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = (DWORD)strlen(injectError0) + 1;
memcpy(workspace + workspaceIndex, injectError0, dwTmpSize);
workspaceIndex += dwTmpSize;

error1Addr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = (DWORD)strlen(injectError1) + 1;
memcpy(workspace + workspaceIndex, injectError1, dwTmpSize);
workspaceIndex += dwTmpSize;

error2Addr = workspaceIndex + dwCodecaveAddress;
dwTmpSize = (DWORD)strlen(injectError2) + 1;
memcpy(workspace + workspaceIndex, injectError2, dwTmpSize);
workspaceIndex += dwTmpSize;

workspace[workspaceIndex++] = 0xCC;
workspace[workspaceIndex++] = 0xCC;
workspace[workspaceIndex++] = 0xCC;

codecaveExecAddr = workspaceIndex + dwCodecaveAddress;

workspace[workspaceIndex++] = 0x68;
memcpy(workspace + workspaceIndex, &user32NameAddr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xB8;
memcpy(workspace + workspaceIndex, &loadlibrary, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;

workspace[workspaceIndex++] = 0x68;
memcpy(workspace + workspaceIndex, &msgboxNameAddr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0x50;

workspace[workspaceIndex++] = 0xB8;
memcpy(workspace + workspaceIndex, &getprocaddress, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;

workspace[workspaceIndex++] = 0xA3;
memcpy(workspace + workspaceIndex, &msgboxAddr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0x68;
memcpy(workspace + workspaceIndex, &dllNameAddr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xB8;
memcpy(workspace + workspaceIndex, &loadlibrary, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;

workspace[workspaceIndex++] = 0x83;
workspace[workspaceIndex++] = 0xF8;
workspace[workspaceIndex++] = 0x00;

workspace[workspaceIndex++] = 0x75;
workspace[workspaceIndex++] = 0x1E;

workspace[workspaceIndex++] = 0x6A;
workspace[workspaceIndex++] = 0x10;

workspace[workspaceIndex++] = 0x68;
memcpy(workspace + workspaceIndex, &error0Addr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0x68;
memcpy(workspace + workspaceIndex, &error1Addr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0x6A;
workspace[workspaceIndex++] = 0x00;

workspace[workspaceIndex++] = 0xA1;
memcpy(workspace + workspaceIndex, &msgboxAddr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;

workspace[workspaceIndex++] = 0x6A;
workspace[workspaceIndex++] = 0x00;

workspace[workspaceIndex++] = 0xB8;
memcpy(workspace + workspaceIndex, &exitprocess, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;

workspace[workspaceIndex++] = 0xA3;
memcpy(workspace + workspaceIndex, &dllAddr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0x68;
memcpy(workspace + workspaceIndex, &funcNameAddr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0x50;

workspace[workspaceIndex++] = 0xB8;
memcpy(workspace + workspaceIndex, &getprocaddress, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;

workspace[workspaceIndex++] = 0x83;
workspace[workspaceIndex++] = 0xF8;
workspace[workspaceIndex++] = 0x00;

workspace[workspaceIndex++] = 0x75;
workspace[workspaceIndex++] = 0x1C;

workspace[workspaceIndex++] = 0x6A;
workspace[workspaceIndex++] = 0x10;

workspace[workspaceIndex++] = 0x68;
memcpy(workspace + workspaceIndex, &error0Addr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0x68;
memcpy(workspace + workspaceIndex, &error2Addr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0x6A;
workspace[workspaceIndex++] = 0x00;

workspace[workspaceIndex++] = 0xA1;
memcpy(workspace + workspaceIndex, &msgboxAddr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;

workspace[workspaceIndex++] = 0x6A;
workspace[workspaceIndex++] = 0x00;

workspace[workspaceIndex++] = 0xB8;
memcpy(workspace + workspaceIndex, &exitprocess, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;

#if 1
workspace[workspaceIndex++] = 0x6A;
workspace[workspaceIndex++] = 0x00;

workspace[workspaceIndex++] = 0xB8;
memcpy(workspace + workspaceIndex, &exitthread, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;
#endif

#if 0
workspace[workspaceIndex++] = 0x6A;
workspace[workspaceIndex++] = 0x00;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0x35;
memcpy(workspace + workspaceIndex, &dllAddr, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xB8;
memcpy(workspace + workspaceIndex, &freelibraryandexitthread, 4);
workspaceIndex += 4;

workspace[workspaceIndex++] = 0xFF;
workspace[workspaceIndex++] = 0xD0;
#endif

VirtualProtectEx(hProcess, codecaveAddress, workspaceIndex, PAGE_EXECUTE_READWRITE, &oldProtect);

WriteProcessMemory(hProcess, codecaveAddress, workspace, workspaceIndex, &bytesRet);

VirtualProtectEx(hProcess, codecaveAddress, workspaceIndex, oldProtect, &oldProtect);

FlushInstructionCache(hProcess, codecaveAddress, workspaceIndex);

HeapFree(GetProcessHeap(), 0, workspace);

hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)((void*)codecaveExecAddr), 0, 0, NULL);
WaitForSingleObject(hThread, INFINITE);

VirtualFreeEx(hProcess, codecaveAddress, 0, MEM_RELEASE);
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
BOOL result = FALSE;

char* workingDir = new char[255];
char* exeString = new char[255];

ifstream file("exe.txt");
file.getline(exeString, 255);
file.getline(workingDir, 255);
file.close();

cout << workingDir;
cout << exeString;

char dllPath[MAX_PATH + 1] = {0};

_snprintf(dllPath, MAX_PATH, "D2Dll.dll");

si.cb = sizeof(STARTUPINFO);

result = CreateProcess(NULL, exeString, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, workingDir, &si, &pi);

if(!result) {
MessageBox(0, "Process failed to load :/", "Damn....", MB_ICONERROR);
return -1;
}

Inject(pi.hProcess, dllPath, "Initialize");

ResumeThread(pi.hThread);

return 0;
}
[/code]



Code for Dll:
[code]
#include <windows.h>


// Define the DLL's main function

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ulReason, LPVOID lpReserved)
{
// Get rid of compiler warnings since we do not use this parameter

UNREFERENCED_PARAMETER(lpReserved);

// If we are attaching to a process

if(ulReason == DLL_PROCESS_ATTACH)
{
// Do not need the thread based attach/detach messages in this DLL

DisableThreadLibraryCalls(hModule);
}

// Signal for Loading/Unloading

return (TRUE);
}

extern "C" __declspec(dllexport) void Initialize()
{
MessageBox(0, "Locked and Loaded.", "DLL Injection Successful!", 0);
}
[/code]

Please help im using Microsoft Visual C++ 2010 Express btw

_________________
Psychic Symphony
////
// Very Happy //
// //
////
Back to top
View user's profile Send private message
Innovation
Grandmaster Cheater
Reputation: 12

Joined: 14 Aug 2008
Posts: 617

PostPosted: Thu May 19, 2011 9:13 pm    Post subject: Reply with quote

I don't like how the injector was written. Anyway, your DLL's project settings are most likely messed up. Create an "Empty Project", and change the "Configuration Type" to "Dynamic Library (.dll)".
Back to top
View user's profile Send private message
PsychicSymphony
Newbie cheater
Reputation: 0

Joined: 17 May 2011
Posts: 13
Location: Ontario

PostPosted: Fri May 20, 2011 2:24 pm    Post subject: Reply with quote

Yea that didnt work D:
_________________
Psychic Symphony
////
// Very Happy //
// //
////
Back to top
View user's profile Send private message
Innovation
Grandmaster Cheater
Reputation: 12

Joined: 14 Aug 2008
Posts: 617

PostPosted: Fri May 20, 2011 3:34 pm    Post subject: Reply with quote

PsychicSymphony wrote:
Yea that didnt work D:

Does Dependency Walker still detect errors in the DLL?
Back to top
View user's profile Send private message
PsychicSymphony
Newbie cheater
Reputation: 0

Joined: 17 May 2011
Posts: 13
Location: Ontario

PostPosted: Fri May 20, 2011 9:07 pm    Post subject: Reply with quote

Yea it still has the same 2 errors :/
_________________
Psychic Symphony
////
// Very Happy //
// //
////
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sun May 22, 2011 10:27 am    Post subject: Reply with quote

Make sure you include a .DEF file in your project to explain the exports to the linker.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
PsychicSymphony
Newbie cheater
Reputation: 0

Joined: 17 May 2011
Posts: 13
Location: Ontario

PostPosted: Sun May 22, 2011 4:38 pm    Post subject: Reply with quote

thanks Very Happy I wish there was a thank button lol
_________________
Psychic Symphony
////
// Very Happy //
// //
////
Back to top
View user's profile Send private message
Krähne
Expert Cheater
Reputation: 0

Joined: 06 Jun 2010
Posts: 108
Location: Inside of my Kernel

PostPosted: Sun May 22, 2011 4:59 pm    Post subject: Reply with quote

PsychicSymphony wrote:
thanks Very Happy I wish there was a thank button lol


It already exists.

_________________
Excuse me if you don't understand what I just said, but "english" isn't my native language.
Back to top
View user's profile Send private message MSN Messenger
Innovation
Grandmaster Cheater
Reputation: 12

Joined: 14 Aug 2008
Posts: 617

PostPosted: Sun May 22, 2011 8:30 pm    Post subject: Reply with quote

Wiccaan wrote:
Make sure you include a .DEF file in your project to explain the exports to the linker.

Isn't PsychicSymphony using __declspec(dllexport)?
Back to top
View user's profile Send private message
sponge
I'm a spammer
Reputation: 1

Joined: 07 Nov 2006
Posts: 6009

PostPosted: Sun May 22, 2011 11:16 pm    Post subject: Reply with quote

__declspec(dllexport) decorates the exported symbol.

Also, http://opcode0x90.wordpress.com/2011/01/15/injecting-dll-into-process-on-load/

_________________
Back to top
View user's profile Send private message
Innovation
Grandmaster Cheater
Reputation: 12

Joined: 14 Aug 2008
Posts: 617

PostPosted: Mon May 23, 2011 4:30 am    Post subject: Reply with quote

sponge wrote:
__declspec(dllexport) decorates the exported symbol.

MSDN wrote:
__declspec(dllexport) adds the export directive to the object file so you do not need to use a .def file.

MSDN wrote:
If C++ name mangling is not desired, either use a .def file (EXPORTS keyword) or declare the function as extern "C".

Why is a .def file needed in this case?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Mon May 23, 2011 9:07 am    Post subject: Reply with quote

Innovation wrote:
sponge wrote:
__declspec(dllexport) decorates the exported symbol.

MSDN wrote:
__declspec(dllexport) adds the export directive to the object file so you do not need to use a .def file.

MSDN wrote:
If C++ name mangling is not desired, either use a .def file (EXPORTS keyword) or declare the function as extern "C".

Why is a .def file needed in this case?


I would assume due to how its being used in his injector.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites