473,804 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Registry Access (Write Access)

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\M yKey

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
Apr 3 '07
20 3805

"Newbie Coder" <ne*********@sp ammeplease.comw rote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
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.Curren tUser.OpenSubKe y(strKey,
True)
Try
reg.SetValue(sV alue, "*")
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/lib...gistrykey.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.Cur rentUser.OpenSu bKey and
RegistryKey.Set Value 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/lib...te(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(CSt ring path);

~CRegisterEx(vo id);

public:

void WriteString(CSt ring 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::CR egisterEx(CStri ng path)

{

pt = path;

}

CRegisterEx::~C RegisterEx(void )

{

}

// Writing strings to the register.

void CRegisterEx::Wr iteString(CStri ng str, CString subPath, CString Key)

{

HKEY hk;

TCHAR szBuf[2048];

CString insidePath = pt;

if (Key)

{

insidePath = insidePath + "\\" + subPath;

}

if (RegCreateKey(H KEY_CURRENT_USE R, _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::Wr iteString(....) ;

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.
Apr 5 '07 #11
>
Private Sub RemoveSomeValue (ByVal sValue As String)
Dim strKey As String = "Software\MyKey "
Dim reg As RegistryKey = Registry.Curren tUser.OpenSubKe y(strKey,
True)
Try
reg.SetValue(sV alue, "*")
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_U SER, _T("SOFTWARE\\M yKey"), 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.SetStringValu e (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("MySpecialK ey"));
return 0;
}

Brian


Apr 5 '07 #12
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/lib...te(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

Apr 5 '07 #13
>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/lib...te(vs.71).aspx) & all it
says is to comment it out. What use is that?
By the way, the line that is commented shows the correct implementation. The
intention is for you to uncomment the:

// MyClass::MyFunc 2();

and comment the

MyClass::MyFunc (); // C2352

to see how to correct the error.

Brian
Apr 6 '07 #14
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.Set Value("", "", "")

--
Newbie Coder
(It's just a name)
"Brian Muth" <bm***@mvps.org wrote in message
news:ej******** ********@TK2MSF TNGP02.phx.gbl. ..
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/lib...te(vs.71).aspx) & all
it
says is to comment it out. What use is that?

By the way, the line that is commented shows the correct implementation.
The
intention is for you to uncomment the:

// MyClass::MyFunc 2();

and comment the

MyClass::MyFunc (); // C2352

to see how to correct the error.

Brian


Apr 6 '07 #15
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" <bm***@mvps.org wrote in message
news:el******** ******@TK2MSFTN GP06.phx.gbl...

Private Sub RemoveSomeValue (ByVal sValue As String)
Dim strKey As String = "Software\MyKey "
Dim reg As RegistryKey = Registry.Curren tUser.OpenSubKe y(strKey,
True)
Try
reg.SetValue(sV alue, "*")
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_U SER, _T("SOFTWARE\\M yKey"), 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.SetStringValu e (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("MySpecialK ey"));
return 0;
}

Brian


Apr 6 '07 #16
Newbie Coder wrote:
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
Apr 6 '07 #17
David,

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

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

"David Wilkinson" <no******@effis ols.comwrote in message
news:%2******** *******@TK2MSFT NGP03.phx.gbl.. .
Newbie Coder wrote:
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

Apr 7 '07 #18

"Newbie Coder" <ne*********@sp ammeplease.comw rote in message
news:uH******** *****@TK2MSFTNG P04.phx.gbl...
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

Apr 8 '07 #19
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.SetSomeV alue("bla", "bla", "bla")

Cheers,

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

"Brian Muth" <bm***@mvps.org wrote in message
news:ut******** ******@TK2MSFTN GP06.phx.gbl...
>
"Newbie Coder" <ne*********@sp ammeplease.comw rote in message
news:uH******** *****@TK2MSFTNG P04.phx.gbl...
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

Apr 9 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
436
by: Tim Blizard | last post by:
I know this topic has been discussed before but I couldn't find any thread more recent than about 18 months and was interested in what conclusions people had come to recently. Invariably 3 advantages of XML config files are promoted; 1. The .NET framework provides built-in support for reading application configuration data from .config files very easily 2. Using these files makes it possible to deploy an application using
2
2479
by: Dan Sikorsky | last post by:
Should application data be read and written to the Registry to persist state, or should the App.config file be used? If the Registry should be used, what .NET class reads and writes the Registry? If the App.config file should be used, reading a configuration setting is trivial, but there appears to be no write methtod. Please post a write method when replying. --
7
16520
by: Dennis C. Drumm | last post by:
Can my program access the HKEY_LOCAL_MACHINE/Software section of the registry when being used by a user with restricted rights (not with admin rights)? If so, how? I have a program that functions just fine when run by an administrator but generates an exception when run by a restricted user. The program accesses the HKEY_LOCAL_MACHINE/Software section to set or get application settings that are not user specific. Thanks,
3
1409
by: Martin | last post by:
Hi I am attempting to write a small application that will allow users to manage the AddressBar entries within Internet Explorer. The typed URLs in the AddressBar can be found at HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs and I have successfully managed to get this list via a Windows C# .NET app with the following code : public Form1()
1
1743
by: Yoshitha | last post by:
Hi I am developing web application through which i've to write and read the values from the registry. When i set impersonate property to "false" it is displaying exception " Requested Registry Access is not allowed" so then i changed impersonate to "true" and it is reading values from the registry and writing into the registry. but when i runing the same aplication in same system now am getting the exception "requested registry access...
8
5497
by: Al Kaufman | last post by:
I have a simple console app that uses: regSubKey = <some registry key> Dim reg As RegistryKey = Registry.ClassesRoot.OpenSubKey(regSubKey) Dim path As String path = CStr(reg.GetValue(""))
3
5744
by: Nikolay Petrov | last post by:
Why I always get 'Requested registry access is not allowed' when i try to Read/Write to Windows Registry from ASP service. I use ASP NET account? Also granted full permissions to required Registry keys. What is the problem? TIA
3
18095
by: Dylan Parry | last post by:
Hi folks, I've just got a new machine at work, so I've spent all day copying across all of my work from my old machine. Now I've come across a problem that I've never seen before. I now get the following error when I attempt to run one of my sites: "Access to the registry key 'Global' is denied"
4
3283
by: Colin Priest | last post by:
I am trying to write a program to switch off serial port enumerations via the associated registry key. My code is: RegistryPermission permission = new RegistryPermission(RegistryPermissionAccess.Write, "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\ACPI\\PNP0501"); permission.Demand(); RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum\\ACPI\\PNP0501", true);
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.