Registry Access (Write Access)

N

Newbie Coder

MFC Application VC++.NET 2003

I have a certain registry key (HKCU\Software\MyKey) that contains between 30
& 64 string values

I need to write a '*' to all those 30 - 64 string values under that
particular key.

Example:

HKCU\Software\MyKey

Key name Value

MyKey1 http://www.somesite.com
MyKey2 http://www.someothersite.com

Becomes:

MyKey1 *
MyKey2 *
....

Any ideas?

Have searched The Code Project & cannot make head not tail of these classes
as I am just starting back into C++ after many years

TIA
 
N

Newbie Coder

Bian,

First of all thank you for your reply

The page says Framework 2 specific. I don't want to use the framework
whatsoever

Any other suggestions?
 
B

Brian Muth

Newbie Coder said:
Bian,

First of all thank you for your reply

The page says Framework 2 specific. I don't want to use the framework
whatsoever

Any other suggestions?

Using Visual Studio .NET 2003, create the C++ project of your choice, taking
care to check the "support for ATL" checkbox. You can use CRegKey just fine.
It does not depend on .NET Framework 1.1. or 2.0.

Brian.
 
N

Newbie Coder

Brian,

In my original post it said MFC application

Why should I re-write many thousand lines of code in order to your
suggestion?

I was hoping that someone would write me a function in order to do this

How did you gain your MVP status?

In my original question I asked for MFC C++.NET 2003 app & you gave me a
link to 2005 (Framework 2) when I am using 1.1
Now, you ask me to totally change my project just so your link works - you
are joking of course.

Sorry, but your replies aren't useful at all!!
 
N

Newbie Coder

David,

You have an attitude

If someone asked me how to do it in another language then I'd be the first
one to write the function & paste the code to the user, but that's the
difference between you & I.

There are many so-called MVP who cannot write a line of code if their life
depended on it. Herfried is a perfect example of that.

Back to my original question:

If I search The Code Project I get many, many classes, but I need one
function. Plus, I am starting out back into C++ since I stopped coding in it
around 2001/2002.
 
D

David Wilkinson

Newbie said:
Brian,

In my original post it said MFC application

Why should I re-write many thousand lines of code in order to your
suggestion?

I was hoping that someone would write me a function in order to do this

How did you gain your MVP status?

In my original question I asked for MFC C++.NET 2003 app & you gave me a
link to 2005 (Framework 2) when I am using 1.1
Now, you ask me to totally change my project just so your link works - you
are joking of course.

Sorry, but your replies aren't useful at all!!

Newbie:

What is your problem? Brian told you about CRegKey. You have VS2003.
Look it up in the help. It's there. It's in VC6 also.

David Wilkinson
 
B

Brian Muth

In my original post it said MFC application

My solution applies to MFC applications.
Why should I re-write many thousand lines of code in order to your
suggestion?

Why do you believe you need to rewrite any code at all?
I was hoping that someone would write me a function in order to do this

How did you gain your MVP status?

In my original question I asked for MFC C++.NET 2003 app & you gave me a
link to 2005 (Framework 2) when I am using 1.1

On the reference I gave you, there is a link to the Microsoft Visual Studio
2003/.NET Framework 1.1 version of the document. So save you from the extra
effort on clicking on that link, here it is for you:

http://msdn2.microsoft.com/en-us/library/xka57xy4(vs.71).aspx

You will find that the information is the same for both environments.

The information on this page is valuable and gives clear instructions on how
to use CRegKey. If you don't want to experiment with the fresh solution I
suggested in my previous post, go directly to your MFC project and insert

#include "atlbase.h"

at the top of your code after any other #include statements. You can now use
CRegKey to your heart's delight.
Now, you ask me to totally change my project just so your link works - you
are joking of course.

The changes to your code are minimal and trivial.
Sorry, but your replies aren't useful at all!!

Then please re-post your questions if you are confused. What has not been
answered?.

Brian
 
B

Ben Voigt

Newbie Coder said:

Answer below. But first I address your statements.
You have an attitude

But yours is worse. Someone took the time to try to help you. If you were
paying for the assistance you would have a reason to complain.
If someone asked me how to do it in another language then I'd be the first
one to write the function & paste the code to the user, but that's the
difference between you & I.

There are many so-called MVP who cannot write a line of code if their life
depended on it. Herfried is a perfect example of that.

