In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what is the use of [ImportDll()] in C#", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn "what is the use of [ImportDll()] in C#" this article.
In doing a C#project of the company, to write a webservice to provide a download method, before the company had, but to rectify, so this kind of bird shit stall and handed over to me, one of the ciphertext stream download to apply our group another group of people with C++ to write four dll, these DLL is written by ourselves, non-. NET hosting, this is a trouble;
If you add a reference to a project using the IDE, it will indicate that these dlls are unregistered and not managed by. NET. OK, by the way. NET managed and unmanaged.
The official terminology is:
Managed DLLs are files that can be referenced directly in the Common Language Runtime (CLR) and have the extension "DLL." Specifically, it refers to DLL files that encapsulate various namespaces, such as System.dll, etc. Unmanaged DLL is the usual dynamic link library, which includes DLL files that encapsulate all Windows API functions. Functions in various unmanaged DLLs cannot be called directly in the common language runtime, but need to pass through the "platform invocation" service provided by the. Net framework.
In layman's terms:
Managed Dll is the Dll file generated in the. NET environment.
unmanaged dll is not a dll file generated in the. NET environment. For example, C++ is written in C++.
Managed Dll files can be added directly to the project in the IDE via the "add reference" method. Then invoke the corresponding Dll object by using the Using Dll namespace. How to call unmanaged Dll file, this will use [ImportDll()] this method;
[ImportDll()] This method calls WIN32 API via P/Iovoke. Its purpose is to import external functions that are not hosted by NET. Of course, when using [ImportDll()], you must introduce namespaces:
using System.Runtime.InteropServices;
give an example
[DllImport(@("DownloadPlaintext.dll",EntryPoint="DownloadPlaintext"))] static extern Boolean FileDownLoad ([MarshalAs(UnmanagedType.LPStr)] StringBuilder pstrSourceFile, [MarshalAs(UnmanagedType.LPStr)] long lintFileSize, bool blnEnFlag)
This example imports a dll written in C++, named DownloadPlaintext.dll, which is a relative path, so we must manually copy this dll file to the corresponding release or Debug directory under the bin directory of the project. Otherwise, you will not find dll, or directly throw this dll into system32, but this is not safe;
EntryPoint is the entry point of the external function, which can be understood as the method that calls the external function. Assuming that the function name DownloadPlaintext exists in dll and exists in the project, there will be conflicts. Therefore, C#also provides a method name to redefine the external function.(Not the name in the dll, just change its use name in this project file), just like the above example, the DownloadPlaintext method in C#will be named FileDownLoad, it should be noted that the parameters should be consistent, the type should be consistent;
MarshalAs are rules that provide data encapsulation into unmanaged code, telling net that the first parameter should be of type string and the third bool;
Here to pay attention to a problem, just started to change the code, debugging passed, a hair to IIS, others simply can not successfully call this method, I entangled for a long time, and later found the reason!
Because it is on the basis of the previous code, did not pay attention to the pit left by the predecessors, the initial person of this code, the introduction path of dll is made into an absolute path:
[DllImport(@("C:\CloudDll\DownloadPlaintext.dll",EntryPoint="DownloadPlaintext"))]
In this case, IIS is unable to find this path, even if you have this dll in the C:\CloudDll directory, so be professional...
The above is "C#[ImportDll()] has what to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.