PB calls the. net com Component

Source: Internet
Author: User

 

For projects that are currently developed simultaneously using pb and. NET, if you want to call the functions of. NET under PB, you can only make. NET a COM component and then call it in PB.

The method is as follows:

  1. Create a class library in. NET

  Ii. Register a class library

  3. Calling in PB

The following describes in detail: Take. NET2005 and PB11.5 as examples.

1. Create a class library in. NET

  1. Open Microsoft Visual Studio 2005

2. Create a class library project: click File> New> project. In the displayed window, select a class library.

3. Add code:

A class library file of Class1.cs is displayed after the file is created. open the file and modify it to the following code:

Using System;

Using System. Collections. Generic;

Using System. Text;

Namespace demo

{

   Public class Class1

   {

       Public int AddTenToParameter (int param1)

       {

           Return param1 + 10;

       }

        Public void SayHi ()

       {

           System. Windows. Forms. MessageBox. Show ("Hello World! ");

       }

        Public String GiveMeDate ()

       {

           Return DateTime. Now. ToLongDateString ();

       }

   }

}

  4. Add introduction because the MessageBox requires the Form component.

5. Modify project properties, click Project, right-click, and select Properties.

On the "generate" TAB, select"Register for COM Iterop"

6. Add introduction in Class1.cs

Using System. Runtime. InteropServices;

7. Add the following attributes to the class

[ComVisible(true)] [ClassInterface(ClassInterfaceType.AutoDual)] [ProgId("demo.Class1")] 
Now the class library is complete and can be compiled and used. To modifyProgIdYou can modify it in AssemblyInfo. cs.
Ii. Register a class library 
Use regasm for registration
The command is as follows: regasm demo. dll/tlb: demo. tlb

 3. Calling in PB

  Use the following code in the program:

Int return_code

Return_code = example. ConnectToNewObject ("demo. Class1 ")

If return_code <> 0 then

      Destroy example

      Messagebox ('error', 'an error occurred while opening OLE! ')

      Return

Else

      Example. sayhi ()

      Int v = 20

      Messagebox ('result', v)

      String s

      S = example. givemedate ()

      Messagebox ('result', s)

End if

Now we have finished calling the. NET Component in PB.

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.