View previous topic :: View next topic |
Author |
Message |
Snootae Grandmaster Cheater
Reputation: 0
Joined: 16 Dec 2006 Posts: 969 Location: --->
|
Posted: Thu Apr 17, 2008 8:40 am Post subject: C++ function dll |
|
|
hey, i was wondering if anyone could give me an example of how to load a function from a dll or explain how to do it, and how to use it in a program, i have googled, but i got very confused because the examples i was looking at all involved returning a value
_________________
|
|
Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Thu Apr 17, 2008 8:54 am Post subject: |
|
|
you jsut want to export functions and call them on other programs ?
|
|
Back to top |
|
 |
systat Advanced Cheater
Reputation: 0
Joined: 15 Feb 2008 Posts: 54
|
Posted: Thu Apr 17, 2008 10:49 am Post subject: |
|
|
Code: | http://www.codeproject.com/KB/DLL/loadingdll.aspx |
_________________
uuuuuuuuuuuuu |
|
Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Thu Apr 17, 2008 1:44 pm Post subject: |
|
|
Theres 2 ways to do it.
You can typedef a new function pointer then declare a new instance of that pointer and then give it the value of the address of the function in the dll.
typedef (type __stdcall Name*)(Parameters);
Name FunctionName
FunctionName = (Name)GetProcAddress( LoadLibrary(_T("DllName" )), "Function" );
OR you can make your own new _declspec(naked) function that just jumps straight to the function addy.
DWORD AddyToFunction = (DWORD)GetProcAddress( LoadLibrary(_T("DllName" )), "Function" );
_declspec(naked) Type WINAPI FunctionName( Parameters );
{ _asm jmp dword ptr ds:[AddyToFunction] }
Once you have that, you can call it like a normal function, so you would call FunctionName
Heres an example for loading LoadLibrary from kernel32.dll
Code: | DWORD _LLW = (DWORD)GetProcAddress( LoadLibrary( _T("Kernel32.dll") ), "LoadLibraryW");
_declspec(naked) HMODULE WINAPI _LoadLibrary( __in LPCTSTR lpFileName )
{
_asm jmp dword ptr ds:[_LLW]
} |
Then i just call _LoadLibrary with the same parameters as LoadLibrary
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Apr 17, 2008 2:46 pm Post subject: |
|
|
You really should avoid using inline ASM for such things like this. It's bad practice. Just call the API directly.
You can also export the functions and import them into your project to dynamically call to them. I made a signature scanner that is designed like this which you can check out here:
http://www.extalia.com/forums/viewtopic.php?f=21&t=2789
The function is exported based on:
Code: | #ifdef SIGSCAN_EXPORTS
#define SIGSCAN_API __declspec(dllexport)
#else
#define SIGSCAN_API __declspec(dllimport)
#endif |
Inside the project properties under the preprocessor definitions, SIGSCAN_EXPORTS is defined. You only use that for the DLL project. Inside the project that imports the DLL you do not use it. This way the #ifdef is set to import the function in the real project.
Then the define would look like:
Code: | SIGSCAN_API DWORD __stdcall FindPattern(DWORD dwProcId, BYTE* bBaseAddress, DWORD dwModuleSize, TCHAR* tszPattern, int iOffset, TCHAR* tszMask); |
Simple enough, just drop the .DLL in with the .exe your program makes and include the sigscan.h file and you are set.
That project should give you a general idea of how to do it.
_________________
- Retired. |
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Apr 17, 2008 4:57 pm Post subject: |
|
|
Read the code project link it's very useful. But if you want heres the code, it was commented out so you should be able to understand it. But there are other scenarios where you can't do this so read the code project link.
Code: |
//You need to declare types to point on classes/functions in LoadMe.dll
//Assume, you have a function in your LoadMe.dll with a name
//EntryPoint, which takes two parameters of types int and const char *,
//and is of type void. You need to create a new type as a
//pointer to that function as it is shown below.
typedef void (*EntryPointfuncPtr)(int argc, const char * argv );
//Declare an HINSTANCE and load the library dynamically. Don’t forget
//to specify a correct path to the location of LoadMe.dll
HINSTANCE LoadME;
LoadMe = LoadLibrary("..\\enter a Path To Your Dll here\\LoadMe.dll");
// Check to see if the library was loaded successfully
if (LoadMe != 0)
printf("LoadMe library loaded!\n");
else
printf("LoadMe library failed to load!\n");
//declare a variable of type pointer to EntryPoint function, a name of
// which you will later use instead of EntryPoint
EntryPointfuncPtr LibMainEntryPoint;
// GetProcAddress – is a function, which returns the address of the
// specified exported dynamic-link library (DLL) function. After
// performing this step you are allowed to use a variable
// LibMainEntryPoint as an equivalent of the function exported in
// LoadMe.dll. In other words, if you need to call
// EntryPoint(int, const char *) function, you call it as
// LibMainEntryPoint(int, const char *)
LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadMe,"entryPoint");
|
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Thu Apr 17, 2008 5:27 pm Post subject: |
|
|
dont forget to Code: | #pragma comment( lib, "libname.lib" ) |
|
|
Back to top |
|
 |
Snootae Grandmaster Cheater
Reputation: 0
Joined: 16 Dec 2006 Posts: 969 Location: --->
|
Posted: Thu Apr 17, 2008 7:16 pm Post subject: |
|
|
thanks guys, i think i've got it now
this is a great forum
_________________
|
|
Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Fri Apr 18, 2008 1:55 am Post subject: |
|
|
Just do it like this:
Code: | #ifdef __cplusplus
extern "C" {
#endif
__declspec( dllexport ) void Hello;
#ifdef __cplusplus
}
#endif |
|
|
Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Sun Apr 20, 2008 12:30 pm Post subject: |
|
|
oib111, sorry to bump this but... give your source! http://blog.empas.com/navylkh/read.html?a=1707462
I was just searching some of that in order to learn from it and boom. I found this.
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Apr 20, 2008 12:37 pm Post subject: |
|
|
Lol. I did. I didn't give the link because it's already in this thread. I referred to it as the code project link. I'm pretty sure the first post in this thread. But w/e...http://www.codeproject.com/KB/DLL/loadingdll.aspx
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Sun Apr 20, 2008 12:41 pm Post subject: |
|
|
Sorry, sorry, sorry. Your completely right. My full apologies, I didn't read it properly, and now feel like an idiot as a result.
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Apr 20, 2008 12:48 pm Post subject: |
|
|
Haha. It's ok dude.
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Sun Apr 20, 2008 12:53 pm Post subject: |
|
|
Thanks
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Apr 20, 2008 12:55 pm Post subject: |
|
|
np
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
|