【问题标题】:"Unable to find an entry point named 'connect2' in DLL "xy"“无法在 DLL“xy”中找到名为“connect2”的入口点
【发布时间】:2021-08-05 00:50:30
【问题描述】:

我知道还有很多其他类似的问题,但没有人帮助我。 所以我在 C++ 中为我的 mysql 连接编写了一个 dll,以便稍后在 C# 中使用它。但它告诉我它找不到我的方法的入口点。

有什么想法吗?

这是我的 C++ 代码:

Sweepape.h

#ifdef SWEEPAPE_EXPORTS
#define SWEEPAPE_API __declspec(dllexport)
#else
#define SWEEPAPE_API __declspec(dllimport)
#endif

extern "C" {

    //// Diese Klasse wird aus der DLL exportiert.
    //class SWEEPAPE_API CSweepape {
    //public:
    //  CSweepape(void);
    //  // TODO: Methoden hier hinzufügen.
    //};
    //
    //extern SWEEPAPE_API int nSweepape;
    //extern SWEEPAPE_API int nYear;
    //
    //SWEEPAPE_API int fnSweepape(void);
    
    SWEEPAPE_API int connect2(int a);
    
}

Sweepape.cpp

#include "Sweepape.h"

SWEEPAPE_API int connect2(int a)
{
    std::cout << "Connecting" << a;
    return 0;
}

C#

using System;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    class Program
    {
        private const string path = @"C:\Users\chris\source\repos\Sweepape\Debug\Sweepape.dll";


        [DllImport(path, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
        public static extern int connect2(int a);

        static void Main(string[] args)
        {
            connect2(2);

        }
    }
}

【问题讨论】:

  • 当您在 C# 中使用 EF.Core 时,为什么还要使用 C++ 中的 MySql?
  • 我只是在尝试,c++中还有其他功能。
  • 所以您似乎在问为什么您的 C# 代码无法加载您的 c++ DLL,但您没有显示尝试这样做的 C# 代码?
  • 对不起,我的错。我忘记了。
  • 我添加了 C# 代码。

标签: c# c++ dll entry-point


【解决方案1】:

您在这里并没有真正为我们提供太多帮助,因此最可能的原因可能是:

  1. 您的 C++ 库是 32 位的,而您的 C# 应用程序是 64 位的,因为这些是默认设置。你需要两者都是相同的“位”。

  2. 那条硬编码的路径是一个尖叫的危险信号。正确设置您的环境,没有人会这样使用您的代码。包括你,你可以清楚地看到。

  3. 根据您的编译器和设置,您在调用约定上撒谎。由于您没有在 C++ 代码中明确编写调用约定,也没有显示任何有用的东西(例如您实际编译该代码的方式),因此很难说。

  4. 这里不太可能是一个问题,但你绝对是在撒谎你的 C++ 字符集。里面没有 unicode 代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多