// ChangeTest

// Sample code for calling DinkeyChange.Dll

#include <windows.h>
#include <stdio.h>

#include "Visual Studio 5\DinkeyChange\resource.h"
#include "ChangeTest.h"

#ifdef _MSC_VER
  #pragma warning(disable:4996)							// to disable warnings about sprintf etc... means we can use the same source for all compilers
#endif

char g_szAppName[] = "ChangeTest";
void DisplayError(int ret_code, int extended_error);


// routine for main dialog box
BOOL CALLBACK MainDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {            
	int ret_code, i, num_found, confirmation_code, extended_error;
	char szType[4], szModel[5];
	char szDisplayMsg[12800], szBuffer[257], szProdCode[9];
	// these are the arrays that are filled-in by DCGetInfo
	int type[MAX_USB_DEVICES];
	int model[MAX_USB_DEVICES];
	char prodcode[MAX_USB_DEVICES][MAX_PRODCODE_LEN];
	unsigned int dongle_number[MAX_USB_DEVICES];
	int update_number[MAX_USB_DEVICES];
	unsigned int machineID;
	
	switch (message)
		{
		case WM_INITDIALOG:
			// nothing to do
			break;
	
		case WM_COMMAND:
			// *** Get Dongle Information button clicked ***
			if (LOWORD(wParam) == IDC_DONGLEINFO && HIWORD(wParam) == BN_CLICKED) {	
				
				// find dongle information for all dongle types, Plus & Net models, for all product codes
				// NB In practice you will probably want to set the prodcode_mask to the value of your product code
				ret_code = DCGetInfo(TYPE_MASK_ALL, MODEL_MASK_DEFAULT, NULL, MAX_USB_DEVICES, &num_found, type, model, prodcode, dongle_number, update_number);

				// check error code and display error if an error occurs
				if (ret_code == 401) {
					MessageBox(hwnd, "No dongles detected matching the search criteria specified", g_szAppName, MB_OK);
					return 0;
				}
				else if (ret_code != 0) {
					sprintf(szDisplayMsg, "Error %d getting dongle information", ret_code);
					MessageBox(hwnd, szDisplayMsg, g_szAppName, MB_OK);
					return 0;
				}
				
				// display info from dongle(s) detected
				szDisplayMsg[0] = 0;			// null string
				for (i=0; i<num_found; i++) {
					// get type of dongle
					if (type[i] == TYPE_PRO)
						strcpy(szType, "Pro");
					else
						strcpy(szType, "FD");
					
					// get model of dongle
					if (model[i] == MODEL_LITE)
						strcpy(szModel, "Lite");
					else if (model[i] == MODEL_PLUS)
						strcpy(szModel, "Plus");
					else
						strcpy(szModel, "Net");

					sprintf(szBuffer, "%d. Dinkey %s %s with dongle number: %u, product code: %s, update number: %d\n",
						i+1, szType, szModel, dongle_number[i], prodcode[i], update_number[i]);
					strcat(szDisplayMsg, szBuffer);
				}
				MessageBox(hwnd, szDisplayMsg, "Dongle Information", MB_OK);
			}			

			// *** Write Dongle Diagnostics to file ***
			if (LOWORD(wParam) == IDC_GETDIAGS && HIWORD(wParam) == BN_CLICKED) {	
				GetDlgItemText(hwnd, IDC_DIAG_PATH, szBuffer, sizeof(szBuffer));
				
				// NB this could take a while if the data area is very large
				ret_code = DCGetDiagnosticInfo(szBuffer);

				// check error code and display error if an error occurs
				if (ret_code == 754) {
					sprintf(szDisplayMsg, "Could not create the diagnostic file. Please check that the path exists: %s", szBuffer);
					MessageBox(hwnd, szDisplayMsg, g_szAppName, MB_OK);
					return 0;
				}
				else if (ret_code != 0) {
					sprintf(szDisplayMsg, "Error %d writing diagnostic information", ret_code);
					MessageBox(hwnd, szDisplayMsg, g_szAppName, MB_OK);
					return 0;
				}
				// display success
				MessageBox(hwnd, "Diagnostic file written successfully.", g_szAppName, MB_OK);
			}			

			// *** Make Changes button clicked for Short Codes ***
			if (LOWORD(wParam) == IDC_DO_UPDATE_CODE_STRING && HIWORD(wParam) == BN_CLICKED) {	

				GetDlgItemText(hwnd, IDC_UPDATE_CODE_STRING, szBuffer, sizeof(szBuffer));
				
				// make changes.
				ret_code = DCDoUpdateCodeString(szBuffer, &confirmation_code, &extended_error);

				// check error code and display error if an error occurs
				if (ret_code != 0) {
					DisplayError(ret_code, extended_error);
					return 0;
				}
				sprintf(szDisplayMsg, "Dongle updated successfully!\nConfirmation code is : %.4x", confirmation_code);
				MessageBox(hwnd, szDisplayMsg, g_szAppName, MB_OK);
			}
			
			// *** Make Changes button clicked for file ***
			if (LOWORD(wParam) == IDC_DO_UPDATE_CODE_FILE && HIWORD(wParam) == BN_CLICKED) {	

				GetDlgItemText(hwnd, IDC_UPDATE_CODE_FILE, szBuffer, sizeof(szBuffer));
				
				// make changes.
				ret_code = DCDoUpdateCodeFromFile(szBuffer, &confirmation_code, &extended_error);

				// check error code and display error if an error occurs
				if (ret_code != 0) {
					DisplayError(ret_code, extended_error);
					return 0;
				}
				sprintf(szDisplayMsg, "Dongle updated successfully!\nConfirmation code is : %.4x", confirmation_code);
				MessageBox(hwnd, szDisplayMsg, g_szAppName, MB_OK);
			}			

			// *** Restore Dinkey FD Lite ***
			if (LOWORD(wParam) == IDC_RESTORE_FD_LITE && HIWORD(wParam) == BN_CLICKED) {	
				
				ret_code = DCRestoreDinkeyFDLite();

				// check error code and display error if an error occurs
				if (ret_code != 0) {
					DisplayError(ret_code, 0);
					return 0;
				}
				MessageBox(hwnd, "Dinkey FD Lite restored successfully.", g_szAppName, MB_OK);
			}
			
			// *** Get Machine ID ***
			if (LOWORD(wParam) == IDC_GET_MACHINE_ID && HIWORD(wParam) == BN_CLICKED) {	
				
				ret_code = DCGetMachineID(&machineID, &extended_error);
				
				// check error code and display error if an error occurs
				if (ret_code != 0) {
					DisplayError(ret_code, extended_error);
					return 0;
				}
				// don't display a success message just display the machineID
				sprintf(szBuffer, "%.8X", machineID);
				SetDlgItemText(hwnd, IDC_MACHINE_ID, szBuffer);
			}			

			// *** Download Temporary Software Key ***
			if (LOWORD(wParam) == IDC_GET_TEMP_SWKEY && HIWORD(wParam) == BN_CLICKED) {	
				
				// first get the machine ID...
				ret_code = DCGetMachineID(&machineID, &extended_error);
				
				// check error code and display error if an error occurs
				if (ret_code != 0) {
					DisplayError(ret_code, extended_error);
					return 0;
				}
				
				// ... and then try to download the temporary software key
				ret_code = DCDownloadTempSoftwareKey(machineID, &extended_error);
				
				// check error code and display error if an error occurs
				if (ret_code != 0) {
					DisplayError(ret_code, extended_error);
					return 0;
				}
				MessageBox(hwnd, "Temporary Software Key downloaded successfully.", g_szAppName, MB_OK);				
			}

			// *** Download Demo Software Key ***
			if (LOWORD(wParam) == IDC_GET_DEMO_SWKEY && HIWORD(wParam) == BN_CLICKED) {	

                GetDlgItemText(hwnd, IDC_SWKEY_PRODCODE, szProdCode, sizeof(szProdCode));
            
				// get the machine ID
				ret_code = DCGetMachineID(&machineID, &extended_error);
				
				// check error code and display error if an error occurs
				if (ret_code != 0) {
					DisplayError(ret_code, extended_error);
					return 0;
				}
				
				// try to download the demo software key
                // (NB we are assuming only one dongle model is set in the Demo Software Template.
                //  If multiple models are set you will also need to specify the exact model)
				ret_code = DCDownloadDemoSoftwareKey(machineID, szProdCode, SWKEY_MODEL_DEFAULT, &extended_error);
				
				// check error code and display error if an error occurs
				if (ret_code != 0) {
					DisplayError(ret_code, extended_error);
					return 0;
				}
				MessageBox(hwnd, "Demo Software Key downloaded successfully.", g_szAppName, MB_OK);				
			}
			
			break;
			
		case WM_CLOSE:
			EndDialog(hwnd, TRUE);
		    return TRUE;
		}

	return FALSE;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow ) {
	// display dialog allow user to enter code
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)MainDlgProc);					// display main screen
   	return 0;
}


