Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
iam porting a c++ software to c#
iam reading a registry key in c++ my code is

if(::RegOpenKeyEx(hParentKey, (LPCTSTR) strKeyName2,NULL,KEY_READ  , &hKey)==ERROR_SUCCESS)
{
	lRet = RegQueryValueEx(hKey, key, NULL, &type,(LPBYTE)data,&dataLen); 
}

in c# my code is  

iam getting different output from c# code while reading a binary data from registry
how can i solve this problem ?

What I have tried:

public static readonly UIntPtr HKEY_CURRENT_USER = (UIntPtr)0x80000001;
        
        public const string lpSubKey = "Software\\Scooter Software\\Beyond Compare 3";

        public const int KEY_QUERY_VALUE = 0x1;
        [DllImport("advapi32.dll", EntryPoint = "RegOpenKeyEx")]
        public static extern int RegOpenKeyEx(UIntPtr hKey, string lpSubKey, uint ulOptions, int samDesired, out UIntPtr phkResult);

        [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx", CharSet = CharSet.Auto)]
        public static extern int RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, out uint lpType, StringBuilder lpData, ref int lpcbData);

        UIntPtr hKeyVal;
        int valueRet;
        uint lpType;

StringBuilder sb = new StringBuilder(1000);
            int lpcbData = sb.Capacity;
            //string sb = string.Empty;CacheID

            valueRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpSubKey, 0, KEY_QUERY_VALUE, out hKeyVal);


            valueRet = RegQueryValueEx(hKeyVal, "CacheID", 0, out lpType, sb, ref lpcbData);

            //string str = Encoding.Default.GetString(new byte[]())
            MessageBox.Show(string.Format("data type :{0}\n,data size :{1}\n,data :{2}\n", lpType.ToString(), lpcbData.ToString(), sb.ToString()), "Value", MessageBoxButtons.OK);
Posted
Updated 26-Sep-17 2:55am

 
Share this answer
 
Don't know why you downvoted Richard. His answer was perfectly valid.

Instead of trying to go for a one-to-one translation, just rewrite the code using the .NET classes. You'll be writing far less code, doing the same thing, and get the bonus of better exception handling over and above the problem of what you're doing, which is just ignoring all return codes and, therefor, any errors.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900