Computing.Net > Forums > Programming > Adding programs to Startup

Adding programs to Startup

Reply to Message Icon

Original Message
Name: livingdecay
Date: May 22, 2005 at 01:52:25 Pacific
Subject: Adding programs to Startup
OS: Windows XP Home
CPU/Ram: Athlon 64 S939 3000+ / 10
Comment:

How is it possible to cause a program to create entries to run itself on startup? I know how and where to put the registry entries, and I know how to retrieve the path of the application, but I cant seem to get the two working together. The problem is that the path obtained with GetModuleFileName() is not a type compatible with RegSetValueEx(), and does not want to be converted to a type that is.
Is there any way around this, or is there another way to do this?

AMD Athlon 64 S939 3000+
ASUS A8V Deluxe WE MoBo
1024Mb vData DDR400-Dual Channel
80Gb ATA133 HDD
Leadtek Geforce 6800 128Mb
ThermalTake 360W PurePower PSU



Report Offensive Message For Removal


Response Number 1
Name: StuartS
Date: May 22, 2005 at 04:20:41 Pacific
Subject: Adding programs to Startup
Reply: (edit)

Explain! GetModuleFileName returns a string. RegSetValueEx() excepts a string. So what data type are we talking about.

You may have to do a bit of cleaning up of the returned string, get rid of excess Nulls, but it is still a string.

Stuart


Report Offensive Follow Up For Removal

Response Number 2
Name: livingdecay
Date: May 28, 2005 at 00:40:28 Pacific
Subject: Adding programs to Startup
Reply: (edit)

GetModuleFileName() returns a DWORD, RegSetValueEx() requires the lpData argument (that receives the data to be written to the registry) to be a CONST BYTE.
There does not seem to be any conversion between these two types - even reinterpret_cast doesn't work.

AMD Athlon 64 S939 3000+
ASUS A8V Deluxe WE MoBo
1024Mb vData DDR400-Dual Channel
80Gb ATA133 HDD
Leadtek Geforce 6800 128Mb
ThermalTake 360W PurePower PSU



Report Offensive Follow Up For Removal

Response Number 3
Name: livingdecay
Date: May 28, 2005 at 00:50:37 Pacific
Subject: Adding programs to Startup
Reply: (edit)

A thought occurs...
Is there a function that allows one to create shortcuts? If so, I could add a shortcut to the program to the Start Menu\Startup folder. Maybe this function wouldn't have such type conversion problems.

AMD Athlon 64 S939 3000+
ASUS A8V Deluxe WE MoBo
1024Mb vData DDR400-Dual Channel
80Gb ATA133 HDD
Leadtek Geforce 6800 128Mb
ThermalTake 360W PurePower PSU



Report Offensive Follow Up For Removal

Response Number 4
Name: StuartS
Date: May 28, 2005 at 04:19:44 Pacific
Subject: Adding programs to Startup
Reply: (edit)

>> GetModuleFileName() returns a DWORD <<

GetModuleFileName *does* return a string. It can be anything else. A filename is a string. Look at this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getmodulefilename.asp

The filename is returned in LPTSTR.

With RegSetValueEx I think you are misreading the instructions. lpData is just a pointer to a buffer containing the data terminated by a Null character. That Data can be anything. Its the dwType parameter that tell the API what to expect.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/regsetvalueex.asp

If you were using VB I could tell you how to create an item in the Start Menu\Startup folder but the API is incorporated in a DLL that comes with VB, it would be a waste of time unless you are using VB. If you are using anything else there is certainly a way to do it. Start looking for shell functions.

Stuart


Report Offensive Follow Up For Removal

Response Number 5
Name: livingdecay
Date: May 29, 2005 at 03:52:13 Pacific
Subject: Adding programs to Startup
Reply: (edit)

