C # call Delphi DLL in detail

Source: Internet
Author: User

C # calls the Delphi interface method, there are two ways to solve it:

First, the Delphi program is compiled into a COM component, and then in C # Reference COM components.

Second, unmanaged call Dephi DLL file.

Here we mainly explain the second method, the second method before the first to explain the next dllimport.

DllImport is an attribute class under the System.Runtime.InteropServices namespace, and its function is to provide the necessary invocation information for functions exported from an unmanaged DLL.

The DllImport property is applied to a method, requiring a minimum of the name of the DLL that contains the entry point.
DllImport is defined as follows:

Code1 [AttributeUsage (AttributeTargets.Method)]
2 public class DllImportAttribute:System.Attribute
3 {
4 public DllImportAttribute (string dllName) {...}//positional parameter is DllName
5 public callingconvention CallingConvention; Entry point calling convention
6 Public CharSet CharSet; The entry point uses a character connection
7 public string entrypoint; Entry point Name
8 public bool ExactSpelling; Whether the spelling must be exactly the same as the entry point indicated, false by default
9 public bool PreserveSig; Whether the signature of the method is preserved or converted
Ten public bool SetLastError; The return value of the Findlasterror method is saved here
public string Value {get {...}}
12}
13

The name of the DLL above sometimes needs to be written on the path such as [DllImport (@ "C:\OJ\Bin\Judge.dll")] so the absolute path of the specified DLL can be loaded normally.

If there is no path, DllImport will automatically look for the place in order:
1. exe directory
2. System32 Catalogue
3. Environment variable Directory
So you only need to copy the referenced DLL to these three directories, you can not write the path.

Description
1, DllImport can only be placed on the method declaration.
2. DllImport has a single positional parameter: Specifies the DllName parameter that contains the DLL name of the imported method.
3. DllImport has five named parameters:
A, the CallingConvention parameter indicates the calling convention of the entry point. If CallingConvention is not specified, the default value of Callingconvention.winapi is used.
b, the CharSet parameter indicates the character set used in the entry point. If CharSet is not specified, the default value of CharSet.Auto is used.
The C, entrypoint parameter gives the name of the entry point in the DLL. If entrypoint is not specified, the name of the method itself is used.
The D, exactspelling parameter indicates whether the entrypoint must exactly match the spelling of the indicated entry point. If ExactSpelling is not specified, the default value of false is used.
E, the PreserveSig parameter indicates whether the signature of the method should be preserved or converted. When the signature is converted, it is converted to a signature of an additional output parameter named RetVal with the HRESULT return value and the return value. If PreserveSig is not specified, the default value of True is used.
The F, setlasterror parameter indicates whether the method retains Win32 "previous error". If SetLastError is not specified, the default value of false is used.
4, it is a one-time attribute class.
5. In addition, methods decorated with the DllImport attribute must have an extern modifier.

Here's how to call:

The general use of DllImport to invoke is unmanaged.
The specific form is as follows: 1. [DllImport ("WZFSE.dll", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)].

The first parameter is the name of the DLL to refer to, which should be a constant (otherwise error).


To reference it in your own C # page, you have to declare the function on the page.

