. Net calls an unmanaged Program (COM)

Source: Internet
Author: User

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

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.