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

C # how to call the DLL of C++

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

Share

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

This article mainly explains "how c # calls C++ 's DLL". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "c # how to call C++ 's DLL".

C # is a managed code, and the created objects are automatically recycled. C++ is unmanaged code, the created objects need to be manually recycled (sometimes not manually recycled, memory overflow problems may occur).

There are two ways for C # to call C++: (1) managed calls and (2) unmanaged calls.

1. It is called in a managed way, just like the dll that normally calls c#

Create a new C++ project

The code in Function.h, a method that returns the sum of two numbers, a method that returns a string

# pragma once#include public ref class Function {public: Function (void); ~ Function (void); int menber; int menberFuncAdd (int aline int b); System:: string ^ say (System:: string ^ str);}; / .CPP # include "Function.h" Function::Function (void) {} Function::~Function (void) {} int Function::menberFuncAdd (int aline int b) {return astring;} System:: string ^ Function::say (System:: string ^ str) {return str;}

There is no need to write blank space in Function.h.

# include "Function.h"

Note: C++ projects must choose common language runtime support

Reference in the project of c # like the dll of c #

Call in the code

Function fun = new Function (); int a = fun.menberFuncAdd (1,2); string s = fun.say ("Hello World")

Note: x86 must be selected for the c # project, otherwise an error will be reported.

Running effect:

two。 Call in an unmanaged manner

Create a new C++ project

Code in stdafx.h

/ / stdafx.h: the include file of the standard system include file / / or frequently used but infrequently changed / / project-specific include files / / # pragma once # include "targetver.h" # ifdef A_EXPORTS#define DLL_API _ _ declspec (dllexport) # else#define DLL_API _ _ declspec (dllimport) # endif#define WIN32_LEAN_AND_MEAN / / exclude rarely used information from Windows header files / / Windows header files: # include extern "C" DLL_API void MessageBoxShow () / / TODO: refer to other header files required by the program here

Code in dllmain.cpp

# include "stdafx.h" BOOL APIENTRY DllMain (HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {switch (ul_reason_for_call) {case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break;} return TRUE } # ifdef _ MANAGED#pragma managed (push, off) # endif void MessageBoxShow () {MessageBox (NULL, TEXT ("Hello World"), TEXT ("In a DLL"), MB_OK);} # ifdef _ MANAGED#pragma managed (pop) # endif

Note: C++ projects must choose common language runtime support

Add to the code

[DllImport ("ll.dll")] public extern static void MessageBoxShow ()

Note: x86 must be selected for the c # project, otherwise an error will be reported.

Running result:

Thank you for your reading, the above is the content of "how c # calls C++ 's DLL". After the study of this article, I believe you have a deeper understanding of how c # calls C++ 's DLL, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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