char * program_name = "Remove TS Licenses"; // messagebox title
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
LONG error_code;
stringstream error_message;
// is the user sure they want to do this?
if (IDYES==MessageBox(NULL, "This program will clear the Terminal Server licenses on this computer. Continue?", program_name, MB_YESNO))
{
HKEY hreg = NULL;
// try opening the license store
error_code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\MSLicensing\\Store", 0, KEY_READ, &hreg);
if (ERROR_SUCCESS!=error_code)
{
// display error message upon failure
error_message << "There was an error opening the license store! (error ROKE:" << error_code << ")";
MessageBox(NULL, error_message.str().c_str(), program_name, MB_ICONERROR);
return 1;
}
// get length of longest subkey name so we can allocate the correct sized buffer
DWORD subkey_buffer_length;
if (ERROR_SUCCESS!=(error_code=RegQueryInfoKey(hreg, NULL, NULL, NULL, NULL, &subkey_buffer_length, NULL, NULL, NULL, NULL, NULL, NULL)))
{
// display error message upon failure
error_message << "There was an error reading information from the license store! (error RQIK:" << error_code << ")";
MessageBox(NULL, error_message.str().c_str(), program_name, MB_ICONERROR);
return 2;
}
subkey_buffer_length++; // +1 for terminating null
char * subkey_buffer = new char[subkey_buffer_length];
int subkeys_deleted;
// enumerate subkeys (i.e. the licenses)
for(subkeys_deleted=0;;subkeys_deleted++)
{
// we continually look for subkey zero as it renumbers them after each deletion
error_code = RegEnumKey(hreg, 0, subkey_buffer, subkey_buffer_length);
if (ERROR_NO_MORE_ITEMS==error_code) // no more licenses to delete, so exit loop
break;
if (ERROR_SUCCESS!=error_code)
{
// display error message upon failure
error_message << "There was an error reading information from the license store! (error REKE:" << subkeys_deleted << ":" << error_code << ")";
MessageBox(NULL, error_message.str().c_str(), program_name, MB_ICONERROR);
return 3;
}
// delete the license we just found
if (ERROR_SUCCESS!=(error_code = SHDeleteKey(hreg, subkey_buffer)))
{
// display error message upon failure
error_message << "There was an error deleting a license from the license store! (error SHDK:" << subkey_buffer << ":" << error_code << ")";
MessageBox(NULL, error_message.str().c_str(), program_name, MB_ICONERROR);
return 4;
}
}
// success!
if (0==subkeys_deleted) // did we actually remove any?
{
MessageBox(NULL, "There were no licenses to remove!", program_name, MB_ICONINFORMATION);
}
else
{
MessageBox(NULL, "All licenses were removed successfully", program_name, MB_ICONINFORMATION);
}
}
return 0;
}
Name:
Anonymous2007-05-18 5:34 ID:8Ezvve8Q
>>18
What's even better is that none of these results are Win32. Ouch.