// sample functions for calling DinkeyChange.dll.


// Summary: Displays the error code with a helpful error message. You may want to change the messages.
// Syntax:
//DisplayChangeError (<ret_code> is int, <ext_err> is int)
PROCEDURE DisplayChangeError(ret_code is int, ext_err is int)
SWITCH ret_code
	CASE 401: Info("Error! No dongles detected that meet the search criteria!")
	CASE 409: Info("Error! The dongle detected has not been programmed by DinkeyAdd.")
	CASE 758: Info("Error! Cannot open the Update Code file specified.")
	CASE 759: Info("Error! The file specified is not a valid Update Code file.")
	CASE 762 TO 764: Info("Error! Update Code is not in a correct format / update code file is corrupt.")
	CASE 765: Info("Error! The Update Code does not match any dongle attached to your machine.")
	CASE 766: Info("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.")
	CASE 767: Info("Error! You have already entered this Update Code.")
	CASE 1905: Info("Error! There is no Software Key available for download.")
	CASE 1907: Info("Error! The Temporary Software Key has expired. Cannot download it.")
	CASE 1910: Info("Error! The Software Key has already been downloaded.")
	CASE 1921: Info("Error! You specified the 'default model' but there is more than one model available. You need to specify a specific dongle model.")
	CASE 1922: Info("Error! The model you requested is not available.") 
	CASE 1923: Info("Error! The Demo Software Key Template has been disabled.") 
	CASE 1924: Info("Error! You have run out of Demo Software Key activations. You need to order some more.") 
	OTHER CASE: Info("An error occurred updating the dongle.","Error: " + ret_code, "Extended Error: " + ext_err)
END


// Summary: gets the product code, dongle number and update number for all dongles attached that meet the specify search criteria
// Syntax:
//[ <Result> = ] GetDongleInfo ()
PROCEDURE GetDongleInfo()
hLib, ret_code are system int
type, model, update_number are fixed array of MAX_USB_DEVICES int
dongle_number is a fixed array of MAX_USB_DEVICES unsigned int
prodcode is a fixed array of MAX_USB_DEVICES fixed strings on MAX_PRODCODE_LEN
num_found is int
sDisplayMsg, sType, sModel are strings

// Load the DinkeyChange.dll
hLib = LoadDLL(changedllname)
IF hLib = 0 THEN
	Info("Cannot load " + changedllname, "This DLL should be in the same folder as this executable")
	RESULT -1
END

// get dongle information
ret_code = CallDLL32(changedllname, "DCGetInfo", TYPE_MASK_ALL, MODEL_MASK_DEFAULT, Null, MAX_USB_DEVICES, &num_found, &type, &model, &prodcode, &dongle_number, &update_number)
FreeDLL(hLib)

IF ret_code <> 0 THEN
	DisplayChangeError(ret_code, 0)
	RESULT ret_code
END

// display details on the dongles detected
sDisplayMsg = "Dongle Information:" + CR
FOR i = 1 TO num_found
	// get type of dongle
	IF type[i] = TYPE_PRO THEN
		sType = "Pro"
	ELSE
		sType = "FD"
	END
	// get model of dongle
	IF model[i] = MODEL_LITE THEN
		sModel = "Lite"
	ELSE IF model[i] = MODEL_PLUS THEN
		sModel = "Plus"
	ELSE
		sModel = "Net"	
	END
	sDisplayMsg += i + ". Dinkey " + sType + " " + sModel + " with dongle number " + dongle_number[i] + " product code " + ExtractString(prodcode[i], 1, Charact(0)) + " update number " + update_number[i] + CR
END

Info(sDisplayMsg)


// Summary: collect diagnostic information from the system and write to a file
// Syntax:
//[ <Result> = ] GetDiagnosticInfo (<sDiagPath> is string)
PROCEDURE GetDiagnosticInfo(sDiagPath is string)
hLib, ret_code are system int

// Load the DinkeyChange.dll
hLib = LoadDLL(changedllname)
IF hLib = 0 THEN
	Info("Cannot load " + changedllname, "This DLL should be in the same folder as this executable")
	RESULT -1
END

// get diagnostic info
ret_code = CallDLL32(changedllname, "DCGetDiagnosticInfo", sDiagPath)
FreeDLL(hLib)

IF ret_code = 754 THEN
	Info("Could not create the diagnostic file. Please check that the path exists:" + sDiagPath)
	RESULT ret_code
END

IF ret_code <> 0 THEN
	Info("Error " + ret_code + " writing diagnostic information")
	RESULT ret_code
END

Info("Diagnostic file written successfully")


// Summary: updates the dongle attached using the update code passed
// Syntax:
//[ <Result> = ] UpdateCodeString (<sUpdateString> is string)
//
// Parameters:
//	sUpdateString (string): <specify the role of sUpdateString>

PROCEDURE UpdateCodeString(sUpdateString is string)
hLib, ret_code are system int
extended_error, confirmation_code are int

// Load the DinkeyChange.dll
hLib = LoadDLL(changedllname)
IF hLib = 0 THEN
	Info("Cannot load " + changedllname, "This DLL should be in the same folder as this executable")
	RESULT -1
END

// update dongle
ret_code = CallDLL32(changedllname, "DCDoUpdateCodeString", sUpdateString, &confirmation_code, &extended_error)
FreeDLL(hLib)

