Follow us on Twitter!
Your life is ending one minute at a time. If you were to die tomorrow, what would you do today?
Saturday, April 19, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 34
Guests Online: 32
Members Online: 2

Registered Members: 82838
Newest Member: w1zarrd
Latest Articles

(C++)(Dev-C++) DLLs

Arrow Image How to make a DLL and load functions from it.



    Hello, this is my first article. Well, it is my second, but I decided not to publish the other one. In this one I will try and describe how to make a DLL and use the functions you put into it, in your application, this is successfully compiled with Dev-C++, I don\'t know if it will work with any others.

    The first thing you need to do is make the DLL. To do this, make a new project and change it so that it compiles as a DLL (Project -> Project Options -> Win32 DLL -> ok). We will call it \"My_DLL\", we will try and get a function of \"void HelloWorld()\" from it.

    Add this to a blank file in the project, and save it as *.cpp


    ((¯`·._*. Code .*_.·´¯))
<code>
/* Headers */
#include &lt;windows.h&gt;
#include &lt;iostream&gt;

/* Namespaces */
using namespace std;

/* define \'DLL\' so we can use it to set functions to be exported */
#define DLL extern \"C\" __declspec(dllexport)

/* Make the function */
DLL&nbsp;&nbsp;void HelloWorld()
{
&nbsp;&nbsp;&nbsp;&nbsp; /* you will see the text \'Hello, World!\' if the function successfully executes */

&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; \"\\nHello, World!\\n\";
}
</code>
&nbsp;&nbsp;&nbsp;&nbsp;((¯`·._*. End of Code .*_.·´¯))


what it does is creates a function in the DLL file (when you compile it) that you can access with a application.

&nbsp;&nbsp;&nbsp;&nbsp;The next thing you would want to do is create another C++ project, this time choose a console application, instead of DLL. Add the following code to a blank file in it, and save it as *.cpp.


&nbsp;&nbsp;&nbsp;&nbsp;((¯`·._*. Code .*_.·´¯))
<code>
/* Headers */
#include &lt;windows.h&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;

/* Namespaces */
using namespace std;

/* change the part in the last set of parenthesis to match whatever parameters the function your importing had…for instance if it was: HelloWorld(char*test, int a), you would use: typedef void (WINAPI*Func)(char*,int);&nbsp;&nbsp;the \'Func\' part can be changed to whatever you like, and you also would change the beginning \'void\' to whatever the function is, if it were a \'int\' type, then you just change that to \'int\' */
typedef void (WINAPI*Func)();

/* this is what is going to be holding our function, I like to make it the same name as the function we are importing, but you can make it whatever you like. */
Func HelloWorld;

int main()
{
&nbsp;&nbsp;&nbsp;&nbsp;/* this is what we will use to load the DLL, and DLL will be our handle to that DLL. Remember earlier, I said that we would call the DLL \'My_DLL\', I just added a .dll to the end, because when we compiled it, it made a .dll file. */
&nbsp;&nbsp;&nbsp;&nbsp;HINSTANCE DLL = LoadLibrary(\"My_DLL.dll\");

/* check for error on loading the DLL */
&nbsp;&nbsp;&nbsp;&nbsp;if(DLL==NULL)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; \"We were unable to load: \" &lt;&lt; DLL &lt;&lt; \"\\n\";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getch();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(0);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;/* this is where we set the function of \'HelloWorld()\' from \'My_DLL.dll\' into \'HelloWorld\' */
&nbsp;&nbsp;&nbsp;&nbsp;HelloWorld=(Func)GetProcAddress((HMODULE)DLL, \"HelloWorld\");

&nbsp;&nbsp;&nbsp;&nbsp;/* check for error on getting the function */
&nbsp;&nbsp;&nbsp;&nbsp;if(HelloWorld==NULL)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt; \"Unable to load function(s).\\n\";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FreeLibrary((HMODULE)DLL);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getch();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;/* Now that we have the DLL loaded, and the function set to HelloWorld, we can just use that as a function while the DLL is loaded. If it were a HelloWorld(int), then we would just add a int into it (we would also have to change the typedef to add a int into it) */
&nbsp;&nbsp;&nbsp;&nbsp;HelloWorld();

&nbsp;&nbsp;&nbsp;&nbsp;/* now that we are done with it, we will release. */
&nbsp;&nbsp;&nbsp;&nbsp;FreeLibrary((HMODULE)DLL);
}
</code>
&nbsp;&nbsp;&nbsp;&nbsp;((¯`·._*. End of Code .*_.·´¯))


&nbsp;&nbsp;&nbsp;&nbsp;I hope this article helps out anyone who is curious about writing and getting functions from their own DLLs (you can also do the same thing to get functions from other DLLs.) I don’t know how to withdraw classes and stuff from DLLs, only functions.

&nbsp;&nbsp;&nbsp;&nbsp;Well, I guess that’s it for my article…happy coding.

Comments

Adeilon November 16 2005 - 22:21:22
I used conio.h because I don’t know any of the standard C++ headers (no .h) that has getch(), and I used windows.h because I don’t know of any header files that define the windows functions without the C header style. I don’t know if they have any updated headers in Visual, Borland, etc. I just have Dev-C++ hehe. Also, I would have put the code into [code][/code] tags, but it didn’t seem to accept BBCode for the article. I would have changed the font color of all the stuff, so it looked like it does in a compiler’s rich edit text, but it seemed like too much extra work hehe.
i2l84u00x00on March 12 2007 - 12:06:06
yea everything is corrputed buddy
PoorHackeron February 27 2008 - 12:40:52
alex good job Grin
Post Comment

Sorry.

You must have completed the challenge Basic 1 and have 100 points or more, to be able to post.