The following is followed by his declaration function:
2.public static extern void Initdll (INTPTR handle, bool methodaddress);(D ePHI How the defined function is defined here in C #: The Dephi declaration function is converted to C # Declaration function).
--Declare a function to refer to his DLLs such as 1 and 2 are tightly connected. That is, write a function to the corresponding application of the corresponding DLL.

The following is a reference to the parameter: the conversion of the Delphi type to C #.

First parameter type: IntPtr This type can be declared as a handle to another language, a pointer, and so on.
To implement a function pointer form that is similar to C + + in other languages, we consider a delegate from C # to implement it.

  

Here's how to display the Dephi form in your own page (and not display the title bar of the Delphi form for a seamless combination).

It's also important to check the Dephi form into your own C # system, which is a form that calls Delphi, and it shows a form with Delphi in our C # form.

How do we get rid of the form in Delphi? Then we need to use the API function. Because the Windows API is a relatively low-level language, it can be manipulated.

This is so quoted in C #: [DllImport ("user32.dll", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)]
public static extern void MoveWindow (INTPTR handler, int x, int y, int width, int height, bool repaint);

Insert a class below, which contains how to reference Dephi DLL and how to declare:

Code1 public class Complianceplatdll
2 {
3 public static string strpath = "";
4//<summary>
5//initialization
6//</summary>
7//<param name= "handle" ></param>
8//<param name= "Methodaddress" ></param>
9 [DllImport ("WZFSE.dll", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)]
Ten public static extern void Initdll (INTPTR handle, bool methodaddress);
<summary>
12///load the appropriate service
</summary>
+//<param name= "str" ></param>
//<param Name= "str2" ></param>
//<param Name= "I" ></param>
<returns></returns>
[DllImport ("WZFSE.dll", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)]
public static extern IntPtr Wzloadmodule (String str, string str2, int i);
<summary>
21///remove the corresponding service
</summary>
//<param name= "handle" ></param>
<returns></returns>
[DllImport ("WZFSE.dll", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)]
-public static extern bool Wzunloadmodule (INTPTR handle);
27
#region API functions
<summary>
+//API function set primary and secondary form
</summary>
///<param name= "Child" ></param>
//<param name= "parent" ></param>
[DllImport ("user32.dll", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)]
public static extern void SetParent (IntPtr child, IntPtr parent);
<summary>
PNS///API function Move form
</summary>
From//<param name= "Handler" ></param>
//<param name= "x" ></param>
X//<param name= "y" ></param>
//<param name= "width" ></param>
//<param name= "height" ></param>
///<param Name= "repaint" ></param>
[DllImport ("user32.dll", CharSet = charset.ansi, callingconvention = Callingconvention.stdcall)]
The public static extern void MoveWindow (INTPTR handler, int x, int y, int width, int height, bool repaint);
47
[DllImport ("user32.dll", EntryPoint = "GetWindowLong")]
The public static extern long GetWindowLong (IntPtr hwnd, int nIndex);
<summary>
*///API function to remove the title bar of the form
</summary>
//<param Name= "hwnd" ></param>
<param///Name= "NIndex" ></param>
///<param name= "Dwnewlong" ></param>
<returns></returns>
[DllImport ("user32.dll", EntryPoint = "SetWindowLong")]
SetWindowLong public static extern long (INTPTR hwnd, int nIndex, long dwnewlong);
The public const int Gwl_exstyle =-16;
The public const int ws_ex_transparent = 0x20;
ws_ex_layered Public const INT = 0x80000;
Lwa_alpha Public const INT = 2;
ws_caption Public const INT = 0xc00000;
#endregion
65}

Where the API SetWindowLong this method is possible to implement the removal of the form of the title bar, the specific call SetWindowLong (COMMON.P, Gwl_exstyle, GetWindowLong (handle, Gwl_exstyle ) & (~ws_caption));

But the general full use of API functions is called this

Code1 Decallback de1 = new Decallback (iscallback);//Use Delegate
2 Initdll (this. Handle, De1 (this. Handle));//initialization
3 IntPtr p = wzloadmodule ("Dorisksetup", "" ", 0);//Get Handle
4 if (P! = (INTPTR) 0)//determines if the handle is not a popup form
5 {
6//Remove the title bar of the Dephi form
7 SetParent (p, Panel1. Handle);
8 SetWindowLong (P, Gwl_exstyle, GetWindowLong (P, Gwl_exstyle) & (~ws_caption));
9 MoveWindow (p, 0, 0, Panel1. Clientsize.width, Panel1. Clientsize.height, false);
10}

SetWindowLong (IntPtr handle, int t,long l) The first parameter is a handle, which is the handle of the Dephi form you tune, the second argument is an integer, the dephi is represented by a constant gwl_exstyle, the style to be displayed, in C # The value of his translation is (-16), and the third letter is a long integer and the second parameter together indicates that the title bar of the first parameter handle form to be stripped is represented in Delphi as: GetWindowLong (Handle,gwl_exstyle) and (not Ws_ CAPTION) in C # is translated as: GetWindowLong (handle,-16) & (~0xc00000), handle refers to the handle of the Delphi form to invoke, GetWindowLong This function is to obtain information about the form. Generally this usage, if you do not understand can be proposed to discuss together.

The general type corresponds to the following:

dephi-->c#

Intger-->int

Longint-->long

Pchar-->string

Thandle-->intptr

C # call Delphi DLL in detail

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.