3
votes

Hello I'm trying to create a take a program path and put in in a registry file, but I keep on having an error. Here is the code:

#include <iostream> 
#include <windows.h> 
#include <winuser.h> 
#include <tchar.h>
#include <limits>

using namespace std;

void reg() {
char buffer[MAX_PATH];

GetModuleFileName(NULL,buffer,sizeof(buffer));

const unsigned char Path[ MAX_PATH ] = {buffer};

::HKEY Handle_Key = 0;

::RegSetValueEx( Handle_Key, "My Directory", 0, 1, Path, sizeof Path  );
};

The error I'm getting says

invalid conversion from 'char*' to 'unsigned char' [-fpermissive]

I have spent hours looking for a solution, but I can't find one.

1

1 Answers

2
votes

The problem, I'm guessing, is this line

const unsigned char Path[ MAX_PATH ] = {buffer};

The problem here is that you try to create an array of single characters with a character pointer.

You only use that variable as a temporary for the RegSetValueEx call, so you don't really need it. Instead call that function with buffer directly.

Also, you should not use sizeof here, since that will put all of the buffer in the registry, and not only the actual string. Use strlen.

Like:

::RegSetValueEx( Handle_Key, "My Directory", 0, 1,
                 reinterpret_cast<unsigned char*>(buffer),
                 strlen(buffer));