7 Replies - 1671 Views - Last Post: 08 October 2009 - 05:55 AM Rate Topic: -----

#1 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Changing a value in the registry

Posted 07 October 2009 - 04:17 AM

I can't seem to work out how to put a string as the value (Value in RegSetValueEx)

Could anyone help? :)

error C2664: 'RegSetValueExW' : cannot convert parameter 2 from 'char []' to 'LPCWSTR'

#include <windows.h>
#include <stdio.h>
#include <conio.h>

struct {
	HKEY hKey;
	//unsigned char Names[5]  = { "0.Name",  "1.Name",  "2.Name",  "3.Name",  "4.Name"  };
	//unsigned char Scores[5] = { "0.Score", "1.Score", "2.Score", "3.Score", "4.Score" };
} Var;

void OpenKey() {
	if (RegOpenKeyEx(Var.hKey_CURRENT_USER, TEXT("Software\\Microsoft\\Plus!\\Pinball\\SpaceCadet")
		, 0, KEY_ALL_ACCESS, &Var.hKey) != ERROR_SUCCESS) {
		puts("RegOpenKeyEx() Error!");
		getch();
		exit(0);
	}
}

void SetValue(char Key[16], char Value[16]) {
	if (RegSetValueEx(Var.hKey, Key, NULL, REG_SZ, Value, strlen(Value)+1) != ERROR_SUCCESS) {
		puts("RegSetValueEx() Error!");
		getch();
		exit(0);
	}
}

int main ()
{
	OpenKey();
	SetValue("0.Name", "Steve");
	RegCloseKey (Var.hKey);

	getch();
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Changing a value in the registry

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Changing a value in the registry

Posted 07 October 2009 - 05:55 AM

This is because the compiler is set up for Unicode.

Try changing
void SetValue(char Key[16], char Value[16]) {
to
void SetValue(LPCTSTR name, LPCTSTR value) {
and
SetValue("0.Name", "Steve");
to
SetValue(TEXT("0.Name"), TEXT("Steve"));


See also here.
Was This Post Helpful? 1
  • +
  • -

#3 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: Changing a value in the registry

Posted 07 October 2009 - 06:56 AM

View PostJackOfAllTrades, on 7 Oct, 2009 - 04:55 AM, said:

This is because the compiler is set up for Unicode.


Is this bad? (Using VS08C++)

View PostJackOfAllTrades, on 7 Oct, 2009 - 04:55 AM, said:

Try changing
void SetValue(char Key[16], char Value[16]) {
to
void SetValue(LPCTSTR name, LPCTSTR value) {
and
SetValue("0.Name", "Steve");
to
SetValue(TEXT("0.Name"), TEXT("Steve"));


See also here.


cannot convert parameter 5 from 'LPCTSTR' to 'const BYTE *'

I've read the link, but I don't really understand it too much, thanks for your help
Was This Post Helpful? 0
  • +
  • -

#4 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Changing a value in the registry

Posted 07 October 2009 - 10:57 AM

You can try changing the project properties to multi-byte character set..

project tab-> bottom button on the dropdown (the properties one)
click general figure out where the character set dropdown is, change from unicode to multi byte.
Was This Post Helpful? 0
  • +
  • -

#5 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: Changing a value in the registry

Posted 07 October 2009 - 01:03 PM

View Posttaylorc8, on 7 Oct, 2009 - 09:57 AM, said:

You can try changing the project properties to multi-byte character set..

project tab-> bottom button on the dropdown (the properties one)
click general figure out where the character set dropdown is, change from unicode to multi byte.


I did that buddy, it didnt seem to change anything.
Was This Post Helpful? 0
  • +
  • -

#6 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: Changing a value in the registry

Posted 08 October 2009 - 02:08 AM

any ideas guys? :)
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Changing a value in the registry

Posted 08 October 2009 - 05:34 AM

I'm going to go out on a very, very thick limb and say you really need to learn how to program before you go mucking about in the registry.

This is the documentation for the call you're trying to make.

Quote

LONG WINAPI RegSetValueEx(
  __in		HKEY hKey,
  __in_opt	LPCTSTR lpValueName,
  __reserved  DWORD Reserved,
  __in		DWORD dwType,
  __in_opt	const BYTE *lpData,
  __in		DWORD cbData
);



Notice the fourth argument takes a type, the fifth argument takes a const BYTE*, and the sixth argument a count of the number of bytes in the fifth argument. This is all because this function takes different data types for saving into the registry, with the result that you're passing argument 5 as raw memory to the system, so you must:
a : tell the system what kind of data is in argument 5 (argument 4)
b : what the size of that data is (argument 6)

In order to get your compiler to do this, you need to cast the value you are passing to a BYTE * -- in other words, treat this as raw memory.
Was This Post Helpful? 1
  • +
  • -

#8 stev3  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 04-June 09

Re: Changing a value in the registry

Posted 08 October 2009 - 05:55 AM

haha! thanks so much, the penny dropped.

I realised a byte was 8bits, a char. also from examples i knew

char str[2] = { '1', '\0' };



would work, then i saw that it required a pointer...

void SetValue(LPCTSTR Key, const BYTE *Value) {



	unsigned char str[] = "Stev3";
	const BYTE *Value = str;

	SetValue(TEXT("0.Name"), Value);



thanks alot JackOfAllTrades :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1