IF ret_code <> 0 THEN
	DisplayChangeError(ret_code, extended_error)
	RESULT ret_code
END

Info("Dongle updated successfully.", "Confirmation Code is " + NumToString(confirmation_code, "04x"))


// Summary: updates the dongle attached using the update code passed
// Syntax:
//[ <Result> = ] UpdateCodeFromFile (<sUpdateCodeFile> is string)
//
// Parameters:
//	sUpdateCodeFile (string)

PROCEDURE UpdateCodeFromFile(sUpdateCodeFile is string)
hLib, ret_code are system int
extended_error, confirmation_code are int

// Load the DinkeyChange.dll
hLib = LoadDLL(changedllname)
IF hLib = 0 THEN
	Info("Cannot load " + changedllname, "This DLL should be in the same folder as this executable")
	RESULT -1
END

// update dongle
ret_code = CallDLL32(changedllname, "DCDoUpdateCodeFromFile", sUpdateCodeFile, &confirmation_code, &extended_error)
FreeDLL(hLib)

IF ret_code <> 0 THEN
	DisplayChangeError(ret_code, extended_error)
	RESULT ret_code
END

Info("Dongle updated successfully.", "Confirmation Code is " + NumToString(confirmation_code, "04x"))


// Summary: attempts to restore a corrupt Dinkey FD Lite dongle
// Syntax:
//[ <Result> = ] RestoreDinkeyFDLite ()
//
// Parameters: none

PROCEDURE RestoreDinkeyFDLite()
hLib, ret_code are system int

// Load the DinkeyChange.dll
hLib = LoadDLL(changedllname)
IF hLib = 0 THEN
	Info("Cannot load " + changedllname, "This DLL should be in the same folder as this executable")
	RESULT -1
END

ret_code = CallDLL32(changedllname, "DCRetoreDinkeyFDLite")
FreeDLL(hLib)

IF ret_code <> 0 THEN
	DisplayChangeError(ret_code, extended_error)
	RESULT ret_code
END

Info("Dinkey FD has been restored successfully.")


// Summary: displays the machine ID of the current computer
// Syntax:
//[ <Result> = ] GetMachineID ()
//

PROCEDURE GetMachineID()
hLib, ret_code are system int
extended_error is int
machineID is unsigned int

// Load the DinkeyChange.dll
hLib = LoadDLL(changedllname)
IF hLib = 0 THEN
	Info("Cannot load " + changedllname, "This DLL should be in the same folder as this executable")
	RESULT -1
END

ret_code = CallDLL32(changedllname, "DCGetMachineID", &machineID, &extended_error)
FreeDLL(hLib)

IF ret_code <> 0 THEN
	DisplayChangeError(ret_code, extended_error)
	RESULT ret_code
END

Info("Machine ID is: " + NumToString(machineID, "08x"))


// Summary: tries to download an available temporary software key for this machine
// Syntax:
//[ <Result> = ] DownloadTempSoftwareKey ()
//

PROCEDURE DownloadTempSoftwareKey()
hLib, ret_code are system int
extended_error is int
machineID is unsigned int

// Load the DinkeyChange module
hLib = LoadDLL(changedllname)
IF hLib = 0 THEN
	Info("Cannot load " + changedllname, "This DLL should be in the same folder as this executable")
	RESULT -1
END

// first call GetMachineID to get the machine ID value
ret_code = CallDLL32(changedllname, "DCGetMachineID", &machineID, &extended_error)

IF ret_code <> 0 THEN
	DisplayChangeError(ret_code, extended_error)
	RESULT ret_code
END

// then try to download a temporary software key for that machine ID
ret_code = CallDLL32(changedllname, "DCDownloadTempSoftwareKey", machineID, &extended_error)
FreeDLL(hLib)

IF ret_code <> 0 THEN
	DisplayChangeError(ret_code, extended_error)
	RESULT ret_code
END

Info("The Temporary Software Key has been downloaded successfully.")


// Summary: tries to download a demo software key for this machine
// Syntax:
//[ <Result> = ] DownloadDemoSoftwareKey (<sProdCode> is string)
//
// Parameters:
//	sProdCode (string)
//

PROCEDURE DownloadDemoSoftwareKey(sProdCode is string)
hLib, ret_code are system int
extended_error is int
machineID is unsigned int

// Load the DinkeyChange module
hLib = LoadDLL(changedllname)
IF hLib = 0 THEN
	Info("Cannot load " + changedllname, "This DLL should be in the same folder as this executable")
	RESULT -1
END

// first call GetMachineID to get the machine ID value
ret_code = CallDLL32(changedllname, "DCGetMachineID", &machineID, &extended_error)

IF ret_code <> 0 THEN
	DisplayChangeError(ret_code, extended_error)
	RESULT ret_code
END

// then try to download a 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 = CallDLL32(changedllname, "DCDownloadDemoSoftwareKey", machineID, sProdCode, SWKEY_MODEL_DEFAULT, &extended_error)
FreeDLL(hLib)

IF ret_code <> 0 THEN
	DisplayChangeError(ret_code, extended_error)
	RESULT ret_code
END

Info("The Demo Software Key has been downloaded successfully.")