Asking you to rewrite your project with a different toolkit is not really
much different from demanding that someone else learn the MFC you've chosen.
Except that it's your problem, and anyone helping you is on a volunteer
basis.
Back to my original question:

If I search The Code Project I get many, many classes, but I need one
function. Plus, I am starting out back into C++ since I stopped coding in
it
around 2001/2002.

No, you need three functions:

RegOpenKeyEx
RegSetValueEx
RegCloseKey

All provided by windows itself, with no extra runtime library dependencies.
MFC should already be including windows.h for you, just make sure you link
with Advapi32.lib.
 
N

Newbie Coder

Ben,

Thanks for the three functions. These are the same as I used to use back in
the VB 6 days.

I wanted to write in a Win32 application with a dialog box

If someone asked this question for VB.NET then I would code them a complete
reply. In this case a re-usable function like so:

Private Sub RemoveSomeValue(ByVal sValue As String)
Dim strKey As String = "Software\MyKey"
Dim reg As RegistryKey = Registry.CurrentUser.OpenSubKey(strKey,
True)
Try
reg.SetValue(sValue, "*")
reg.Close()
reg = Nothing
Catch ex As Exception
End Try
End Sub

I wouldn't say look at 'RegistryKey'. How lame is that & what help is it to
the OP? No help because they probably knew that much already

======================================

