Introduction to dongle authorization
Each dongle has a fixed serial number that cannot be modified. When the software is running, it reads the serial number of the dongle for determination. After the serial number is verified, the software can be used normally.
Buy a Dongle Development Kit
I bought a USB software dog for saftnet and checked the development materials (the development materials for dongle of different brands are generally different). The necessary development resources are as follows:
- Softdogsetup. dll: Provides the DLL for dongle Driver-related operations.
The following methods are required:
(1) The interface function getdogdriverinfo for obtaining the installation information of the dog driver is defined as follows:
Int Pascal getdogdriverinfo ();
Parameter: None
Return Value:
0 driver Not Installed
1. The driver version is the same (parallel port and USB)
2. the USB driver version is the same.
3. The parallel port driver version is the same.
4. You have installed an earlier version of the driver (parallel port and USB)
5. You have installed the old USB driver.
6. the old version of the parallel port driver has been installed.
7. The new driver version (parallel port and USB) has been installed)
8. A new version of USB driver is installed.
9. A new version of parallel port driver is installed.
3008 No administrator permission
(2) The function interface instdriver for driver installation is defined as follows:
Int Pascal instdriver (INT iflag );
Parameters:
Iflag = 1 USB dog Driver Installed
Iflag = 2 install parallel dog driver
Iflag = 3 install parallel dog driver and USB dog driver
Return Value:
If the operation is successful, 0 is returned; otherwise, an error value is returned.
(3) The uninstalldriver function interface for driver uninstallation is defined as follows:
Int Pascal uninstalldriver (INT iflag );
Parameters:
Iflag = 1 uninstall USB dog driver
Iflag = 2 uninstall the parallel dog driver
Iflag = 3 uninstall parallel dog drive and USB dog drive
Return Value:
If the operation is successful, 0 is returned; otherwise, an error value is returned.
- Win32dll. dll: Provides DLL for dongle read and write operations
The following methods are required:
(1) DWORD winapi dogread (DWORD dwbytes, DWORD dwaddr, char * pdata );
If the function returns 0, the read/write operation is successful, and other return values are error codes.
Variable description:
DWORD dwbytes: number of bytes for each conversion or read/write operation
DWORD dwaddr: the first address in the dog during each read/write operation
Char * pdata: Data pointer to the transform operation Buffer
Implement dongle authorization (C #)
Define softdog class
First, define a static softdog class. The main features of the static class are as follows:
- They only contain static members.
- They cannot be instantiated.
- They are sealed and cannot be inherited.
- They cannot contain instance constructors.
Using system;
Using system. runtime. interopservices;
Namespace hcommon. License
{
[Structlayout (layoutkind. Sequential)]
Public static class softdog
{
}
}
Then, use dllimport to introduce the used DLL method. Because the parameters and returned values are unfriendly, the method is defined as private and is not directly disclosed to the outside.
/// <Summary>
/// Interface function for obtaining dog driver installation information getdogdriverinfo
/// </Summary>
[Dllimport ("softdogsetup. dll", charset = charset. ANSI)]
Private Static extern ushort getdogdriverinfo ();
/// <Summary>
/// Function interface instdriver for Driver Installation
/// </Summary>
[Dllimport ("softdogsetup. dll", charset = charset. ANSI)]
Private Static extern ushort instdriver (ushort flag );
/// <Summary>
/// Uninstalldriver
/// </Summary>
[Dllimport ("softdogsetup. dll", charset = charset. ANSI)]
Private Static extern ushort uninstalldriver (ushort flag );
/// <Summary>
/// Read the dongle's single function interface dogread
/// </Summary>
[Dllimport ("win32dll. dll", charset = charset. ANSI)]
Private Static extern uint dogread (uint length, uint startindex, byte [] data );
Then, define the public method related to the dongle driver.
Public static bool driverinstalled
{
Get
{
Ushort code = getdogdriverinfo ();
If (code = 3008) throw new hcommon. hexception ("{0}: No administrator permission! ", 3008 );
If (code = 1 | code = 2 | code = 7 | code = 8) return true;
Return false;
}
}
Public static void installdriver ()
{
Ushort code = getdogdriverinfo ();
If (code = 3008) throw new hcommon. hexception ("{0}: No administrator permission! ", 3008 );
If (code = 1 | code = 2 | code = 7 | code = 8) return;
If (code = 4 | code = 5)
Uninstalldriver ();
Code = instdriver (1 );
If (code! = 0) throw new hcommon. hexception ("{0}: failed to install the driver! ", Code );
}
Public static void uninstalldriver ()
{
Ushort code = uninstalldriver (1 );
If (code! = 0) throw new hcommon. hexception ("{0}: failed to uninstall the driver! ", Code );
}
Finally, define the public method for reading the dongle serial number.
Public static string serialnumber
{
Get
{
Uint code = 0;
Byte [] DATA = new byte [100];
Code = dogread (0, 0, data );
If (code! = 0)
{
Throw new hcommon. hexception ("{0}: failed to read the serial number! ", Code );
}
Uint Sn = bitconverter. touint32 (data, 0 );
Return SN. tostring ();
}
}
Use softdog class
Client code snippets using softdog:
...
// Determine whether the dongle driver is installed when the software starts.
If (! Softdog. driverinstalled)
{
Softdog. installdriver ();
MessageBox. Show ("the dongle driver has been installed. Please insert the dongle. ");
}
...
// Define the serial number attribute
Private Static readonly string g_key = "12345678 ";
Private string serialnumber
{
Get
{
Try
{
String Sn = softdog. serialnumber;
If (string. isnullorempty (SN) return string. empty;
Return Sn;
}
Catch (exception ex)
{
Hcommon. applicationlog. logexception (Ex );
}
Return string. empty;
}
}
// Method for Determining the serial number
Private void checklicense ()
{
If (serialnumber! = G_key)
{
MessageBox. Show ("sorry, the computer must be inserted with the correct dongle before the software can be authorized to use. Please confirm to exit the system. ");
System. Windows. Forms. application. Exit ();
}
}
...
In addition, the serial number should be checked regularly during software operation, and the use of the software should be terminated when the dongle is found to have been pulled out. You need to define a timer and call the checklicense method every two minutes.
Summary of several common authorization Methods
- Software serial number authorization: a single machine is supported, and the serial number is easy to leak.
- Software serial number network registration activation: the optimal authorization control, but does not support standalone.
- Dongle authorization: supports single-host authorization, which is better than software serial number authorization, but occupies a hardware port.
Authorization using dongle control software (C #)