// displays messages for the most common errors. You will want to change this for your code
void DisplayError(int ret_code, int extended_error) {
	char szBuffer[200];
	
	switch (ret_code) {
	case 401:
		MessageBox(NULL, "Error! No dongles detected that meet the search criteria!", g_szAppName, MB_OK);
		break;
	
	case 409:
		MessageBox(NULL, "Error! The dongle detected has not been programmed by DinkeyAdd.", g_szAppName, MB_OK);
		break;

	case 758:
		MessageBox(NULL, "Error! Cannot open the Update Code file specified.", g_szAppName, MB_OK);
		break;

	case 759:
		MessageBox(NULL, "Error! The file specified is not a valid Update Code file.", g_szAppName, MB_OK);
		break;
		
	case 762:
	case 763:
	case 764:
		MessageBox(NULL, "Error! Update Code is not in a correct format / update code file is corrupt.", g_szAppName, MB_OK); 
		break;

	case 765:
		MessageBox(NULL, "Error! The Update Code does not match any dongle attached to your machine.", g_szAppName, MB_OK); 
		break;

	case 766:
		MessageBox(NULL, "Error! The update number for this Update Code is too high. You need to first enter an update code that was previously sent to you.", g_szAppName, MB_OK); 
		break;

	case 767:
		MessageBox(NULL, "Error! You have already entered this Update Code.", g_szAppName, MB_OK); 
		break;

	case 1905:
		MessageBox(NULL, "Error! There is no Software Key available for download.", g_szAppName, MB_OK); 
		break;

	case 1907:
		MessageBox(NULL, "Error! The Temporary Software Key has expired. Cannot download it.", g_szAppName, MB_OK); 
		break;

	case 1910:
		MessageBox(NULL, "Error! The Software Key has already been downloaded.", g_szAppName, MB_OK); 
		break;
        
    case 1921:
		MessageBox(NULL, "Error! You specified the 'default model' but there is more than one model available. You need to specify a specific dongle model.", g_szAppName, MB_OK); 
        break;
        
    case 1922:
		MessageBox(NULL, "Error! The model you requested is not available.", g_szAppName, MB_OK); 
        break;
        
    case 1923:
		MessageBox(NULL, "Error! The Demo Software Key Template has been disabled.", g_szAppName, MB_OK); 
        break;

    case 1924:
		MessageBox(NULL, "Error! You have run out of Demo Software Key activations. You need to order some more.", g_szAppName, MB_OK); 
        break;
        
	default:
		sprintf(szBuffer, "An error occurred.\nError: %d, Extended Error: %d", ret_code, extended_error);
		MessageBox(NULL, szBuffer, g_szAppName, MB_OK);
		break;
	}
	return;
}
