C # developing COM components

Source: Internet
Author: User

1, each COM component all external methods must be implemented through the interface form;

2, because. NET compiles COM components and is equivalent to C compiled COM components, so there is a case that must be running the target machine registration, for this. NET compiled COM components must be signed for the program, that is, strong-named key;

Steps:

1. Create a new Class library project (file-New-Project-Class library "name: Hk.netpbdemo")

2, for "Note 1th requirements", create a new interface file (project right-add-new item-Interface "named: IHK.cs")

3, IHK.cs interface file encoding:

int testfunction (int num, string str1, String str2, double db);

4, renamed Class1.cs: HKTest.cs (This step is actually very verbose, but the documents are sorted, so write it)

Click "Yes" in the Prompt box, as

5, HKTest.cs class file encoding:

Add namespaces: Using System.Runtime.InteropServices;

Implement the interface and add properties to the class header: [ClassInterface (ClassInterfaceType.None)]. Such as

6. Find the node "Properties" in Solution Explorer to open the AssemblyInfo.cs file

Such as

7. Change [Assembly:comvisible (false)] property to [Assembly:comvisible (true)]

Such as

8, Project right-property, or double-click the "Properties" node, go to the Project property settings interface

8.1. Select the "Applications" tab and click on the "Assembly Information" button, as

8.2. Tick "Make assembly COM visible", OK. Such as

8.3. Select the "Generate" tab, and under "Output" section of the bottom, tick "register for COM Interop". Such as

8.4. Select the "Signature" tab.

8.4.1, tick "sign the Assembly";

8.4.2, select "< New ...>" in the drop-down box "Select Strong Name Key File"

8.4.3, in the "Key file name" input: HK.NetPBDemo.snk (any name, SNK suffix can be)

8.4.4, remove the "Protect key file with password", OK.

"8.4.3, 8.4.4" such as

9, compile the project.

10. Open the VS command window, as

11. Operation Instruction:

gacutil/i DLL Path \hk.netpbdemo.dll Add this DLL to the global cache

regasm DLL Path \hk.netpbdemo.dll register this DLL

Such as

Attached: The 11 steps above will complete the COM component registration in the native environment and provide the call.

1, in order to use the other machine must be compiled "DLL", "TLB" files are copied on the target machine (installed. Net framework Environment) and placed in the required program root directory;

2, in the target machine "c:\WINDOWS\Microsoft.Net\Framework\v4.0.3.319" Find "RegAsm.exe", the path is my installation path, if not search under the "RegAsm.exe" this stuff. "My Environment: virtual machines, System windowsxp,. Net Framework 4.0"

3, run CMD Open the command window, talk about the path to "c:\WINDOWS\Microsoft.Net\Framework\v4.0.3.319",

Run directive: Regasm.exe DLL path \ HK.NetPBDemo.dll. such as "Registration successful"

Second, Delphi call

1. Open Delphi7,file-close All

2, Project-import Type Library, click the "Add" button to select the "TLB" com file "HK.NetPBDemo.tlb" click "Create Unit"

3. Project-import Type Library, select the component you just added in the list and click "Install ..." Button.

Such as

4, File-new-application, add a button, and then, on the ActiveX tab, select the component you just added to add to the project. Such as

5, Delphi key code

C # developing COM components

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.