小弟再次打擾各位了
我建立了一個c++的專案,需要與另一個c# framework net4.7.2 的專案做api傳值呼叫
因此我將C#生成DLL,再由C++這邊做處理
我嘗試了以下方法
1.微軟的CLRCreateInstance API<metahost.h> ExecuteInDefaultAppDomain()
2./CLR 由於我C++專案不支援所以作罷
3.嘗試import".dll" tlb tlh 等
4.using .dll
5.使用HMODULE hDll = LoadLibraryA(".dll");
6.嘗試使用/CLI 調用C#DLL庫
最後我還是無法成功調用C#DLL庫,看看各位大大能有可以調用C#DLL庫的專案或範例
(我的環境是visual studio 2022 x64 to 2010)
問 ChatGPT 得如下:
To refer to a C# DLL in a C++ project, you typically use a technique called Platform Invocation Services (P/Invoke) or COM Interop. The specific approach you choose depends on whether the C# DLL is a .NET DLL or a COM DLL.
Method 1: Referencing a .NET C# DLL in a C++ Project using P/Invoke:
If your C# DLL is a .NET assembly, you can use P/Invoke to access its functionality from a C++ project. Here are the steps:
Compile the C# DLL as a .NET Assembly:
Make sure your C# DLL is compiled as a .NET assembly with functions marked as extern
and decorated with DllImport
attribute.
using System;
using System.Runtime.InteropServices;
public class MyCSharpClass
{
[DllImport("MyCSharpDLL.dll")]
public static extern int MyFunction();
}
Use P/Invoke in C++:
In your C++ code, you can use the extern
keyword to declare the function from the C# DLL and use it as if it were a native C++ function.
#include <windows.h>
extern "C" {
int MyFunction(); // Declaration of the C# function
}
int main() {
int result = MyFunction(); // Call the C# function
return 0;
}
Compile the C++ Project:
Ensure that you properly link your C++ project with the C# DLL (.lib
file or reference to the DLL).
Run the Application:
Make sure that the C# DLL is in the same directory as the C++ executable or is available in the system's PATH.
Method 2: Referencing a COM C# DLL in a C++ Project:
If your C# DLL is a COM DLL, you can use COM Interop to access it from a C++ project. Here are the steps:
Expose Your C# DLL as a COM Component:
In your C# project, make sure your classes and functions are ComVisible and that you register the assembly for COM Interop. You can do this by adding the [ComVisible(true)]
attribute to the relevant classes and registering the assembly using the regasm.exe
tool.
Generate a Type Library:
Use the tlbexp.exe
tool to generate a type library from your C# DLL. This step is essential for importing the types into your C++ project.
tlbexp MyCSharpDLL.dll
Use the Generated Type Library in C++:
In your C++ project, you can import the type library using the #import
directive.
#import "MyCSharpDLL.tlb" raw_interfaces_only
int main() {
CoInitialize(NULL);
MyCSharpClassPtr pMyClass(__uuidof(MyCSharpClass));
pMyClass->MyFunction();
CoUninitialize();
return 0;
}
Compile the C++ Project:
Ensure that you properly link your C++ project with any required libraries and the COM DLL.
Run the Application:
Make sure the C# DLL (COM DLL) is registered or available on the system.
Choose the appropriate method based on whether your C# DLL is a .NET assembly or a COM DLL.
我之前是用DllExport來讓Java透過JNA呼叫C# dll function,我沒研究過C++