www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Startup from registry program doesn't work

reply Sam Hu <samhu.samhu nospam.com> writes:
Greetings!

Below program is purpose to execute a program(exe) from registry upon system
startup.It was built successfuly,but unfortunately it does not work as
expected,nothing would happen.It prints:

"Oh..no,not a good doctor."

when the program runs.

What's problem?It is under DMD2.036 under windows xp.

Thanks for your help in advance.


import *.*;//everything needed

BOOL StartupThruRegistry(char* pFileName)
{
	DWORD rc;

	DWORD length=pFileName.sizeof;
	DWORD type=REG_SZ;
	HKEY hKey;

	rc=RegOpenKeyEx(HKEY_CURRENT_USER,
		"Software\\Microsoft\\Windows\\CurrentVersion\\Run",
		0,KEY_READ,&hKey);
	if(rc==ERROR_SUCCESS)
	{
		rc=RegSetValueEx(hKey,toStringz("Startup"),0,
				type,cast(ubyte*)pFileName,length);
		RegCloseKey(hKey);
	}
	if(rc==ERROR_SUCCESS)
		return TRUE;
	else
		return FALSE;
}

int main(string[] args)
{
	
	string filename="c:\\windows\\explorer.exe";

	if(!StartupThruRegistry(cast(char*)toStringz(filename)))
	{
		writeln("Oh..no,not a good doctor.");
	
	}
	return 0;
}
Nov 26 2009
parent reply Richard Webb <numpsyuk hotmail.com> writes:
Do you need to be calling RegOpenKeyEx with KEY_WRITE instead of KEY_READ ?
Nov 26 2009
parent reply Sam Hu <samhu.samhu nospam.com> writes:
Richard Webb Wrote:

 Do you need to be calling RegOpenKeyEx with KEY_WRITE instead of KEY_READ ?
 
Thanks,I changed to KEY_WRITE and the function now return true when the app executes.However the specified app dosn't start up automatically,when I check the startup settings by some third party software(say,SpySpot-Search and Destroy),I found the setting is : C:\\w other than c:\\windows:\\explorer.exe So where is the problem?
Nov 30 2009
parent reply Richard Webb <numpsyuk hotmail.com> writes:
Is the 

DWORD length=pFileName.sizeof;

setting length to the size of the pointer, when you need the length of the
string? That would explain why only 4 bytes have been written.
Dec 01 2009
parent Sam Hu <samhu.samhu nospam.com> writes:
Richard Webb Wrote:

 Is the 
 
 DWORD length=pFileName.sizeof;
 
 setting length to the size of the pointer, when you need the length of the
string? That would explain why only 4 bytes have been written.
 
Solved.Thank you so much for your help!
Dec 01 2009