Click here to Skip to main content
13,141,576 members (59,012 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked
Posted 28 Jan 2009

How to Register a Program

, 28 Jan 2009
Rate this:
Please Sign up or sign in to vote.
How to register a program on end-user machine only and without end-user interference.

Program registration tester


How to hide values in the registry and use it for program registration!

Would it be possible to perform a program registration for a certain program (or other similar objects) and only keep the hidden on the end-user machine - in a "pretty safe" way - so that the average user would/could not mess around with it? This is what my article tries to solve. Put simply: how to register a program.

I wouldn't say that my solution is anything state-of-the art, or even close to being elegant, but it works and it's safe to the extent that I feel that most users would not dare to look into how it is made and how it is avoided. Also, in writing this I hope to learn more from the feedback it may produce, especially if I unintentionally re-invent techniques or functions already known.


Before I begin please allow me to first define what the article and code actually solves and exactly what I mean by saying "program registration" (it may not be obvoius). After that I'll go into more detail.

Definitions and Limits

  • A registration object is the entity provided to pinpoint (1) an official UTC*-timestamp and (2) a serial no. (SN) for the program being installed.
  • The code deals with the smallest possible set of information to establish such a registration entity and is by def. and for this demo. purpose (1) and (2).
  • The aticle does 'not' go into discussions about how a SN is made and how secure they are etc.
  • Except for time-services the code must not be dependend of any servers in the outside world.
  • The registration performed must be 100% free of charge for us.
*) Universal Time Coordinate.


  • Although having a tiny Win-registry API the point is not to produce yet-another-such :-).

What is the idea?

The basic idea is relatively simple: Would it be possible to write something somewhere and then hide it for the user to see? And next, would it be possible to get the right time - and then let the 'something' be this time? If 'yes' to both it could be done. Or, in other words, could I hide a timestamp in a secret Harry-Potter-spot? Of course in the end we are forced to write our 'thingy' in a registry-branch/key already present so that the (advanced) user does not just remove an added (and visible) key - the key must exist to begin with - like "Console" or something!

That's the idea. Let's have some fun!

The Hidden Registry Value

One month ago I came across this:

Nowadays, everyone has access to the registry editor and can modify, delete or add registry keys. This is a great thing, as long as you know how and which registry values need to be modified. Creating an invisible registry key gives you the guarantee that your keys will remain what you set them, and the applications you develop won’t encounter any problems in reading wrong data. If you want to create an invisible registry folder, this is the solution for you!

The trick lies in a Windows flaw, a flaw regularly exploited by malicious applications (virus, spyware) to hide their traces. The flaw (trick) is that a registry value name can only have the length of 255, if a name longer than 255 characters is given to a registry value, windows will think the end of the key has been reached and all values in that key will be hidden from view. You can see them only with special software designed for this purpose.

Source: ReviewingIT

I tested it and to my surprice it worked. Is this generally known? I have to admit that I didn't know about it. So after reading this I came up with this tiny and simple win-registry-class:

class CRegistryAccess
		// 0. Properties. (alphabetical).
		bool m_bHiddenName;
		HKEY m_hKey;
		CString m_cstrMainKey, 
		// 3. Methods. (alphabetical).
		static const bool 
		static HKEY 
			CreateKey(const CString&);
		static BOOL WINAPI 
			EnableProcessPrivilege(LPCTSTR, BOOL);
		static const CString 
			FormatKey(const CString&, const CString&);
		const bool 
			GetDataDWORD(const CString&, DWORD&);
		const bool 
			GetDataSZ(const CString&, CString&);
		static const CString 
			GetLongName(const CString&);
		static HKEY 
			OpenKey(const CString&);
		const bool 
			SetDataDWORD(const CString&, CONST DWORD&);
		const bool 
			SetDataSZ(const CString&, const CString&);
		// 0. Properties. (alphabetical).
		struct SNameTypeData
			CString m_cstrName;
			CString m_cstrType;
			CString m_cstrData;
		// 1. Constructors. (alphabetical).
		CRegistryAccess(const CString&, const CString &cstrSubKey = _T(""),
                      const bool bHiddenKeyEx = false);
		// 3. Methods. (alphabetical).
		const bool 
			DataExist(const CString&);
		const bool 
			DeleteData(const CString&);
		const bool 
		const bool 
		const void 
			GetAll(CArray<SNameTypeData, SNameTypeData>&);
		const void 
		const void 
		const void 
		const bool 
			GetData(const CString&, DWORD&);
		const bool 
			GetData(const CString&, CString&);
		const bool 
			GetData(const CString&, int&);
		const bool 
			GetData(const CString&, bool&);
		const bool 
			SetData(const CString&, CONST DWORD&);
		const bool 
			SetData(const CString&, const CString&);
		const bool 
			SetData(const CString&, const int&);
		const bool 
			SetData(const CString&, const bool&);

