# Using the DinkeyChange API with Java

The DinkeyChange API allows you apply secure update codes to modify the information stored in Dinkey Pro and FD dongles directly from your Java application. The API also allows you to view the information needed to create these codes, and produce encrypted diagnostic files to help you troubleshoot any dongle-related problems your users might have.

The `DinkeyChange` class provides a Java wrapper around the API, which is implemented by native libraries. If you are not familiar with calling native libraries, it is recommended that you read the Java documentation on the subject, especially the documentation for `System.loadLibrary()`, and how Java uses the system property `java.library.path` to locate native libraries.

The native libraries and the classes described below are part of the `uk.microcosm.dinkeydongle` package. See the documentation for your development environment for details of how to include and reference this package in your project. dinkeydongle.jar provides all of the Java classes in the `uk.microcosm.dinkeydongle` package in a single JAR file. This allows you to import the package much more easily in many Java IDEs. Note that the native libraries are not included in the JAR file. You will need to distribute with your software the native libraries for every platform that your software supports.

## The DinkeyChange Class

This class exposes the DinkeyChange API to Java programs. It contains only static methods and fields, and cannot be instantiated.

### Fields

Useful constants you can use in your code:

- `static final int MAX_USB_DEVICES` - maximum number of dongles that the API can return information about.
- `static final int MAX_PRODCODE_LEN` - maximum length of a product code string.

Possible values to use as the `typeMask` parameter of `DinkeyChange.getDongles()`:

- `static final int TYPE_MASK_PRO` - search for Dinkey Pro dongles only.
- `static final int TYPE_MASK_FD` - search for Dinkey FD dongles only.
- `static final int TYPE_MASK_ALL` - search for both Dinkey Pro and FD dongles.

Possible values to use as the `modelMask` parameter of `DinkeyChange.getDongles()`:

- `static final int MODEL_MASK_LITE` - search for Lite dongles only.
- `static final int MODEL_MASK_PLUS` - search for Plus dongles only.
- `static final int MODEL_MASK_NET` - search for Net dongles only.
- `static final int MODEL_MASK_ALL` - search for all models of dongle.
- `static final int MODEL_MASK_DEFAULT` - search only for models that can be modified using update codes (i.e. ignore Lite dongles.)

Possible values to use as the `model` parameter of `DinkeyChange.downloadDemoSoftwareKey()`:

- `static final int SWKEY_MODEL_DEFAULT` - download the default model (valid only if exactly one model is set in the demo key template).
- `static final int SWKEY_MODEL_PRO_LITE` - download a Pro Lite demo software key.
- `static final int SWKEY_MODEL_PRO_PLUS` - download a Pro Plus demo software key.
- `static final int SWKEY_MODEL_PRO_NET` - download a Pro Net demo software key.
- `static final int SWKEY_MODEL_FD_LITE` - download an FD Lite demo software key.
- `static final int SWKEY_MODEL_FD_PLUS` - download an FD Plus demo software key.
- `static final int SWKEY_MODEL_FD_NET` - download an FD Net demo software key.

### Methods

All methods in this class throw a `DinkeyChangeException` if the underlying native API call fails. The `DinkeyChangeException` contains the error code and extended error code returned by the native API function.

#### static DongleToChange[] getDongles(int typeMask, int modelMask, String productCodeMask)

Returns a list of the dongles attached to this computer that match the search criteria specified by the method's arguments. `typeMask` and `modelMask` should be set to one of the possible values listed above. If `productCodeMask` is specified, only dongles with a product code that exactly matches (case is not important) this string will be returned. In most cases this string will be the product code of your protected application. Use `null` or the empty string as this parameter to search for dongles with any product code.

#### static String writeDiagnosticsToFile(String filename)

Writes diagnostic information about all dongles connnected to this computer to the file specified by `filename`. The output file is encrypted, and its contents can only be viewed using DinkeyLook. The path to the file can be absolute, or relative to the current working directory. If the filename does not end in ".dlpf" this extension will be added automatically. This method returns the absolute path to the file created.

#### static String writeDiagnosticsToFile(java.io.File file)

Writes diagnostic information about all dongles connnected to this computer to the file represented by `file`. The output file is encrypted, and its contents can only be viewed using DinkeyLook. The path to the file can be absolute, or relative to the current working directory. If the filename does not end in ".dlpf" this extension will be added automatically. This method returns the absolute path to the file created.

#### static int applyUpdate(String updateCode)

Applies a short secure update code to a dongle connected to this computer. Returns the confirmation code for this update.

#### static int applyUpdate(java.io.File updateFile)

Applies the secure update code in the file represented by `updateFile` to a dongle connected to this computer. Returns the confirmation code for this update.

#### static void restoreDinkeyFDLite()

Restores the hidden .DO NOT DELETE.dat on a Dinkey FD Lite dongle.

#### static long getmachineID()

Returns the machine ID required to generate a temporary software key for this computer.

#### static void downloadTempSoftwareKey()

Downloads and installs a temporary software key that was created by DinkeyAdd.

#### static void downloadDemoSoftwareKey(String productCode, int model)

Downloads and installs a demo software key from a demo template that was created by DinkeyAdd. `productCode` specifies the product code of the demo template to download the key from. `model` specifies the model of demo key to download, and should be set to one of the values listed above.

## The DongleToChange Class

An instance of `DongleToChange` represents one dongle found by `DinkeyChange.getDongles()`.

### Fields

Possible values for `type`:

- `static final int TYPE_PRO` - a Dinkey Pro dongle.
- `static final int TYPE_FD` - a Dinkey FD dongle.

Possible values for `mask`:

- `static final int MODEL_LITE` - a Lite dongle.
- `static final int MODEL_PLUS` - a Plus dongle.
- `static final int MODEL_NET5` - a Net dongle (5 users).
- `static final int MODEL_NETU` - a Net dongle (unlimited users).

Fields that provide information needed to create an update code for this dongle:

- `final int type` - integer representation of the dongle type.
- `final String typeAsString` - string representation of the dongle type.
- `final int model` - integer representation of the dongle model.
- `final String modelAsString` - string representation of the dongle model.
- `final String productCode` - the dongle's product code.
- `final long dongleNumber` - the dongle's dongle number (sometimes called dongle serial number).
- `final int updateNumber` - the dongle's update number.

## The DinkeyChangeException Class

An instance of this `Exception` subclass is thrown if a call to the DinkeyChange API fails. The usual `getMessage()` method returns a string providing general information about the error. If the problem occurred in Java code, `getCause()` will return the Java exception that caused the `DinkeyChangeException`.

### Methods

`DinkeyChangeException`'s methods provide further details about the error that occurred. It is strongly recommended that you display this information in the event of an error, to help both you and us troubleshoot a recurring problem. More information about Dinkey Pro/FD error numbers can be found in the user manual chapter Error Codes and online in our knowledge base.

#### int getError()

Returns an error code indicating the cause of the error.

#### int getExtendedError()

Returns an extended error code providing additional information about the error.

## Java Example Files

- DCSample.java - various examples of using the DinkeyChange classes.

## Supported Versions

All parts of the `uk.microcosm.dinkeydongle` package support Java platforms version 5 and newer.

## Try It Yourself

DCSample.java gives a simple example of calling each of the API functions. To run the DCSample program you will need a copy of the appropriate shared library for your platform (DinkeyChange.dll for 32-bit Windows, DinkeyChange64.so for 64-bit Linux etc.). Place the library in one of the paths specified by your Java environment's `java.library.path` system property. Browse the examples in `DCSample.java` to see how different features can be used.