These two days have been used for research.. Net calls an unmanaged DLL. The project prepares to use C # To write the logic processing and WebService on the server. Soap is used in the middle to transmit data. The client's hardware and software environments and network environments are complex, it is better to try to use less intervention on the client, and finally adopt Delphi to write the client application. After all, it is installed on the client machine of the customer. NET framework is too large, especially on machines with low configuration. Of course, data transmission requires a compression and encryption process. To achieve the same set of compression between different languagesAlgorithmIt is still quite troublesome, so I came up with a simple processing method. The compression algorithm uses zlib provided by dephi, And the encryption uses des and then writes it as COM.
1. Create an ActiveX Library Project in Delphi and add an Automation Object Unit to automatically create a COM interface unit and define the interface function. Pay attention to widestring for the return type.
Unit compress;
{$ Warn symbol_platform off}
Interface
Uses
Comobj, ActiveX, zlibcompress_tlb, stdvcl;
Type
Tzlibcompress = Class (tautoobject, izlibcompress)
Protected
Function compressdata (const sdata: widestring): widestring; safecall;
Function uncompressdata (const sdata: widestring): widestring; safecall;
Function decryptdata (const astr, akey: widestring): widestring; safecall;
Function encryptdata (const astr, akey: widestring): widestring; safecall;
{Protected Declarations}
End;
Implementation
Uses comserv, usoappacketcomuncompressor, des;
// ================================================ ==========================================================
// Compress data
// ================================================ ==========================================================
Function tzlibcompress. compressdata (const sdata: widestring): widestring;
Begin
Result: = xttocompresssoappacket (sdata );
End;
// ================================================ ==========================================================
// Uncompress data
// ================================================ ==========================================================
Function tzlibcompress. uncompressdata (const sdata: widestring): widestring;
Begin
Result: = xttouncompresssoappacket (sdata );
End;
// ================================================ ==========================================================
// Decrypt data
// ================================================ ==========================================================
Function tzlibcompress. decryptdata (const astr,
Akey: widestring): widestring;
Begin
Result: = decrypt (astr, akey );
End;
// ================================================ ==========================================================
// Encrypt data
// ================================================ ==========================================================
Function tzlibcompress. encryptdata (const astr,
Akey: widestring): widestring;
Begin
Result: = encrypt (astr, akey );
End;
Initialization
Tautoobjectfactory. Create (comserver, tzlibcompress, class_zlibcompress,
Cimultiinstance, tmapartment );
End.
2. reference the COM file in the. Net project and call the corresponding interface as follows
If ( This . Textbox1.text = String . Empty)
{
MessageBox. Show ("Null Param");
}
Zlibcompress. zlibcompress OBJ = New Zlibcompress. zlibcompress ();
Try
{
This. Textbox2.text=OBJ. encryptdata (This. Textbox1.text,"Gurub2b");
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
System. runtime. interopservices. Marshal. releasecomobject (OBJ );
}
3. The client program does not need to call com to directly reference the algorithm's unit file.
ExampleCode: Http://files.cnblogs.com/arping/testCOM.rar