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");


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...

Name | Type | Data
filenamehere | REG_SZ | chinese characters <-- WHY?

Edited by Jiwe: n/a

8 Years
Discussion Span
Last Post by Jiwe


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.

Edited by Ancient Dragon: n/a

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");

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.

Edited by Jiwe: n/a


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*

Edited by Ancient Dragon: n/a


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.

Edited by Jiwe: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.