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 


[C++] How do I use a function from a separate dll?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
TraxMate
Master Cheater
Reputation: 0

Joined: 01 Mar 2008
Posts: 363

PostPosted: Sun Oct 19, 2008 1:13 pm    Post subject: [C++] How do I use a function from a separate dll? Reply with quote

The title says it all. I've searched and I didn't come up with a way to do it.
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Sun Oct 19, 2008 1:41 pm    Post subject: Reply with quote

Here's some sample code that I'll quickly code for ya here (sorry for errors if they occur.)

EXE:
Code:
#include <Windows.h>
#include <Tchar.h>

typedef INT (WINAPI * _Add)(__in INT nNum1, __in INT nNum2);
_Add Add;

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
    UNREFERENCED_PARAMETER(hInstance);
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    UNREFERENCED_PARAMETER(nShowCmd);
   
    HMODULE hDLL = NULL;
    int nAdded = NULL;
    LPTSTR lpAdded = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(TCHAR)*5);
   
    if (lpAdded != NULL)
    {
        hDLL = LoadLibrary(_T("Add.dll"));
        Add = (_Add)GetProcAddress(hDLL, "Add");

        if ((DWORD)Add != NULL)
        {
            nAdded = Add(10, 15);
            wsprintf(lpAdded, _T("%d"), nAdded);

            MessageBox(NULL, lpAdded, _T("Add function ret"), MB_OK);
        }
        HeapFree(GetProcessHeap(), 0, (LPVOID)lpAdded);
    }
    return 0;
}


DLL:
Code:
#include <Windows.h>

__declspec(dllexport) INT WINAPI Add(__in INT nNum1, __in INT nNum2)
{
    return (nNum1 + nNum2);
}

BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
    UNREFERENCED_PARAMETER(hModule);
    UNREFERENCED_PARAMETER(dwReason);
    UNREFERENCED_PARAMETER(lpReserved);
    return TRUE;
}

_________________
Back to top
View user's profile Send private message
TraxMate
Master Cheater
Reputation: 0

Joined: 01 Mar 2008
Posts: 363

PostPosted: Sun Oct 19, 2008 2:57 pm    Post subject: Reply with quote

Thx Very Happy. Now it's time to study the code and learn ^^-

EDIT: When I'm adding this to the dll:
Code:
void omg()
{
   MessageBox(NULL, _T("Test"), _T("Test"), MB_OK);
}
and add a CreateThread like this
Code:
BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
   UNREFERENCED_PARAMETER(hModule);
   UNREFERENCED_PARAMETER(dwReason);
   UNREFERENCED_PARAMETER(lpReserved);

   CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)omg, NULL, NULL, NULL);
   
   return TRUE;
}
Why does it spam the messagebox like in a loop? And it's not like a normal loop when you click OK it pops up again, now it's popping up all the time lagging up my comp so i have to restart it.
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Sun Oct 19, 2008 3:02 pm    Post subject: Reply with quote

Because DllMain has 4 different ways of being called.

DLL_PROCESS_ATTACH
DLL_PROCESS_DETACH
DLL_THREAD_ATTACH
DLL_THREAD_DETACH

so if you wanted to make it message box once, do

Code:
if (dwReason == DLL_PROCESS_ATTACH)
        CreateThread(...)

_________________
Back to top
View user's profile Send private message
TraxMate
Master Cheater
Reputation: 0

Joined: 01 Mar 2008
Posts: 363

PostPosted: Mon Oct 20, 2008 8:09 am    Post subject: Reply with quote

oh yeah.. forgot that Razz. Thanks again lurc, i would +rep you if i could.
Back to top
View user's profile Send private message
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