Because many of the bottom of the company SDK, All are C++ Development , The upper application software is C# Winform Program . In the process of actual work , I often come across C# Program call C++ The problem with dynamic libraries . I've been working with C++ Dealing with ,C# How to call C++ Class library functions . There are some problems , So let's sum up C# Program call C++ All kinds of pits in dynamic library .
1. Possible problems ：
C# In the process of calling the dynamic library, I also encountered the following problems ：
1、C++ Pointer in ,C# Do I need to use pointers in ？
because C++ There are pointer parameters in the dynamic library in , So I also use .NET Unsafe code for , Used C# The pointer to , But there are still some problems , If in C# There is a problem when the parameter passed in is a two-dimensional array , In the end, we can only change C++ The parameter type of the parameter passed in by the function .
2、C# and C++ How to convert the type in ？
although C# and C++ similar , But the type of parameter it gives us is related to C# One by one, the parameter types of , See the following instructions for details .
3、C++ Function CallingConventionCharSet How to set ？
call C++ Function must be confirmed before , Otherwise, there may be function calls leading to stack asymmetry . The reason may be trusteeship PInvoke The signature does not match the unmanaged target signature . Functional CallingConvention and CharSet, You can view the corresponding .h The header file .
4、 How to decompile C++ Of dll The name of , port ？
Can pass Dependency Walker Tool to decompile and view the dynamic library information written by others
5、 How to pass parameters to a pointer function ？
For pointer functions required by functions ,C# Invocation time , You can define delegates to pass in parameters .
6、 We need to pay attention to C++ dll The compiled platform is x86 still x64, Is it multibyte or double byte (Unicode).
2. adopt Dependency Walke see dll The name of , port
download Dependency After that, the corresponding C++ dll The file loads in , You can see the corresponding information of the dynamic library , At the same time, you can also go through .h Header file view .
3. How to call
c# call c++ Dynamic library is usually written like this
[DllImport(SDK, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int IKSDK_Release();
1. DllImport The first parameter of SDK It's a dynamic library dll The path of , this dll Put it in the root of the program or c:windows/sytem32 Next , It is recommended to create a subdirectory in the program root directory to place the corresponding C++ Dynamic library files , Easy to update later .
2. CallingConvention Parameter is c# call c++ The way It's an enumeration. msdn Explain the following ：
|Cdecl||The caller cleans the stack . This enables you to call the varargs Function of （ Such as Printf）, Methods that can be used to accept variable arguments .|
|FastCal||This calling convention is not supported .|
|StdCall||The callee cleans up the stack . This is the platform to use invoke Default Convention for calling unmanaged functions .|
|ThisCall||The first parameter is this The pointer , It's stored in registers ECX in . Other parameters are pushed onto the stack . This calling convention is used to call from unmanaged DLL The exported class calls the method .|
|Winapi||This member is not actually a calling convention , Instead, the default platform calling convention is used . for example , stay Windows There is a tacit view that StdCall, stay Windows CE.NET There is a tacit view that Cdecl.|
3. CharSet Parameters control the way names are reorganized and string parameters are marshaled into functions . The default value is CharSet.Ansi.
4. entrypoint Parameter is used to identify the function in DLL Position in . In a managed object , The original name or ordinal entry point of the objective function identifies the function that crosses the boundary of the interaction . Besides , You can map the entry point to a different name , This is actually renaming the function . Generally, this parameter is not set by default .
5. The other parameters , Please check out MSDN about DllImportAttribute Explanation .
4. Other instructions
C# call C++ Dynamic library , Another particularly troublesome problem is the problem of parameters for . In the future, we will summarize a detailed comparison table based on the information on the Internet .
- .netcore stay linux Next use P/invoke Way to call linux Dynamic library
http://www.mamicode.com/info-detail-2358309.html .netcore It has been realized through p/invoke Way to call linux Dynamic link library for (*.so) file 1 ...
- c# call c++ Some understanding of dynamic library
call c++ Dynamic library is usually written like this [DllImport("UCamer.dll", CallingConvention = CallingConvention.Winapi)] ...
- C# call C++ Dynamic library （dll）
In the actual software development process , Because the company uses multiple languages to develop , stay C# You may need to implement a function in , This function may have been implemented in other languages , So can we call modules written in other languages ? And that is , because C# Well developed projects , We can use re ...
- Java call dll Dynamic library
In recent projects java call dll Dynamic library , So I studied this aspect . The toolkits used are as follows <dependency> <groupId>net.java.dev.jna</g ...
- 【C#】 Use Gsof.Native Dynamic invocation C Dynamic library
[C#] Use Gsof.Native Dynamic invocation C Dynamic library One . background Use C# When developing clients , We often call some standard dynamic libraries or C Class library of . although C# Provided PInvoke The way , But because of the variety of scenarios used , ...
- python call .net Dynamic library
# python call .net Dynamic library ### pythonnet brief introduction ------------------------------ pythonnet yes cpython An extension of - pythonnet Provides cp ...
- Delphi XE7 call C++ Review of garbled dynamic library
It's because there's a customer who needs to use us C++ Middleware dynamic library to connect and communicate with devices , But both incoming and outgoing string pointers are malformed ( Garbled or truncated ), It's probably a character coding problem . Here's how to solve the problem : We C++ Middleware dynamic library ...
- Use ctypes stay Python Call in C++ Dynamic library
Use ctypes stay Python Call in C++ Dynamic library Getting started Use ctypes Libraries can be called directly C Dynamic library written by language , And if it's a call C++ Write dynamic library , Need to use extern Keyword to declare the function of the dynamic library : #in ...
- C# call C++ Dynamic library considerations
C# call C++ Dynamic library considerations Recent projects need to be in C# Call in C++, During this period, we encountered a lot of pits , Summarized below : 1.in const char* Corresponding C# in string or IntPtr 2.out const ...
- matplotlib Draw animation
matplotlib from 1.1.0 After the release, animation has been supported , Please refer to the official help document for specific use . Here's a very basic example : """ A simple example of an ...
- Sed It's a good thing
The source code encountered today uses abnormal macro , Macro processing of parameters directly , Make my vim The plug-ins are almost completely useless , We can only find a way to replace the macro . #!/bin/bash find . -type f -name '*.c' | xargs ...
- eclipse Manual installation svn and maven
One . to Eclipse install SVN, There are two most common ways : Manual mode and installation wizard mode . The specific steps are as follows : Mode one : Manual installation 1. Download from official website site-1.6.9.zip file , The address is :subclipse.tigri ...
- Marriage Match IV HDU - 3416
The question Here you are. n A little bit ,m side , Each side is required to walk only once S To T The number of shortest paths of Answer key In my life WA also TLE also RE when ,yyb The boss told me to run twice SPFA, And said I wrote it again SPFA It's wrong. , However Slap your face ... And he ...
- Windows Use Visual Studio compile caffe
explain : Recently caffe Found in github Download the source code does not windows Version of , You need to generate your own project files to use Visual Studio compile , Let's record the generation here Windows Project file method and compilation ...
- Excel How to download template information when importing （Java）
As we all know , We are realizing Excel When you upload it , Let's download a template , Then implement the import function . I'm here to record , For subsequent use ... The first thing to think about is This template is for the front desk , And this template comes from this , At first ...
Let's make a bold statement :for Loops are usually useless , It also makes the code hard to understand . When it comes to iterative arrays . Look for the element . Or sort them or anything you think of , There may be an array method that you can use . However , Although these methods are useful , But its ...
- Python subprocess Method
import subprocess #subprocess.call("df -h",shell=True,stdout=subprocess.PIPE)# Print to view , But it can't be saved ...
- python virtualenv Use
1. What is? virtualenv virtualenv It's used for environmental isolation , For example, projects A Used python2, project B Used python3 Use virtualenv Projects can be generated separately A And projects B Environment package of 2.virt ...
- OAF_OAF Control series 1 - Region Type Summary （ Concept ）
2014-06-22 Created By BaoXinjian