Thread overview
Startup from registry program doesn't work
Nov 26, 2009
Sam Hu
Nov 26, 2009
Richard Webb
Dec 01, 2009
Sam Hu
Dec 01, 2009
Richard Webb
Dec 02, 2009
Sam Hu
November 26, 2009
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;
}
November 26, 2009
Do you need to be calling RegOpenKeyEx with KEY_WRITE instead of KEY_READ ?

December 01, 2009
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?

December 01, 2009
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.

December 02, 2009
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!