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 create a dynamic library in C++ to call C#

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to create dynamic library C# calls in C++. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Modification of C++ dynamic library

First of all, open the header file of Cppdll.h, in which we define a callback function

Typedef int (* cb) (int, int)

Then add the calling cb pointer to the declared export function

Extern "C" int Cppdll_API call_func (cb callback, int a, int b)

The figure below is as follows

Then we write the implementation method of call_func in the source file of Cppdll.cpp, the method is also very simple, that is, first find the sum and difference of the input values of the two int types, and then pass these two values back as callback parameters.

Int Cppdll_API call_func (cb callback, int a, int b)

{

Int c = AddCount (a, b)

Int d = abs (a-b)

Return callback (c, d)

}

In this way, we have completed the dynamic library of C++.

Modification of the calling program of C #

First write the calling function declaration of C++ dynamic library

[DllImport ("Cppdll", EntryPoint = "call_func"

CallingConvention = CallingConvention.StdCall)]

Public static extern int CallFun (Dllcallback dcb, int num1, int num2)

[UnmanagedFunctionPointer (CallingConvention.Cdecl)]

Public delegate int Dllcallback (int num1, int num2)

The above Dllcallback is the callback function we defined, which must be defined by delegate. The following [CallingConvention.Cdecl in UnmanagedFunctionPointer] must be written, because the default pointer of C++ is Cdecl. If it is not declared here, it will default to _ stdcall. When calling the dll function dynamically, prompt Run-Time Check Failure # 0-The value of ESP was not properly saved across a function call.

Then we write a callback method.

Public int Call (int a, int b)

{

TextBox1.AppendText ("the first parameter of the callback function is:" + a + "\ r\ n")

TextBox1.AppendText ("the second parameter of the callback function is:" + b + "\ r\ n")

Return a + b

}

Finally, the implementation method of calling C++ dynamic library is written at the end of the original button event.

TextBox1.AppendText ("call C++ dynamic library call_func callback function\ r\ n")

Num = CallFun (Call, num1, num2)

TextBox1.AppendText ("get the sum of two numbers:" + num + "\ r\ n")

At this point, the modification of the C# calling program has been completed.

Run the program to see the effect.

The above is the editor for you to share how to create dynamic library C# called in C++, if you happen to have similar doubts, you may wish to refer to the above analysis to understand. If you want to know more about it, 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

Internet Technology

Wechat

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

12
Report