I prefer Set- and GetData(...) over Set- and GetValue(...) since this is what we do - setting/getting data - like the third column in regedit.exe suggests. For most purposes, at least for me right now, dealing with the registry is the same as working at one key (and ONE key only) at a time and then handling N data's under it. In that way I can concentrate on my one-and-only key opened (think of it as *this)! Hence the only method in this class dealing with a key is DeleteKey(...) and calling this is suicide - it removes the key and jumps up the tree one step making the parent node the open one. It fails if the key holds sub-keys BUT it wipes all data - so be careful. DeleteData(...) wipes all data under a key and DeleteData(const CString&) a specific. The group GetAll... is really just for enumerating the name(s)/type(s)/data(s) and the rest is overloaded get/set for the most important datatypes.

Also worth mentioning is: static BOOL WINAPI EnableProcessPrivilege(LPCTSTR, BOOL) which enables the owner of the object to have full access to the registry (got this from C Board and MS) AND the rest more or less are a protected by a thin layer over the win32 registry API.

The 007 stuff is bool m_bHiddenName;. If this is T the data is hidden under its key (hidden defaults to F in its constructor). 255 char's are padded in front of the name using:

const CString CRegistryAccess::GetLongName(const CString &cstrName)
// Return long name (for invisible entries).
		// 0         0         0         0          = 40
		// 0123456789012345678901234567890123456789
		static const CString s_cstr255Chars(
		CString cstrNameLong(s_cstr255Chars + cstrName);
		return cstrNameLong;
		WNDTRACEEXCEPTION_USERDLG("CRegistryAccess", "GetLongName", "", e);

Test stub for the class...


...after a value added...


Simple and straightforward. Next step is to get the right time.

What time is it?


Sync. to internet time (sorry for the Danish language)

Stupid question... but, chances are that the time sync. in Windows is turned off by the user making the registration worthless if we ask for his local PC-time. Therefore we need a time&date-server to ask instead, and luckily there is one such server on the internet, and has been for many years. They are called:


And there are many more. This host-list serves as the basis for my function inline const bool GetInternetTime(COleDateTimeEx &tNTP, const CString &cstrNTPServer = _T("")){...}. Calling this you'll have the current UTC! I was able to write this based on another CP article called CSNTPClient - An SNTP Implementation written by Mr. PJ Naughter.

After cutting some and adding some (mostly cut) I ended up with a set of classes and structs to retrieve the std. time as orig. defined by Mr. PJ Naughter. For this impl. we are only interested in the precision YYYYMMDD.

To make GetInternetTime(...) work we need a bunch of classes - defining the packet look, socket behaviour and some neat conversions etc. They are:

  • class SNTPTimePacket - a struct rep. seconds and fraction of s. as two DWORD's.
  • class CNTPTime - a class having SNTPTimePacket- and SYSTEMTIME cast operators etc.
  • class SNTPServerResponse - a cut down of a more advanced struct to just a CNTPTime for this purpose (so it could be removed all together).
  • class CNTPClient - class having const bool CNTPClient::GetServerTime(const CString &cstrHostName, SNTPServerResponse& sntpResponse, const int iPort).
  • class CNTPSocket - encapsulates native Win. sock calls like Close, Create and Connect.
  • class SNTPBasicInfo - struct holding the mandatory part of an NTP packet.
  • class SNTPAuthenticationInfo - struct holding the optional part of an NTP packet.
  • class SNTPFullPacket - struct rep. the two above.

I'll not go into all the time-code, but in short it's basically just setting up a Win32 UPD-socket (NTP uses UDP instead of the usual TCP) - ::WSAStartup(...) blah. blah. (see the books) and then create and connect it. GetServerTime(...) Sends and Recieves and after its success it goes: sntpResponse.m_ReceiveTime = sntpFullPacket.m_Basic.m_ReceiveTimestamp; after which we have a CNTPTime and following a SYSTEMTIME and in the end a COleDateTime. All hosts are found on port 123 (all that I know of at any rate).

class CNTPClient : public CObject
		// 1. Constructors. (alphabetical).
		// 3. Methods. (alphabetical).
		const bool GetServerTime(const CString&, SNTPServerResponse&,
                      const int iPort = 123);
		// 0. Properties. (alphabetical).
		DWORD m_dwTimeout;

If GetInternetTime(...) fails it continues using the next server in the table until success. In the unlikely event that no time (actually date only) can be returned an appropriate exception is thrown.

Using the Code

When we want to register our program now, the class for that deals with a hidden name under a predefined reg. key named in the class - we pick "..\HKEY_CURRENT_USER\Console" for the purpose. So at time of creation it says:

CProgramRegistration::CProgramRegistration(const CString &cstrAppName,
     const int iDaysToUseFreeOfCharge, const bool bHidden) : 
	// Create.
			// Get official UTC-time.
				// Abort.
			// From now on m_dtNow are the official UTC time!
			m_pregAccess = new CRegistryAccess(_T("Console"), _T(""),
			// If first-time-run are present we read it back and if
                            // not we write it as m_dtNow.
				m_dtFirstRun = m_dtNow;
			// Calc. expire date.
			m_dtExpire = m_dtFirstRun + COleDateTimeSpanEx(
                               m_iDays2UseFreeOfCharge, 0, 0, 0);
		catch(CUserException *pe)
		catch(CException *pe)

To create a trial version of your program for 7 days:

// Leave last arg. empty to hide it!
m_pReg = new CProgramRegistration(_T("My Program"), 7, false);

On new it UTC-timestamps and call UnRegister(...) after which the registry looks:


After registration using a random SN the registry looks:


Other methods of interest in CProgramRegistration are (bold text):

class CProgramRegistration
		// 0. Properties. (alphabetical).
		static const CString sm_cstrSNDummy;
		static const CString sm_cstrFirstRunRegKeyPostfix;
		static const CString sm_cstrSNRegKeyPostfix;
		int m_iDays2UseFreeOfCharge;
		CString m_cstrAppName;
		CRegistryAccess *m_pregAccess;
		// 3. Methods. (alphabetical).
		inline const CString GetFirstRunRegName()
		{ return(m_cstrAppName + sm_cstrFirstRunRegKeyPostfix); }
		inline const CString GetSNRegName()
		{ return(m_cstrAppName + sm_cstrSNRegKeyPostfix); }
		const bool 
			SetFirstRunDate(const COleDateTimeEx&);
		// 1. Constructors. (alphabetical).
		CProgramRegistration(const CString&, const int,
                       const bool bHiddenEx = true);
		// 3. Methods. (alphabetical).
		const int 
                        // Return 7 the first day for "MyProgram" and 6 tomorrow etc.
		static inline const CString&
                // Return "00000-00000-00000-00000-00000-00000".
		{ return sm_cstrSNDummy; }
		const bool  
                        // Return "090123" for "MyProgram".
		const bool 
                        // Return "uIkdx-1NNwc-xqvgd-cBGxR-Ju0ws-TTdsL" for "MyProgram".
		const bool 
                        // Return T if registred or in trial and F if not.
		const bool 
                        // Return T if registred and F if not.

		const bool 
                        // Write "uIkdx-1NNwc-xqvgd-cBGxR-Ju0ws-TTdsL" for "MyProgram".
			Register(const CString&); 
		const void 
                        // Delete MYPROGRAM_FIRST_RUN and MYPROGRAM_SN.
		const bool 
                        // Write "00000-00000-00000-00000-00000-00000" for "MyProgram".

In real life situations use:

// Last arg. is true to hide it (or empty)!
m_pReg = new CProgramRegistration(_T("My Program"), 7, true);

Also use:

const bool CRegistryAccess::DeleteData()

with extreme caution!

Points of Interest

One downside of this is that hidden data can be seen if enumerated! Try running the RegistryAccessTester and press 'Get all data' - long names are among the possibilities. Also remember to remove the registration if the user uninstalls the program!

End note: Some of the ...XXXEx classes are not in the project - I work with one set of classes and if not relevant I only release the XXX version. Like for COleDateTimeEx and COleDateTimeSpanEx where only the MFC-bases are used. Open prgreg.sln - it holds the two projects.


  • 1.00: 09.01.28: Initial ver.


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


About the Author

Michael Pauli
Software Developer (Senior)
Denmark Denmark
c/c++/c# -developer.

You may also be interested in...

Comments and Discussions

GeneralYou could also use a Unicode string... Pin
Ben Burnett3-Feb-09 4:31
memberBen Burnett3-Feb-09 4:31 
You could also use a plane old Unicode string. The Registry Editor also considers '\0' as the end-of-line character, but this can be added to a Unicode string with no ill effect (so long as there are not two in succession). However, security by obscurity is not really, in my opinion, the best approach to keeping your customers honest (is that your responsibility anyway? [possibly too philosophical a point for this forum]). That said, I'm no security expert so any of recommendation should be taken with a grain of salt.


"[F]reedom isn't a licence, it's responsibility." [David Gerrold, Author's Note in "The Man Who Folded Himself", 2003, p. 119]

GeneralRe: You could also use a Unicode string... Pin
Priyank Bolia3-Feb-09 8:33
memberPriyank Bolia3-Feb-09 8:33 
GeneralRe: You could also use a Unicode string... Pin
Ben Burnett5-Feb-09 6:29
memberBen Burnett5-Feb-09 6:29 
GeneralRe: You could also use a Unicode string... Pin
Michael Mogensen6-Feb-09 3:40
memberMichael Mogensen6-Feb-09 3:40 
QuestionRegistry access? Pin
Priyank Bolia2-Feb-09 19:06
memberPriyank Bolia2-Feb-09 19:06 
AnswerRe: Registry access? Pin
Michael Mogensen2-Feb-09 23:04
memberMichael Mogensen2-Feb-09 23:04 
GeneralC# Registry access? Pin
Priyank Bolia3-Feb-09 8:27
memberPriyank Bolia3-Feb-09 8:27 
GeneralRe: C# Registry access? Pin
Michael Mogensen3-Feb-09 13:34
memberMichael Mogensen3-Feb-09 13:34 
QuestionWhose PC is it anyway? Pin
supercat929-Jan-09 6:12
membersupercat929-Jan-09 6:12 
AnswerRe: Whose PC is it anyway? Pin
Michael Mogensen29-Jan-09 12:59
memberMichael Mogensen29-Jan-09 12:59 
QuestionVirus detection? Pin
Hans Dietrich28-Jan-09 19:31
mvpHans Dietrich28-Jan-09 19:31 
AnswerRe: Virus detection? Pin
Michael Mogensen29-Jan-09 11:22
memberMichael Mogensen29-Jan-09 11:22 
GeneralI think its interesting, but .. Pin
Garth J Lancaster28-Jan-09 12:52
memberGarth J Lancaster28-Jan-09 12:52 
GeneralRe: I think its interesting, but .. Pin
Michael Mogensen28-Jan-09 16:17
memberMichael Mogensen28-Jan-09 16:17 
GeneralRe: I think its interesting, but .. Pin
Garth J Lancaster28-Jan-09 17:32
memberGarth J Lancaster28-Jan-09 17:32 
GeneralRe: I think its interesting, but .. Pin
sgllama29-Jan-09 1:01
membersgllama29-Jan-09 1:01 
GeneralRe: I think its interesting, but .. Pin
Paolo Messina29-Jan-09 3:28
memberPaolo Messina29-Jan-09 3:28 
GeneralRe: I think its interesting, but .. Pin
Michael Mogensen29-Jan-09 12:45
memberMichael Mogensen29-Jan-09 12:45 
GeneralRe: I think its interesting, but .. Pin
fakyrr2-Feb-09 22:56
memberfakyrr2-Feb-09 22:56 
GeneralRe: I think its interesting, but .. Pin
Michael Mogensen2-Feb-09 23:18
memberMichael Mogensen2-Feb-09 23:18 
GeneralRe: I think its interesting, but .. Pin
SohailB12-Dec-11 5:15
memberSohailB12-Dec-11 5:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 28 Jan 2009
Article Copyright 2009 by Michael Pauli
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid