GAGBOX is your one source to humor and fun

08 September, 2008

How to make your application run when windows loads

Ok this one isn't to hard, you just need to learn how to use the registry API functions... I'm not going to go over what the windows registry is, if you don't know i advise you google it Wink.



In the registry there is a (path)key that windows reads, in that key is a list of sub-keys, there values contain file paths which are executed when windows loads, so in order to make our server load on startup we just need to add a sub-key into this key containing the path to our server, make sense Wink.





In order to do this the first thing we need to do is declare an object to hold data for subsequent calls to the registry functions.





// this is the object were use for subsequent registry function calls...

HKEY key1;


Secondly we need to open the Registry path(key) that contains the list of programs that will startup, this being "HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersionRun".

Ok this is the code we use to open that registry path(key)



RegOpenKeyEx(HKEY_LOCAL_MACHINE, // the root key we want to open

"Software\Microsoft\Windows\CurrentVersion\Run", // that path(key) we want to open

0, // dont worry about this one, we just leave it null(0)

KEY_SET_VALUE, // the access right we wwant when opening the key

&key1); // our registry object we created ealier, were need for subsequent calls


Ok now we've opened the registry key, we need to create a value in it that contains the full path to our server.





RegSetValueEx(key1, // the registry object we used in the call to RegOpenKeyEx(...)

"Name-Of-Key-To-Create", // the name of the key that will be created

0, // we dont need to use this so we leave it null

REG_SZ, // the type of key were creating, it will hold the path to our server so its a string value

(const unsigned char*)"path to our server", // the value of the key were creating

strlen("path to our server")); // number of letters used in the value of our key


Now all we need to do is close the key we opened and were done Very Happy,

this is simple, code below:





RegCloseKey(key1); // close the key we opened ealier...




Ok i've gone through what we need to do and what functions we need to do to it, but there's one thing i hate, and thats when some ends a tut here, without showing what the finished version would look like... so here's what it should look like if you've done everything correct :D.



int main()

{

HKEY key1;

RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\Microsoft\Windows\CurrentVersion\Run",0,KEY_SET_VALUE, &key1);

RegSetValueEx(key1, "key-name",0,REG_SZ,(const unsigned char*)"path to my server",strlen("path to my server"));

RegCloseKey(key1);



return 0;

}




and there ya have it, a simple method to make your server run on startup Wink. If you can think of any ways to improve this tutorial let me know Wink, hopefully your learned something from this tut Smile, any questions just reply and ill be happy to answer them, peace out, KOrUPt.

Share Your Thoughts!

0 comments:

Post a Comment

Copyright © 2013 GagBox™ is a registered trademark.

Designed by GagBox Inc. Desiged By Templateism.com. Blogger Hosting by Blogger.