I got a registry class from The Code Project, stripped out all the functions
I didn't need & are left with the one function I want. However, I add the
header to the MFC form & when I try to call the function in that class I get
a compiler error (C2352 Illegal Call To Non Static Member). God knows what
it means. Checked MSDN
(http://msdn2.microsoft.com/en-us/library/2x426hte(vs.71).aspx) & all it
says is to comment it out. What use is that?

So, if you got a total newbie asking the same question as I did & you get a
useless answer like Brian wrote then its absolutely no use whatsoever, is
it? No. Why? Because they don't have a clue to begin with & what I've found
on these programming newsgroups is that if someone answers the thread others
see that its been answered & leave the post alone. Thefore if you get a
useless, no help whatsoever, ridiculous reply like I got originally by Brian
then you have to ask the question again & again & again until it does get
answered correctly.

For C++ I used to ask questions on the GotDotNet website, but its now
closed. Used to get good results... & detailed answers too.

There are so, so, so many MVP's who give out rubbish in order to get another
post listed. Does MVP status mean you can use Google & you cannot write
code? It seems that way to me from what I have seen by using these
newsgroups for a number of years.

So, Ben. Can you see now why I answered Brian like I did?

Header file (RegisterEx.h):

#pragma once
#include "stdafx.h"

class CRegisterEx

{

public:

CRegisterEx(CString path);

~CRegisterEx(void);

public:

void WriteString(CString str, CString subPath = "", CString Key = "");

};

CPP Filr (RegisterEx.cpp)

#include "StdAfx.h"

#include "registerex.h"

#include <stdlib.h>

#pragma warning ( disable : 4267 )

#define MAX_BUFFER 2048

char buffer[MAX_BUFFER];

CString pt;

CRegisterEx::CRegisterEx(CString path)

{

pt = path;

}

CRegisterEx::~CRegisterEx(void)

{

}

// Writing strings to the register.

void CRegisterEx::WriteString(CString str, CString subPath, CString Key)

{

HKEY hk;

TCHAR szBuf[2048];

CString insidePath = pt;

if (Key)

{

insidePath = insidePath + "\\" + subPath;

}

if (RegCreateKey(HKEY_CURRENT_USER, _T(insidePath), &hk))

{

// Woops, you don't have privileges

TRACE0("Could not create the registry key.\n\nDo you have the right
privileges?\n");

}

strcpy(szBuf, str);

if (RegSetValueEx(hk, _T(Key), 0, REG_EXPAND_SZ, (LPBYTE)szBuf, strlen(str)
+ 1))

{

// Hmm, you did something wrong

TRACE0("Could not set the given String.\n\nDo you have the right
privileges?\n");

}


RegCloseKey(hk);

}


--
Newbie Coder
(It's just a name)








Ben Voigt said:
Newbie Coder said:

Answer below. But first I address your statements.
You have an attitude

But yours is worse. Someone took the time to try to help you. If you were
paying for the assistance you would have a reason to complain.
If someone asked me how to do it in another language then I'd be the first
one to write the function & paste the code to the user, but that's the
difference between you & I.

There are many so-called MVP who cannot write a line of code if their life
depended on it. Herfried is a perfect example of that.

Asking you to rewrite your project with a different toolkit is not really
much different from demanding that someone else learn the MFC you've chosen.
Except that it's your problem, and anyone helping you is on a volunteer
basis.
Back to my original question:

If I search The Code Project I get many, many classes, but I need one
function. Plus, I am starting out back into C++ since I stopped coding in
it
around 2001/2002.

No, you need three functions:

RegOpenKeyEx
RegSetValueEx
RegCloseKey

All provided by windows itself, with no extra runtime library dependencies.
MFC should already be including windows.h for you, just make sure you link
with Advapi32.lib.
 
B

Ben Voigt

Newbie Coder said:
Ben,

Thanks for the three functions. These are the same as I used to use back
in
the VB 6 days.

I wanted to write in a Win32 application with a dialog box

If someone asked this question for VB.NET then I would code them a
complete
reply. In this case a re-usable function like so:

Private Sub RemoveSomeValue(ByVal sValue As String)
Dim strKey As String = "Software\MyKey"
Dim reg As RegistryKey = Registry.CurrentUser.OpenSubKey(strKey,
True)
Try
reg.SetValue(sValue, "*")
reg.Close()
reg = Nothing
Catch ex As Exception
End Try
End Sub

I wouldn't say look at 'RegistryKey'. How lame is that & what help is it
to
the OP? No help because they probably knew that much already

Plenty of help. It tells you what to look for on MSDN, which has lots of
examples
http://msdn2.microsoft.com/en-us/library/microsoft.win32.registrykey.aspx
If you can't understand the example on MSDN, you should say that, not that
you want to do this and don't know where to start.

Here is the documentation for RegSetValueEx:
http://msdn2.microsoft.com/en-us/library/ms724923.aspx

On that page you will find a section called "Example Code" with a link.
That link is a complete example including error handling. Were you not able
to find it?

Sure, saying look at RegistryKey.CurrentUser.OpenSubKey and
RegistryKey.SetValue would be better than just RegistryKey, but that is an
appropriate level of detail, since you can type that into your Visual Studio
help and get an answer.
======================================

I got a registry class from The Code Project, stripped out all the
functions
I didn't need & are left with the one function I want. However, I add the
header to the MFC form & when I try to call the function in that class I
get
a compiler error (C2352 Illegal Call To Non Static Member). God knows what
it means. Checked MSDN
(http://msdn2.microsoft.com/en-us/library/2x426hte(vs.71).aspx) & all it
says is to comment it out. What use is that?

Why don't you just use RegOpenKeyEx, RegSetValueEx, and RegCloseKey like I
suggested? They work perfectly well and come with good documentation,
unlike some functions in Windows.
So, if you got a total newbie asking the same question as I did & you get
a
useless answer like Brian wrote then its absolutely no use whatsoever, is
it? No. Why? Because they don't have a clue to begin with & what I've
found

I don't agree with Brian's answer, because it's an ATL-specific solution
when a very good generic Win32 API exists. But you have been quite
insulting to everyone.
on these programming newsgroups is that if someone answers the thread
others
see that its been answered & leave the post alone. Thefore if you get a
useless, no help whatsoever, ridiculous reply like I got originally by
Brian
then you have to ask the question again & again & again until it does get
answered correctly.

For C++ I used to ask questions on the GotDotNet website, but its now
closed. Used to get good results... & detailed answers too.

There are so, so, so many MVP's who give out rubbish in order to get
another
post listed. Does MVP status mean you can use Google & you cannot write
code? It seems that way to me from what I have seen by using these
newsgroups for a number of years.

So, Ben. Can you see now why I answered Brian like I did?

No. I cannot understand why you lash out at people who try to help you, and
except that it will make others willing to help.

The correct thing to reply to Brian would have been:

"Thanks for your suggestion, but my project doesn't use ATL. I'm looking
for a Win32/MFC solution."
Header file (RegisterEx.h):

#pragma once
#include "stdafx.h"

class CRegisterEx

{

public:

CRegisterEx(CString path);

~CRegisterEx(void);

public:

void WriteString(CString str, CString subPath = "", CString Key = "");

};

CPP Filr (RegisterEx.cpp)

#include "StdAfx.h"

#include "registerex.h"

#include <stdlib.h>

#pragma warning ( disable : 4267 )

#define MAX_BUFFER 2048

char buffer[MAX_BUFFER];

CString pt;

CRegisterEx::CRegisterEx(CString path)

{

pt = path;

}

CRegisterEx::~CRegisterEx(void)

{

}

// Writing strings to the register.

void CRegisterEx::WriteString(CString str, CString subPath, CString Key)

{

HKEY hk;

TCHAR szBuf[2048];

CString insidePath = pt;

if (Key)

{

insidePath = insidePath + "\\" + subPath;

}

if (RegCreateKey(HKEY_CURRENT_USER, _T(insidePath), &hk))

{

// Woops, you don't have privileges

TRACE0("Could not create the registry key.\n\nDo you have the right
privileges?\n");

}

strcpy(szBuf, str);

if (RegSetValueEx(hk, _T(Key), 0, REG_EXPAND_SZ, (LPBYTE)szBuf,
strlen(str)
+ 1))

{

// Hmm, you did something wrong

TRACE0("Could not set the given String.\n\nDo you have the right
privileges?\n");

}


RegCloseKey(hk);

}


The code you provided looks perfectly ok, except for warning disable pragma,
fixed buffer sizes, magic numbers, and plenty of other code smell. However,
I suspect that you tried to call it like this:

CRegisterEx::WriteString(....);

It's not a static member function, so you can't call it without an instance.
Since you claim to know .NET, you should already know what that means and
how to fix it. A little humility would be very much in order the next time
you ask a question, after all, you are the one who can't figure it out
without help.
 
B

Brian Muth

Private Sub RemoveSomeValue(ByVal sValue As String)
Dim strKey As String = "Software\MyKey"
Dim reg As RegistryKey = Registry.CurrentUser.OpenSubKey(strKey,
True)
Try
reg.SetValue(sValue, "*")
reg.Close()
reg = Nothing
Catch ex As Exception
End Try
End Sub

The equivalent C++ code is:

#include "stdafx.h"
#include "stdio.h"
#include "atlbase.h"

void RemoveSomeValue (_TCHAR *sValue)
{
CRegKey r;

LONG lRes = r.Open (HKEY_CURRENT_USER, _T("SOFTWARE\\MyKey"), KEY_READ |
KEY_WRITE);
if (lRes != ERROR_SUCCESS)
{
// error handling here
_tprintf_s (_T("Error %d can be found in WinError.h"), lRes);
return;
}

lRes = r.SetStringValue (sValue, _T("*"));
if (lRes != ERROR_SUCCESS)
{
// error handling here
_tprintf_s (_T("Error %d can be found in WinError.h"), lRes);
return;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
RemoveSomeValue (_T("MySpecialKey"));
return 0;
}

Brian
 
B

Brian Muth

I don't agree with Brian's answer, because it's an ATL-specific solution
when a very good generic Win32 API exists.

This is directed more at Newbie Coder than Ben:

Although the CRegKey class is defined in atlbase.h, there is really nothing
ATL-specific about it. It's basically just a thin wrapper around the Win32
API's you are struggling with. Feel free to bypass and go directly to
RegOpenKeyEx, RegSetKeyValue, and sibling Win32 API calls if you prefer.

Part of the purpose of classes in C++ is to provide a cleaner more
object-oriented approach to access OS services. It's my personal preference
to use such classes when they are available. Since the source code comes
with CRegKey, one can even single-step into the code to see what is
happening under the hood. In the full program that I posted, I suggest you
try that out with the debugger. The exercise will pay off in dividends in
future projects.
However, I add the
header to the MFC form & when I try to call the function in that class I
get
a compiler error (C2352 Illegal Call To Non Static Member). God knows what
it means. Checked MSDN
(http://msdn2.microsoft.com/en-us/library/2x426hte(vs.71).aspx) & all it
says is to comment it out. What use is that?

I'd venture to say that the MSDN Library is a superb resource and is getting
better all the time. Because I'm familiar with C++ I can instantly recognize
the issue when looking at that reference. Your problem is your unfamiliarity
with the C++ language. Instead of blaming me and other MVP's for weak
replies, buckle down and learn the language. Get a good C++ reference book.
And feel free to post lots of questions. Remember, questions without the
vitriol will deliver better responses.

Brian
 
B

Brian Muth

I didn't need & are left with the one function I want. However, I add the
By the way, the line that is commented shows the correct implementation. The
intention is for you to uncomment the:

// MyClass::MyFunc2();

and comment the

MyClass::MyFunc(); // C2352

to see how to correct the error.

Brian
 
N

Newbie Coder

Brian,

I have been away from C++ for 5-6 years concentrating more on VB.NET.
Getting bored with it so I decided to change back & prefer not to write .NET
Framework managed applications but application that don't need it because so
many people I help around the world don't have the .NET Framework installed
& why get them to download a 23.1 MB file just so they can run a few apps
that I have created for them?

My call which produced the compiler error was a call I wanted in that
specific class to delete the value in that registry key & the MSDN document
I sent the link about said to comment it out. So, if I comment the call out
that calls that function then it won't work. What is my way around it? How
do I call that function in CRegisterEx when I am in the cpp dialog of the
main app.. In VB.NET:

Dim regSetValue As New RegisterEx
regSetValue.SetValue("", "", "")
 
N

Newbie Coder

Brian,

Thank you for the code, but how do I call it from a MFC form on a button
click (btnRemove) when the code is in another header/cpp file?

I inclucde the line in the MFC form:

#include "RegisterEx.h"

But the function is still invisible

===========================

I have ordered a C++ MSPRESS book & have an old C++.NET Managed Extension
one here, but I want to write most apps without needing the .NET Framework
because no everyone has them.

--
Newbie Coder
(It's just a name)



Brian Muth said:
Private Sub RemoveSomeValue(ByVal sValue As String)
Dim strKey As String = "Software\MyKey"
Dim reg As RegistryKey = Registry.CurrentUser.OpenSubKey(strKey,
True)
Try
reg.SetValue(sValue, "*")
reg.Close()
reg = Nothing
Catch ex As Exception
End Try
End Sub

The equivalent C++ code is:

#include "stdafx.h"
#include "stdio.h"
#include "atlbase.h"

void RemoveSomeValue (_TCHAR *sValue)
{
CRegKey r;

LONG lRes = r.Open (HKEY_CURRENT_USER, _T("SOFTWARE\\MyKey"), KEY_READ |
KEY_WRITE);
if (lRes != ERROR_SUCCESS)
{
// error handling here
_tprintf_s (_T("Error %d can be found in WinError.h"), lRes);
return;
}

lRes = r.SetStringValue (sValue, _T("*"));
if (lRes != ERROR_SUCCESS)
{
// error handling here
_tprintf_s (_T("Error %d can be found in WinError.h"), lRes);
return;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
RemoveSomeValue (_T("MySpecialKey"));
return 0;
}

Brian
 
D

David Wilkinson

Newbie said:
Brian,

Thank you for the code, but how do I call it from a MFC form on a button
click (btnRemove) when the code is in another header/cpp file?

I inclucde the line in the MFC form:

#include "RegisterEx.h"

But the function is still invisible

Newbie:

Invisible? Won't compile? Won't link?
What is in "RegisterEx.h" ?

Problems of this kind have nothing to do with MFC, ATL, or Windows
programming, but rather with the basic C/C++ compilation model.

David Wilkinson
 
N

Newbie Coder

David,

The headers & the cpp files are in a previous post of mine in this thread
 
B

Brian Muth

Newbie Coder said:
Brian,

Thank you for the code, but how do I call it from a MFC form on a button
click (btnRemove) when the code is in another header/cpp file?

You need to insert a "function prototype" somewhere in either your source
code or header file. In this case, it would be:

void RemoveSomeValue (_TCHAR *sValue);

A function prototype basically looks like the function definition but
without the body. Some programmers add the "extern" modifier to clear
indicate that this has scope outside the translation unit, as in:

extern void RemoveSomeValue (_TCHAR *sValue);

http://msdn2.microsoft.com/en-us/library/061kwye0.aspx

The link above describes a variable with the extern modifer, but it can be
applied to a subroutine as well.

HTH

Brian
 
N

Newbie Coder

Brian,

The function prototype is already defined in the RegisterEx.h file which if
you see a few of my posts back the full header/cpp files I am using

I just want to be able to use that class in my MFC application like I wrote
before in VB.NET:

Dim clsReg As New RegisterEx
clsReg.SetSomeValue("bla", "bla", "bla")

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top