I made a function SetRegistry() to write a string in the registry like this:

void SetRegistry()
{
          HKEY hKey;

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

          RegSetValueEx(hKey, TEXT("filenamehere"),0,REG_SZ,(LPBYTE) "C:\\Windows\\filenamehere.exe",sizeof("C:\\Windows\\filenamehere.exe");

          RegCloseKey(hKey);
}

NOTE: C:\\Windows\\ is the path where the .exe file I want to add is.

it sets the string with the filename and type correctly but the data that should be C:\\Windows\\filenamehere.exe appears like some chinese weird characters.
I want to know why it writes weird characters and how can I solve it so it saves the right path...

EXAMPLE HOW IT APPEARS:
Name | Type | Data
filenamehere | REG_SZ | chinese characters <-- WHY?

>>sizeof("C:\\Windows\\filenamehere.exe");

That will not produce the value you want -- the sizeof(any pointer here) is the same for all pointers (most likely 4). It would be easier to just declare a variable to hold that string so that you don't have it in your program twice. char filename[] = "C:\\Windows\\filenamehere.exe"; With that you can use either sizeof() or strlen() because variable filename is not a pointer.


Your function will not work at all under Windows 7. I get "Access Deined" error on RegOpenKeyEx function. There is obviously a permissions problem somewhere that needs to be fixed even though I am logged in with Administrator rights. I've seen similar problem elsewhere in this os.

char filename[] = "C:\\Windows\\filenamehere.exe";
RegSetValueEx(hKey,TEXT("filenamehere"),0,REG_SZ,(LPBYTE) filename,sizeof(filename));

I did that but in the registry it still saves the data as chinese weird characters. I tried to only save 1 character and it works fine but with more then 1 it turns chinese.

Are you compiling for UNICODE? If you are, then try making filename a TCHAR instead of char*

TCHAR filename[] = TCHAR("C:\\Windows\\filenamehere.exe");
RegSetValueEx(hKey,TEXT("filenamehere"),0,REG_SZ,(LPBYTE)filename,sizeof(filename)/sizeof(TCHAR));

It compiles but with the TCHAR it doesn't even create the string in the registry. How do I check if it's compiling for UNICODE or not? I'm using Visual Studio 2008.

To see if unicode, goto menu Projuect --> Properties --> expand the Configuration Properties tab, select General. Now check the value of "Character Set" -- if its set to "Use Unicode Character Set" then you are compiling for UNICODE. You can turn that off by changing it to "Not Set". When you do that then TCHAR does nothing and all strings will become char* instead of wchar_t*

PROBLEM SOLVED! THX Ancient Dragon for your help.

It didn't work like this:

TCHAR path[] = TCHAR("C:\\Windows\\filename.exe");

So I tried some stuff and it worked like this:

WCHAR path[40] = TEXT("C:\\Windows\\filename.exe");

with path[20] it would just write C:\Windows\ so I added 20 more.

I don't really understand why this works like this so if you could explain I would be really thankfull. Anyways problem solved THX!


EDIT: Btw I checked if it's compiling for UNICODE and yes it is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.