iT邦幫忙

0

C++調用C#DLL庫函數問題?

  • 分享至 

  • xImage

小弟再次打擾各位了
我建立了一個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)

wdchiang iT邦新手 2 級 ‧ 2023-09-20 07:38:02 檢舉
你的 Visual Studio 版本有點舊,.NET Framework 4.7.2 release time Apr 30, 2018
alien663 iT邦研究生 3 級 ‧ 2023-09-20 09:05:48 檢舉
Visual Studio 2010才到 .net framework 4
https://learn.microsoft.com/zh-tw/visualstudio/releasenotes/vs2010-sp1-vs#details-improvements-in-this-release
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2023-09-20 17:04:00

問 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:

  1. 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();
    }
    
  2. 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;
    }
    
  3. Compile the C++ Project:
    Ensure that you properly link your C++ project with the C# DLL (.lib file or reference to the DLL).

  4. 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:

  1. 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.

  2. 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
    
  3. 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;
    }
    
  4. Compile the C++ Project:
    Ensure that you properly link your C++ project with any required libraries and the COM DLL.

  5. 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.

0
shootingstar
iT邦新手 4 級 ‧ 2023-09-22 18:27:54

我之前是用DllExport來讓Java透過JNA呼叫C# dll function,我沒研究過C++

我要發表回答

立即登入回答