C # detailed description of calling the dll of Delphi-full version (the Demo source code can be downloaded at the end)

Source: Internet
Author: User

C # There are two solutions to call the Delphi interface:

1. Compile the Delphi program into a COM component and reference the COM component in C.

II. The DLL file of the unmanaged call Dephi.

 

Here we will mainly explain the second method. before talking about the second method, we will first explain DllImport.

DllImport is an attribute class in the System. Runtime. InteropServices namespace. Its function is to provide necessary call information for functions exported from unmanaged DLL.

When the DllImport attribute is applied to a method, the dll name containing the entry point must be provided at least.
The definition of DllImport is as follows:
 

 

Code 1 [AttributeUsage (AttributeTargets. Method)]
2 public class DllImportAttribute: System. Attribute
3 {
4 public DllImportAttribute (string dllName ){...} // The positioning parameter is dllName.
5 public CallingConvention; // call convention for the entry point
6 public CharSet; // string used for the entry point
7 public string EntryPoint; // entry point name
8 public bool ExactSpelling; // whether the spelling must be exactly the same as the indicated entry point. The default value is false.
9 public bool PreserveSig; // whether the signature of the method is retained or converted
10 public bool SetLastError; // the return value of the FindLastError method is stored here
11 public string Value {get {...} }
12}
13

 

The above DLL name sometimes needs to write the path such as [DllImport (@ "C: OJBinJudge. dll")] so that the absolute path of the specified DLL can be loaded normally.

If there is no path, DllImport will automatically find the location in order:
1. Directory of exe
2. System32 directory
3. Environment Variable directory
Therefore, you only need to copy the referenced DLL to these three directories, and you do not need to write the path.

Note :&

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.