"Newbie Coder" <newbiecoder@spammeplease.comwrote in message
news:%23J2wVz7dHHA.1216@TK2MSFTNGP03.phx.gbl...
Quote:
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/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.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.
Quote:
>
======================================
>
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.
Quote:
>
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.
Quote:
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."
Quote:
>
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.