reg link

Hi all!

Here is my problem.
Got registry unter HKLM\SOFTWARE\A.
Now I want to create symlink to this key. in HKLM\SOFTWARE__symlink__
So I’ve prepared following code:

HKEY key;
int res;

DWORD dispo;
TCHAR linkname = “SOFTWARE_symlink_”;
TCHAR linkto = “\Registry\MACHINE\SOFTWARE\A”;

res = RegCreateKeyEx( HKEY_LOCAL_MACHINE, linkname, 0, NULL, REG_OPTION_VOLATILE|REG_OPTION_CREATE_LINK,
KEY_ALL_ACCESS | KEY_CREATE_LINK , NULL, &key, &dispo);
if (ERROR_SUCCESS == res)
{

res = RegSetValueEx(key, “SymbolicLinkValue”, 0, REG_LINK, (LPBYTE)link, lstrlen(link)*sizeof(WCHAR));
if (ERROR_SUCCESS == res)
{
printf(“link created\n”);
}
else
{
printf(“link not created %d\n”, res);
}

RegCloseKey(key);

}
else
{
printf(“error %d\n”, res);
}

res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, b, 0, NULL, REG_OPTION_VOLATILE,
KEY_ALL_ACCESS , NULL, &key, &dispo);
if (ERROR_SUCCESS == res)
{
printf(“OK!\n”);
}
else
{
printf(“error %d”, res);
}

Now - output for this code is:
link created
error 2
Error is ERROR_FILE_NOT_FOUND.

When I’m going to regedit and click on my symlink key I see message box: “Error while opening key”.
Can somebody tell me what’s wrong with this code?
I would like to open symlink, perform some actions on it and see results in original registry…
Thank you for any comment here.

xxxxx@gmail.com wrote:

Here is my problem.
Got registry unter HKLM\SOFTWARE\A.
Now I want to create symlink to this key. in HKLM\SOFTWARE__symlink__
So I’ve prepared following code:

Registry symbolic links are not recommended.

This cannot be your exact code. Note:

HKEY key;
int res;

DWORD dispo;
TCHAR linkname = “SOFTWARE_symlink_”;
TCHAR linkto = “\Registry\MACHINE\SOFTWARE\A”;

res = RegCreateKeyEx( HKEY_LOCAL_MACHINE, linkname, 0, NULL, REG_OPTION_VOLATILE|REG_OPTION_CREATE_LINK, KEY_ALL_ACCESS | KEY_CREATE_LINK , NULL, &key, &dispo);
if (ERROR_SUCCESS == res)
{
res = RegSetValueEx(key, “SymbolicLinkValue”, 0, REG_LINK, (LPBYTE)link, lstrlen(link)*sizeof(WCHAR));

There are two problems here. First, you do not have a variable called
“link” declared here. What is it? Second, you are passing ANSI strings
(note the lack of an L when you create the arrays), but you are passing
the length as if they were Unicode strings. If you are going to use
TCHARs, then you should use sizeof(TCHAR), not sizeof(WCHAR).

res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, b, 0, NULL, REG_OPTION_VOLATILE,
KEY_ALL_ACCESS , NULL, &key, &dispo);

You do not have a variable called “b” declared here. What is it?

When I’m going to regedit and click on my symlink key I see message box: “Error while opening key”.
Can somebody tell me what’s wrong with this code?

Yes, this code is referring to variables that you haven’t shown us, and
might very well contain garbage.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Tim.

Thank you for response on my post.
Well as you pointed out variable link is mistake in this listing. I don’t know why I pasted this code badly.
Correct code is:

HKEY key;
int res;

DWORD dispo;
TCHAR linkname = “SOFTWARE_symlink_”;
TCHAR linkto = “\Registry\MACHINE\SOFTWARE\A”;

res = RegCreateKeyEx( HKEY_LOCAL_MACHINE, linkname, 0, NULL, REG_OPTION_VOLATILE|REG_OPTION_CREATE_LINK,
KEY_ALL_ACCESS | KEY_CREATE_LINK , NULL, &key, &dispo);
if (ERROR_SUCCESS == res)
{

res = RegSetValueEx(key, “SymbolicLinkValue”, 0, REG_LINK, (LPBYTE)linkto, lstrlen(linkto)*sizeof(TCHAR));
if (ERROR_SUCCESS == res)
{
printf(“link created\n”);
}
else
{
printf(“link not created %d\n”, res);
}

RegCloseKey(key);

}
else
{
printf(“error %d\n”, res);
}

res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, linkname, 0, NULL, REG_OPTION_VOLATILE,
KEY_ALL_ACCESS , NULL, &key, &dispo);
if (ERROR_SUCCESS == res)
{
printf(“OK!\n”);
}
else
{
printf(“error %d”, res);
}

I also fixed problem with sizeof(WCHAR), now it is sizeof(TCHAR) but there is still same effect - theoretically link is created, but when I’m trying to open it - I can’t do that, same effect when I’m trying to open this link in regedit.

Note if you are using TCHAR, your literals should be _T(“”) so they, too,
are Unicode-aware.

I found that the “volatile” option seemed to have no effect whatsoever; on
reboot, the registry key still existed, with the value from the previous
boot cycle (which was useless)

Note that if you are asking for KEY_ALL_ACCESS, you must be running in a
context that allows this, which means with admin privileges.

The second RegCreateKey should fail with the error that the name already
exists. If you are just checking, use RegOpenKeyEx instead. Also, you
should not ask for more access rights than you need.

That’s all I can think of at the moment.
joe

Tim.

Thank you for response on my post.
Well as you pointed out variable link is mistake in this listing. I don’t
know why I pasted this code badly.
Correct code is:

HKEY key;
int res;

DWORD dispo;
TCHAR linkname = “SOFTWARE_symlink_”;
TCHAR linkto = “\Registry\MACHINE\SOFTWARE\A”;

res = RegCreateKeyEx( HKEY_LOCAL_MACHINE, linkname, 0, NULL,
REG_OPTION_VOLATILE|REG_OPTION_CREATE_LINK,
KEY_ALL_ACCESS | KEY_CREATE_LINK , NULL, &key, &dispo);
if (ERROR_SUCCESS == res)
{

res = RegSetValueEx(key, “SymbolicLinkValue”, 0, REG_LINK,
(LPBYTE)linkto, lstrlen(linkto)*sizeof(TCHAR));
if (ERROR_SUCCESS == res)
{
printf(“link created\n”);
}
else
{
printf(“link not created %d\n”, res);
}

RegCloseKey(key);

}
else
{
printf(“error %d\n”, res);
}

res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, linkname, 0, NULL,
REG_OPTION_VOLATILE,
KEY_ALL_ACCESS , NULL, &key, &dispo);
if (ERROR_SUCCESS == res)
{
printf(“OK!\n”);
}
else
{
printf(“error %d”, res);
}

I also fixed problem with sizeof(WCHAR), now it is sizeof(TCHAR) but there
is still same effect - theoretically link is created, but when I’m trying
to open it - I can’t do that, same effect when I’m trying to open this
link in regedit.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Joe thank you for your post.
Well it still doesn’t solve the problem sinec I’m still able to theoretically create symlink but can’t really access it.

maybe someone has complete code that is working?

didn’t think it can be a problem, ever.

If volatile didn’t work, there would be a wide range of things that didn’t work. What is your repro?

d

debt from my phone


From: xxxxx@flounder.com
Sent: 1/16/2012 12:21 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] reg link

Note if you are using TCHAR, your literals should be _T(“”) so they, too,
are Unicode-aware.

I found that the “volatile” option seemed to have no effect whatsoever; on
reboot, the registry key still existed, with the value from the previous
boot cycle (which was useless)

Note that if you are asking for KEY_ALL_ACCESS, you must be running in a
context that allows this, which means with admin privileges.

The second RegCreateKey should fail with the error that the name already
exists. If you are just checking, use RegOpenKeyEx instead. Also, you
should not ask for more access rights than you need.

That’s all I can think of at the moment.
joe

Tim.

Thank you for response on my post.
Well as you pointed out variable link is mistake in this listing. I don’t
know why I pasted this code badly.
Correct code is:

HKEY key;
int res;

DWORD dispo;
TCHAR linkname = “SOFTWARE_symlink_”;
TCHAR linkto = “\Registry\MACHINE\SOFTWARE\A<file:>”;
>
>
> res = RegCreateKeyEx( HKEY_LOCAL_MACHINE, linkname, 0, NULL,
> REG_OPTION_VOLATILE|REG_OPTION_CREATE_LINK,
> KEY_ALL_ACCESS | KEY_CREATE_LINK , NULL, &key, &dispo);
> if (ERROR_SUCCESS == res)
> {
>
>
> res = RegSetValueEx(key, “SymbolicLinkValue”, 0, REG_LINK,
> (LPBYTE)linkto, lstrlen(linkto)*sizeof(TCHAR));
> if (ERROR_SUCCESS == res)
> {
> printf(“link created\n”);
> }
> else
> {
> printf(“link not created %d\n”, res);
> }
>
>
> RegCloseKey(key);
>
> }
> else
> {
> printf(“error %d\n”, res);
> }
>
>
> res = RegCreateKeyEx(HKEY_LOCAL_MACHINE, linkname, 0, NULL,
> REG_OPTION_VOLATILE,
> KEY_ALL_ACCESS , NULL, &key, &dispo);
> if (ERROR_SUCCESS == res)
> {
> printf(“OK!\n”);
> }
> else
> {
> printf(“error %d”, res);
> }
>
> I also fixed problem with sizeof(WCHAR), now it is sizeof(TCHAR) but there
> is still same effect - theoretically link is created, but when I’m trying
> to open it - I can’t do that, same effect when I’m trying to open this
> link in regedit.
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</file:>

What problem are you solving by creating reg symlinks?

d

debt from my phone


From: xxxxx@gmail.com
Sent: 1/16/2012 12:31 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] reg link

