Hello,
Am porting COM component(used by a VC++ app) to dotnet. Apparently, this
needs to be called by the existing app.
The existing signature in COM
HRESULT ReadRegStr(DWORD hKey, BSTR bstrSubKey, BSTR bstrValueName, BSTR*
pbstrResult);
.NET signature:
public string ReadRegStr(Int32 regKey, string sSubkey, string sValueName)
1. Is this alright?
2. From the VC app, the first parameter is always passed a pre-defined
constant like HKEY_CLASSES_ROOT or HKEY_CURRENT_USER etc.
How does one read the IntPtr for these constants?
Any pointers will be helpful,
thanks,
MoriCristian - 13 Nov 2006 18:34 GMT
Hi there
you can use there constant
HKEY_CLASSES_ROOT = H80000000
HKEY_CURRENT_USER = H80000001
HKEY_LOCAL_MACHINE = H80000002
HKEY_USERS = H80000003
HKEY_CURRENT_CONFIG = H80000005
or (that is better, in my opinion) use the Microsoft.Win32.RegistryHive enum
example
Int32 HKeyCurrentUser=(Int32) Microsoft.Win32.RegistryHive.CurrentUser;
Hope this helps
Cristian
> Hello,
> Am porting COM component(used by a VC++ app) to dotnet. Apparently, this
[quoted text clipped - 14 lines]
> Any pointers will be helpful,
> thanks,
Sankalp - 14 Nov 2006 06:19 GMT
Thanks Cristian,
Is there a way to directly resolve the "Int32" to a RegistryKey type where
the value of Int32 pertains to the constants (for registry).
Sankalp
MoriCristian - 14 Nov 2006 08:42 GMT
If i understand well, you want to oper a registry key in .NET and then use
the handle on a dll.
This could not be done, since tha handle of the registry key is in a
non-public field of a non-public field of the RegistryKey class.
Howerver we can work-around this using reflection
public IntPtr GetRegistryKeyHandle(RegistryKey regKey)
{
try
{
Type registryKeyType = typeof (RegistryKey);
BindingFlags bindings = BindingFlags.Instance | BindingFlags.NonPublic |
BindingFlags.Public;
FieldInfo infoRegistry = registryKeyType.GetField("hkey", bindings);
object HKEY = infoRegistry.GetValue(registry);
Type HKEYtype = HKEY.GetType();
FieldInfo infoHandle = HKEYtype.GetField("handle", bindings);
IntPtr HANDLE = (IntPtr) infoHandle.GetValue(HKEY);
return(HANDLE);
}
catch
{
return(IntPtr.Zero);
}
}
you must give an opened registry key as argument.
Hope this was what you needed. Since this rely on reflection it is not
guaranteed that from version to version on the .net fw this code will work.
Regards
Cristian Mori
> Thanks Cristian,
>
> Is there a way to directly resolve the "Int32" to a RegistryKey type where
> the value of Int32 pertains to the constants (for registry).
>
> Sankalp
Sankalp - 15 Nov 2006 06:36 GMT
I want to work with the registy from .NET. However, which key to work upon is
sent by a COM aware client. Now the client sends constants like these -
HKEY_CLASSES_ROOT = H80000000. I wanted to know if these constants can be
directly mapped to RegistryKey objects (i.e Registry.ClassesRoot).
Currently, I have statements like these
swith (regKey)
{
case C__REGISTRYKEYS__HKEY_CLASSES_ROOT:
return Registry.ClassesRoot;
......
......
......
}
etc.
MoriCristian - 15 Nov 2006 08:54 GMT
for any key you can use my prev post.
for those key only you can do like that
return (Int32) Registry.ClassesRoot;
> I want to work with the registy from .NET. However, which key to work upon is
> sent by a COM aware client. Now the client sends constants like these -
[quoted text clipped - 10 lines]
> }
> etc.