Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote some code to lock my software after specified time. For that I used registry.
I created One sub-key and checking there whether my software is running first time or not. If first time then I am creating sub-key and storing installation date.I am reading that date and adding some days and checking every time when user opened my software . Here Everything is working fine but after locked the software if user changes date and restore data then I am unable to track that. So I added other sub-key for that and when ever i am locking software I am replacing that sub-key value with 1.using that I want to check already one time locked software so if he restored data and changed date also I can lock the software. Here only I am getting problem. after locking software system is not adding value to that sub-key and giving error that "cannot write to the registry". So where I did mistake, I am unable to find. I checked net also and I found one post here that telling how to edit values of registry sub-keys. I tried that also but problem not rectified.

Here I am posting my code also

//code to lock software after specified date
            Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings");
            if (key != null)
            {
                Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other");
                if (subkey != null)
                {
                    uncheck();
                }
                else
                {
                    checkService();
                    getipl();
                    crelogin();
                    chklogin();
                    chkpass();
                }
            }
            else
            {
                RegistryKey subkey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\OurSettings\other");
                MessageBox.Show("Created");
                
                subkey.SetValue("InstallDate", DateTime.Now.ToBinary(), RegistryValueKind.QWord);
                subkey.SetValue("number", cn, RegistryValueKind.QWord);//cn is for checking whether locked or not.cn is public variable declared as public void int cn=0
                subkey.Close();
                uncheck();
            }
            checkService();
            getipl();
            crelogin();
            chklogin();
            chkpass();
        }
        public void uncheck()
        {
            Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other");
            try
            {
                var result = (long)subkey.GetValue("InstallDate", "date");
                var date = DateTime.FromBinary(result);
                MessageBox.Show("Install Date" + " " + date.ToString());
                var comp = date.AddDays(30);// locking days after installation.
                var dt = DateTime.Now;
                //var num = (long)key.GetValue("state", "number");
                MessageBox.Show("next Lock Date" + " " + comp.ToString());
                //MessageBox.Show("state"+" "+num.ToString());
                if (date >= comp)
                {
                    Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other");
                    con.Close();
                    con.Open();
                    SqlCommand cmd = new SqlCommand("delete  comapny where ctype='b'", con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    cn = 1;
                    subkey.SetValue("number", cn, RegistryValueKind.QWord);
                    subkey.Close();
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();//calling other window to show message
                    this.Close();
                }
                else if (dt >= comp)
                {
                    Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other");
                    con.Open();
                    SqlCommand cmd = new SqlCommand("delete  comapny where ctype='b'", con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    //cn = 1;
                    //key.SetValue("number", cn, RegistryValueKind.QWord);
                    //key.Close();
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();//calling other window to show message
                    this.Close();
                }
                else
                {
                    checkService();
                    getipl();
                    crelogin();
                    chklogin();
                    chkpass();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Software Is Locked. Contact 9290345689");
                this.Close();
            }
        }


What I have tried:

I checked net also and i found one post here that telling how to edit values of registry sub-keys. I tried that also but problem not rectified.
Posted
Updated 25-Jan-18 22:20pm
v6
Comments
Sinisa Hajnal 24-Jan-18 5:54am    
You're not checking if the key exists uncheck method. Go step by step, find out which line causes the error and check that part only.
vijay_bale 24-Jan-18 6:36am    
I am checking at the top if the key exists or not. if not I am creating key.If exists,
simply I am coming out of loop and going to next step checkService();

Rule one: stop using the registry.
Access to the registry is more restricted than it used to be, and likely to become more so. And your problem is most likely to be that the part of the registry you are trying to edit needs Admin access, which means using UAE and getting permission from the user to elevate your app.

Instead, consider using a configuration file where you can add, delete, and edit entries as needed.
 
Share this answer
 
Comments
vijay_bale 24-Jan-18 6:04am    
OK taking settings from other file, I will try for that.

What you told maybe right. But my login is admin login only. already one sub-key I am creating and adding. I am running visual studio also in admin mode.
vijay_bale 24-Jan-18 8:02am    
I m able to store settings but unable to read from that file.
OriginalGriff 24-Jan-18 8:14am    
And without any idea of the code you used I am supposed to do what?
vijay_bale 24-Jan-18 9:38am    
You have to do nothing. Just I told.As experienced programmer, may be you can give any idea. Thats why I told.
OriginalGriff 24-Jan-18 9:59am    
As an experienced programmer, I know that there are a large number of ways to do things correctly, and an even larger number of ways to do them incorrectly! But I have no idea what code you used, because I cannot access your HDD, or see your screen...
You have to close keys before they can be re-opened. In your code you are never closing the key to "SOFTWARE\OurSettings" (which does not care because it is not used anywhere but should be also closed). The same applies to the key for "SOFTWARE\OurSettings\other" opened just after the above. You are then calling uncheck() which tries to open the same key again. When creating the key, you do it right by closing the key before calling uncheck().

Always close keys when not used anmyore. For your example you might also pass the key as argument to the uncheck() function and close it afterwards.

[EDIT]
If you want to write to the registry, you have to open the subkey with write access. The actually used method with a single parameter will open the subkey read only. See RegistryKey.OpenSubKey Method (Microsoft.Win32)[^] for methods that have access parameters.

I just noted also that you are opening the same subkey on top of the uncheck() function as key and then again later as subkey.

Note also that such end of trial period detections can be simply circumvented by deleting the keys.
[/EDIT]
 
Share this answer
 
v2
Comments
vijay_bale 24-Jan-18 6:38am    
I did What You told. But same error.
Jochen Arndt 24-Jan-18 7:01am    
See my updated answer.
vijay_bale 24-Jan-18 7:43am    
I changed that also.Same problem.I changed my code above.
Jochen Arndt 24-Jan-18 8:14am    
Check your code carefully. It should meanwhile differ from your posted code but may still contain wrong calls.

It is also still unclear where exactly the error occurs (at which code line).

For testing you might also try to read a value before writing it.

Finally, to be on the safe side, you might end your Windows session to ensure that there are no keys still opened (I don't know for sure if unclosed registry handles are closed when closing an application).
vijay_bale 24-Jan-18 9:44am    
OK. I will look again as you told. creation time what value I stored, that is only there.Date I am able to store in hexadecimal and able to read that.This int value is problem giving.This int also I am trying to store as string also. But no use.
I found solution and rectified the problem. Thanks to jochen and OriginalGriff.

below I am pasting the complete final code. Maybe it will useful for some one.

I found in net a solution that they told to add true while opensubkey. I added and that solved my problem

//code to lock software after specified date
            Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings");
            if (key != null)
            {
                key.Close();
                Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other");
                if (subkey != null)
                {
                    subkey.Close();
                    uncheck();
                }
                else
                {

                    checkService();
                    getipl();
                    crelogin();
                    chklogin();
                    chkpass();
                }
            }
            else
            {
                RegistryKey subkey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\OurSettings\other");
                MessageBox.Show("Created");
                
                subkey.SetValue("InstallDate", DateTime.Now.ToBinary(), RegistryValueKind.QWord);
                subkey.SetValue("number", cn, RegistryValueKind.QWord);
                subkey.Close();
                uncheck();
            }
            checkService();
            getipl();
            crelogin();
            chklogin();
            chkpass();
        }
        public void uncheck()
        {
            Microsoft.Win32.RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings\other",true);
            try
            {
                var stat = (long)subkey.GetValue("number", "st");
                var result = (long)subkey.GetValue("InstallDate", "date");
                var date = DateTime.FromBinary(result);
                MessageBox.Show("Install Date" + " " + date.ToString());
                DateTime comp = date.AddDays(1);//specifing days after inatallation date to lock
                var dt = DateTime.Now;
                
                MessageBox.Show("next Lock Date" + " " + comp.ToString());
                
                if (date >= comp)
                {
                    
                    con.Close();
                    con.Open();
                    SqlCommand cmd = new SqlCommand("delete  comapny where ctype='b'", con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    cn = 1;//already define as public. (public static int cn=0;)
                    subkey.SetValue("number", cn, RegistryValueKind.QWord);
                    subkey.Close();
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();//calling other window to show message
                    this.Close();
                }
                else if (dt >= comp)
                {
                    
                    con.Open();
                    SqlCommand cmd = new SqlCommand("delete  comapny where ctype='b'", con);
                    cmd.ExecuteNonQuery();
                    con.Close();

                    cn = 1;
                    subkey.SetValue("number", cn, RegistryValueKind.QWord);
                    subkey.Close();
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();//calling other window to show message
                    this.Close();
                }
                else if (stat!= 0)
                {
                    
                    con.Open();
                    SqlCommand cmd = new SqlCommand("delete  comapny where ctype='b'", con);
                    cmd.ExecuteNonQuery();
                    //Not calling any message window
                    con.Close();
                    subkey.Close();

                }

                else
                {

                    checkService();
                    getipl();
                    crelogin();
                    chklogin();
                    chkpass();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Software Is Locked. Contact 9290345689");
                this.Close();
            }
        }
 
Share this answer
 
v3

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