Joe thank you for your post.
Well it still doesn’t solve the problem sinec I’m still able to theoretically create symlink but can’t really access it.

maybe someone has complete code that is working?

didn’t think it can be a problem, ever.


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

This tool claims to be able to do it
http://www.codeproject.com/KB/system/regsymlink.aspx

Don Burn
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

xxxxx@gmail.com” wrote in message
news:xxxxx@ntdev:

> Joe thank you for your post.
> Well it still doesn’t solve the problem sinec I’m still able to theoretically create symlink but can’t really access it.
>
> maybe someone has complete code that is working?
>
> didn’t think it can be a problem, ever.

Don,

Thank you for your posts.
Answering to your questoin. I just testing how it works in windows :).
Will check link you posted.

One additional question.
in this link:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724872(v=vs.85).aspx

You can find information like this: “Tree A registry tree can be 512 levels deep. You can create up to 32 levels at a time through a single registry API call.”
and this seems to be strange, since (maybe I don’t understand documentation properly) but it seems that it is not really true.
Suppose we have registry \a\b\c\d.

Now I’m using RegCreateKeyEx() for following key: \a\b\c\d\1\2\3\4\5\6\7..\50 (suppose that 1-50 are new keys). and I dont know why I’m able to create those keys. Isn’t it againts that what doc says?

Thank you.,

xxxxx@flounder.com wrote:

The second RegCreateKey should fail with the error that the name already
exists. If you are just checking, use RegOpenKeyEx instead.

No, RegCreateKeyEx opens successfully if the key already exists, just
like CreateFile. It’s certainly quicker than calling RegOpen…
followed by RegCreate…


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

I was able to do this using the following…
Note: The code sample does not create target key, hence, create it manually

OUTPUT

Z:\Data\Projects\RegLnk>objchk_wnet_x86\i386\reglnk.exe
Target key exist and readable.
Linked key created.
SymbolicLinkValue set.
Linked key closed.
Linked key opened.
Linked key closed.

SOURCES FILE

SOURCES = reglnk.c
TARGETLIBS =
TARGETNAME = reglnk
TARGETPATH =
TARGETTYPE = PROGRAM

C_DEFINES = $(C_DEFINES) /D_UNICODE /DUNICODE
MSC_WARNING_LEVEL = /W4 /WX
UMENTRY = wmain
UMTYPE = console
USE_MSVCRT = 1

REGLNK.C

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

UINT __cdecl wmain(__in ULONG argc, __in_ecount(argc) WCHAR argv)
{
LONG lResult;
HKEY hKey;
DWORD dwDisposition;
size_t szLen;

WCHAR szLnkKeyTgtFullPath = L"\REGISTRY\MACHINE\SOFTWARE\LnkKeyTgt";
WCHAR szLnkKeyTgt = L"SOFTWARE\LnkKeyTgt";
WCHAR szLnkKey = L"SOFTWARE\LnkKey";

UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);

StringCbLength(szLnkKeyTgtFullPath, MAX_PATH, &szLen);

// Verify if target key exist & is readable…
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szLnkKeyTgt, 0, KEY_READ, &hKey);
if(lResult == ERROR_SUCCESS) {
printf(“Target key exist and readable.\n”);
RegCloseKey(hKey);
} else {
printf(“Failed to open target key. Error Code %d.”, lResult);
goto Exit;
}

// Create key as link…
lResult = RegCreateKeyEx(HKEY_LOCAL_MACHINE, szLnkKey, 0, NULL,
REG_OPTION_VOLATILE | REG_OPTION_CREATE_LINK,
KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if (lResult == ERROR_SUCCESS) {
printf(“Linked key created.\n”);
} else {
printf(“Failed to create linked key. Error Code %d.”, lResult);
goto Exit;
}

// Set “SymbolicLinkValue”…
lResult = RegSetValueEx(hKey, L"SymbolicLinkValue", 0, REG_LINK,
(const BYTE *)szLnkKeyTgtFullPath, szLen);
if (lResult == ERROR_SUCCESS) {
printf(“SymbolicLinkValue set.\n”);
} else {
printf(“Failed to set SymbolicLinkValue. Error Code %d.”, lResult);
goto Exit;
}

// Close linked key…
lResult = RegCloseKey(hKey);
if (lResult == ERROR_SUCCESS) {
printf(“Linked key closed.\n”);
} else {
printf(“Failed to close linked key. Error Code %d.”, lResult);
goto Exit;
}

// Open created linked key…
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szLnkKey, 0, KEY_ALL_ACCESS,
&hKey);
if (lResult == ERROR_SUCCESS) {
printf(“Linked key opened.\n”);
} else {
printf(“Failed to open linked key. Error Code %d.”, lResult);
goto Exit;
}

// Close linked key…
lResult = RegCloseKey(hKey);
if (lResult == ERROR_SUCCESS) {
printf(“Linked key closed.\n”);
} else {
printf(“Failed to close linked key. Error Code %d.”, lResult);
}

Exit:
return 0;
}</stdio.h></strsafe.h></windows.h>