My problem is that I cannot find a way of converting between the LPTSTR stored in GetModuleFileName()'s lpFileName argument, and the CONST BYTE required for RegSetValueEx()'s lpData argument. Sorry about the DWORD thing, I havent worked on this problem in a while and I forgot what exactly my problem was.
Now, please, please help me, I'm getting sick of Error C2664.

AMD Athlon 64 S939 3000+
ASUS A8V Deluxe WE MoBo
1024Mb vData DDR400-Dual Channel
80Gb ATA133 HDD
Leadtek Geforce 6800 128Mb
ThermalTake 360W PurePower PSU



Report Offensive Follow Up For Removal


Response Number 6
Name: StuartS
Date: May 29, 2005 at 05:16:19 Pacific
Subject: Adding programs to Startup
Reply: (edit)

There is no conversion required. How you pass LPTSTR depends on which programming language you are using. Once you have the string value into a string variable the rest is easy.

With C++ you prefix the variable with an asterisk. This tell the function not to pass the string but the address of the string, a DWORD value. A DWORD is 32 bits or four bytes and can have a value of over 4 billion. A string can be up to 64Kbs long.

With VB you just enter the string as VB passes values by reference by default. In VB if you really do want to pass a value and not a reference to the value then you have to enclose the variable in []. This is rare in API calls but they do exist.

In all cases it a DWORD value that is passed which is the address of the value being passed. dwType tells the function what type of data it is so it can behave accordingly.

Passing and returning values to and from an API function is something you need to read up on. Most pass values by reference, some require a value and other require pointers to structures.


Stuart


Report Offensive Follow Up For Removal

Response Number 7
Name: livingdecay
Date: May 31, 2005 at 10:15:47 Pacific
Subject: Adding programs to Startup
Reply: (edit)

I know all of that.
I'm using VC++ 6, and declaring or passing the variable as a pointer does not seem to make any difference to the conversion needed. For instance, if I declare AppPath as a LPTSTR, it is essentially a char*, or a pointer to a char. Now, if I want to pass AppPath to GetModuleFileName, i.e.

GetModuleFileName(NULL,AppPath,NULL);

then it has no problems, but when I try to pass AppPath to RegSetValueEx, i.e.

RegSetValueEx(KeyStart, "An App",0,REG_SZ, AppPath, strlen(AppPath));

I get the error message:

'error C2664: 'RegSetValueExA' : cannot convert parameter 5 from 'char *' to 'const unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast'

An attempt to reinterpret_cast AppPath to <const unsigned char*> results in a run time error.
Now, please carify whatever I'm not understanding exactly, cause I really want to get this working.
Thanks for all previous advice though, hopefully this will be the last time you will have to post.



Report Offensive Follow Up For Removal

Response Number 8
Name: StuartS
Date: May 31, 2005 at 11:38:42 Pacific
Subject: Adding programs to Startup
Reply: (edit)

This API function cannot accept strings which is what you are doing. The need a pointer to a string.

Try this:

RegSetValueEx(KeyStart, "An App",0,REG_SZ, &AppPath, strlen(AppPath)+1);

Its a long time since I did any C++ programming but I do remember that C++ passes reference by value by default which is what you are doing. The API can't use a string value, it needs the address in memory where the string is stored.

You also need add a character to the strlen as the length also needs to include the terminating null character which strlen ignores.

Look at this example.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/adding_a_source_to_the_registry.asp

If this doesn't work you need to re-post and hopefully somebody with a better knowledge of C++ will be able to help you.

Stuart


Report Offensive Follow Up For Removal

Response Number 9
Name: livingdecay
Date: June 5, 2005 at 07:50:57 Pacific
Subject: Adding programs to Startup
Reply: (edit)

AHA!
Thanks you so much. I now understand what I was doing wrong. I was declaring the variable as a BYTE*, and passing it without an ampersand - I declared it as a BYTE and passed it as a pointer (&AppPath) and now it is working perfectly.
Again, thank you, and sorry for all the trouble over so stupid a mistake.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Adding programs to Startup

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge