PB calls dll written in C #

Source: Internet
Author: User
Tags md5 encryption

C # is popular for its ease of use and powerful features. PowerBuilder as C/s MIS development tool, very simple and flexible, short development time, low development and maintenance costs, has been the first choice of SME information management system development tools. But the limitations of PB limit its further development, this is not much to say, The friends who play PB are clear. How PB calls C # write DLL, this interest comes up, can't help but to solve it. After a multi-party search and write code testing, is to solve the problem. The development steps and various setup options are listed below (development tools vs2008sp1+pb9.0-8836)

First we open VS2008, create a new project,

Then we double-click on the solution under the Properties folder [property], the system will open the type of the Property Settings window, select [Application]

Click the Assembly Info button, pop up the info window, check [make assembly COM visible],

Next select the [Generate] tab and select [Register for COM Interop]

Select the [Signature] tab, select [Sign]-->[for the assembly, select Strong Name key file]-->[new], enter your key name, remove [protect key file with password],

OK, here to configure the completion, the following code

Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Web; Using System.Security.Cryptography; Using System.IO; Using System.Web.SessionState; Using System.Runtime.InteropServices;

Namespace Encry {    [Guid ("ff6b4d57-f34e-49ec-9a3b-d0a17b59f78a")]     public Interface iencryption     {        [DispID (1)]          string encryptstring (String encryptstring, string encryptkey);         [DispID (2)]         string Decryptstring (String decryptstring, string decryptkey);         [DispID (3)]         string MD5 ( string str, int code);    }

    [Guid ("531d2d13-11de-41a8-a788-cb51b5642cce"), ClassInterface (ClassInterfaceType.None), ComSourceInterfaces (typeof (Iencryption))]     public class Encryption:iencryption     {        #region "3DES encrypted string"        //< Summary>        //des encrypted strings        // </summary>        //<param name= "encryptstring" > Strings to be encrypted </ Param>        //<param name= "Encryptkey" > Encryption key, required for 8-bit </param>        ///<returns> encryption successfully returns the encrypted string, failed to return the source string </returns>          public string encryptstring (string encryptstring, string encryptkey)          {            Try             {                 byte[] RgbKey = Encoding.UTF8.GetBytes (encryptkey.substring (0, 8));                 byte[] Rgbiv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};                 byte[] Inputbytearray = Encoding.UTF8.GetBytes (encryptstring);                 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();                 MemoryStream mstream = New MemoryStream ();                 CryptoStream cstream = New CryptoStream (Mstream, Dcsp.createencryptor (RGBKEY, RGBIV), cryptostreammode.write);                 Cstream.write ( Inputbytearray, 0, inputbytearray.length);                 Cstream.flushfinalblock ();                 return Convert.tobase64string (Mstream.toarray ());            }              catch              {                 return encryptstring;            }        }         #endregion

        #region "3DES decryption string"        //< Summary>        //des decryption strings        // </summary>        //<param name= "decryptstring" > String to Decrypt </ Param>        //<param name= "Decryptkey" > Decryption key, required for 8-bit, same as encryption key </ Param>        ///<returns> decryption successfully returns the decrypted string, failed to return to the source string </returns>          public string decryptstring (string decryptstring, string decryptkey)          {            Try             {                 byte[] RgbKey = Encoding.UTF8.GetBytes (decryptkey.subsTring (0, 8));                 byte[] Rgbiv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};                 byte[] Inputbytearray = Convert.frombase64string (decryptstring);                 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();                 MemoryStream mstream = New MemoryStream ();                 CryptoStream cstream = New CryptoStream (Mstream, DCSP. CreateDecryptor (RgbKey, Rgbiv), cryptostreammode.write);                 Cstream.write ( Inputbytearray, 0, inputbytearray.length);                 Cstream.flushfinalblock ();                 return Encoding.UTF8.GetString (Mstream.toarray ());            }              catch             {                 return decryptstring;            }        }

        #endregion

        #region "MD5 Encryption"        //< Summary>        //MD5 encryption        //< /summary>        //<param name= "str" > Cryptographic characters </param>         //<param name= "code" > number of encryption bits 16/32</param>         //<returns></returns>         public string MD5 (string STR, int code)         {             string strencrypt = string. Empty;             if (code = =)              {                 Strencrypt = system.web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5"). Substring (8, 16);            }

if (code = =) {Strencrypt = System.Web.Security.FormsAuthentication.HashPasswordForStoringI             Nconfigfile (str, "MD5"); }

return strencrypt; } #endregion}}

Generate, in your program directory under the Bin folder has a debug folder, the generated DLL is inside. This time, PB cannot call this DLL.

Here's a description of how the GUID in the code is generated.

Open your VS2008 command prompt,

Enter [Guidgen] and press ENTER

Choose Option 4, register the format, click [New GUID], will generate a new serial number, click [Copy] to copy the serial number, paste the time remember to remove the curly braces before and after

To be able to call this DLL, the key is to register the DLL, the registration process is as follows, open the VS2008 command prompt, open the path of your DLL, and then enter the following command

RegAsm your DLL name. dll/tlb: Your DLL name. tlb, see figure

If the registration is successful, the above screen will appear.

Next up is our PB How to call this DLL. See (Call as OLE)

Add an instance variable

OLEObject encryption

Write the following code in the form's Open () event

Encryption = Create OLEObject encryption. Connecttonewobject ("Encry.encryption")

"Encry" is the name of the DLL namespace you wrote in C #

"Encryption" is the class name of your DLL. Don't be mistaken.

In the [Perform MD5 encryption] button script write the following code

Long ll_status string Ls_text string Ls_dotext

Ls_text = Sle_1.text Ls_dotext = encryption.md5 (ls_text,32) Sle_2.text = Ls_dotext

If everything is OK, the following screen will appear

At this point, the program is finished. You can learn from the common reference.

PB calls dll written in C #

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.