Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use DLLImport to call external DLL in C #

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to use DLLImport to call external DLL in C#". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use DLLImport to call external DLL in C#" can help you solve your doubts.

The basic format of C#.Net call:

The DllImport property provides call information for unmanaged DLL functions.

[DLLImport ("DLL file path")]

The modifier extern returns the value type method name (parameter list) such as:

Using System.Runtime.InteropServices; [DllImport ("KERNEL32.DLL", EntryPoint= "MoveFileW", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] public static extern bool MoveFile (String src, String dst); call general methods of unmanaged functions in DLL

First of all, external methods should be declared in the C # language source program, in the basic form:

Where:

DLL file: contains a library file that defines external methods.

Modifiers: access modifiers, other than abstract, that can be used when declaring methods.

Return variable type: the return variable type of the method you need to call in the DLL file.

Method name: the name of the method you need to call in the DLL file.

Parameter list: the list of methods you need to call in the DLL file.

Note: you need to use the System.Runtime.InteropServices namespace in the program declaration.

Other optional DllImportAttribute properties:

EntryPoint: specify the DLL entry point to be invoked.

SetLastError: determines whether there is an error in executing the method (using the Marshal.GetLastWin32Error API function to determine).

The default value in C# is false.

CharSet: controls how the name and string parameters in the function are encoded. The default is CharSet.Ansi.

ExactSpelling: whether to modify the entry point to correspond to different character encoding.

CallingConvention: specifies the calling convention used to pass method parameters. The default is WinAPI. This value corresponds to _ _ stdcall based on the 32-bit Intel platform.

BestFitMapping: whether the best mapping feature is enabled, the default is true. The best mapping feature provides matching characters automatically when there is no match. Characters that cannot be mapped are usually converted to the default "?".

PreserveSig: whether the managed method signature is converted to return HRESULT. The default value is true (the signature should not be converted). And the return value has an unmanaged signature with an additional [out, retval] parameter.

After reading this, the article "how to use DLLImport to call external DLL with C#" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report