C # dongle Program

Source: Internet
Author: User

When purchasing a Dongle, the manufacturer usually comes with a development manual and a CD. The development manual describes how to use and develop dongles. In this example, we use the dongle product of cylinder Information Technology Co., Ltd., which provides a class library not hosted by. Net to read and write data of the dongle. The following describes the Read and Write Functions in the dongle class library.

● Dogwrite Function

This function writes the data pointed to by pdogdata to the dongle, starts from the dogaddr address, and stops at the dogbytes address.

The function declaration is as follows:

[Dllimport ("win32dll. dll", charset = charset. ANSI)]

Public static unsafe extern uint dogwrite (uint idogbytes, uint idogaddr, byte * pdogdata );

The parameters are described as follows.

L idogaddr: the first address in the user zone when performing read/write operations on software dogs. Value Range: 0 ~ 99.

L idogbytes: the length of the byte used to perform read/write operations on software dogs. The value range is 1 ~ 100, and the sum of the value cannot exceed 100.

L pdogdata: pointer type variable. Point to the read/write operation or the transformed data buffer.

L return value: 0 indicates that the operation is successful, and other values are error codes.

● Dogread Function

This function reads data from the storage area starting with the gaddr In the dongle and stores the data in the buffer zone specified by pdogdata. The number of read bytes is idogbytes. Remember, the buffer size should be long enough.

The function declaration is as follows:

[Dllimport ("win32dll. dll", charset = charset. ANSI)]

Public static unsafe extern uint dogread (uint idogbytes, uint idogaddr, byte * pdogdata );

The parameters are described as follows.

L idogaddr: the first address in the user zone when performing read/write operations on software dogs. Value Range: 0 ~ 99.

L idogbytes: the length of the byte used to perform read/write operations on software dogs. The value range is 1 ~ 100, and the sum of the value cannot exceed 100.

L pdogdata: pointer type variable. Point to the read/write operation or the transformed data buffer.

L return value: 0 indicates that the operation is successful, and other values are error codes.

Before using this function, you must install it with the dongleProgramComplete installation, and copy the win32dll. dll file under the installation directory to the system directory. For example:

"\ SafeNet China \ softdog SDK v3.1 \ Win32 \ win32dll \ highdll \ win32dll" under the installation directory in Windows 2003. copy the DLL file to the "C: \ windows \ system32 \" folder.

Write a read/write class for dongle

Code
[Structlayout (layoutkind. Sequential)]

//This class is used to read and write dongles.

Public Unsafe ClassDog

{

Public UintDogbytes, dogaddr;//Set dongle byte length and start address

Public Byte[] Dogdata;//Set the Data Length

Public UintRetcode;

[dllimport ( " win32dll. DLL " , charset = charset. ANSI)]

Public static unsafe extern uint dogread ( uint idogbytes, uint idogaddr, byte * pdogdata);

[dllimport ( " win32dll. DLL " , charset = charset. ANSI)]

Public static unsafe extern uint dogwrite ( uint idogbytes, uint idogaddr, byte * pdogdata);

Public UnsafeDog (UshortNum)

{

Dogbytes=Num;

Dogdata= New Byte[Dogbytes];//Set the Data Length

}

Public Unsafe VoidReaddog ()

{

Fixed(Byte*Pdogdata= &Dogdata [0])

{

Retcode=Dogread (dogbytes, dogaddr, pdogdata );//Read data from dongle

}

}

Public Unsafe VoidWritedog ()

{

Fixed(Byte*Pdogdata= &Dogdata [0])

{

Retcode=Dogwrite (dogbytes, dogaddr, pdogdata );//Write Data to dongle

}

}

}

 

The following is the read/write method. Code
///   <Summary>
/// Write dongle
///   </Summary>
Public   Void Dogwrite ()
{
Dog dog =   New Dog ( 100 );
Dog. dogaddr =   0 ;
Dog. dogbytes =   10 ;
String Str =   " 123456 " ;
For ( Int I =   0 ; I < Str. length; I ++ )
{
Dog. dogdata [I] = ( Byte ) STR [I];
}
Dog. writedog ();

If (Dogred () = Str) // Read the data immediately after writing and check whether the data is successfully written.
{
MessageBox. Show ( " The password has been successfully written into the dongle! " , " Success prompt! " , Messageboxbuttons. OK, messageboxicon. information );
}
Else
{
MessageBox. Show ( " Failed to write password to dongle. Check whether the dongle driver is installed! " , " Failed prompt! " , Messageboxbuttons. OK, messageboxicon. information );
}
}
///   <Summary>
/// Read Password
///   </Summary>
///   <Returns> </returns>
Public   String Dogred ()
{
String Str1 =   " 123456 " ;
String Str =   "" ;
Dog dog =   New Dog ( 100 );

Dog. dogaddr= 0;

Dog. dogbytes= 10;

Dog. readdog ();

If(DOG. retcode= 0)//Start to read dongle data
{

char [] chtemp = New char [str1.length];

for ( int I = 0 ; I str1.length; I + )
{

Chtemp [I]=(Char) Dog. dogdata [I];

}

Str= NewString (chtemp );
}
ReturnSTR;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.