C# 代码没有调用它应该调用的 c++ dll 函数(它有一个类指针作为参数),我不应该更改 c++ 代码

标签 c# c++ visual-c++ interop marshalling

下面提到的是我在c++中的dll函数

static bool t72CalculateRMDMethodResult( const double     AccountBalanceAtSetup,
                                     const long       ClientCurrentAge,
                                     const char       Frequency,
                                     double *         pRMDMethodResult,
                                     IRSLookupTable & LookupTable)

下面提到的是我的 C++ 类,它有一个指针作为参数传递给上述函数

class IRSLookupTable
{

    public:
        struct IRSLookupTableRow
        {
            unsigned int    Key;
            double          Value;
        };

        IRSLookupTable( const char * pFilename );
        IRSLookupTable( const struct IRSLookupTableRow Table[], const unsigned int NumEntries );
        virtual ~IRSLookupTable();

        bool         LookupValueByKey(const unsigned int Key, double * Value);
        virtual bool ParseLine( char * Buffer, unsigned int * Key, double * Value);
        bool         IsInitialized();

    private:
        typedef map <unsigned int, double> IRSLookupTableData;

        IRSLookupTableData m_IRSTableData;
        bool               m_bInitialized;
};

下面是我如何在c#中调用c++ dll函数,我不确定它是否正确,我无法进入dll函数

[DllImport("t72CalculatorDLL.dll", ExactSpelling = true, EntryPoint = "t72CalculateRMDMethodResult")]
    public static extern bool t72CalculateRMDMethodResult(double AccountBalanceAtSetup, int ClientCurrentAge, char Frequency, double* pRMDMethodResult, [MarshalAs(UnmanagedType.LPStruct)] ref IRSLookupTable LookupTable);

这是我用c#写的c++类的定义

    [DataContract]
public unsafe class IRSLookupTable
{
    public struct IRSLookupTableRow
    {
        public uint Key;
        public double Value;
    };

    public IRSLookupTable()
    {
        m_bInitialized = true;
    }
    public IRSLookupTable(char* pFilename)
    {
        //        uint Key;
        //        double Value;
        //        m_bInitialized = false;

        //        Debug.Assert( pFilename );
        //        if (pFilename ==null )
        //{
        //    // return without setting the flag to true
        //    return;
        //}

        //// open the file
        //std::ifstream InputFile(pFilename);
        //if ( ! InputFile )
        //{
        //    // return without setting the flag to true
        //    return;
        //}

        //while ( InputFile.getline( &gTmpBuffer[0], BUFFERSIZE) )
        //{
        //    if ( ! ParseLine( gTmpBuffer, &Key, &Value ) )
        //    {
        //        m_IRSTableData[Key] = Value;
        //    }
        //    else
        //    {
        //        // return without setting the flag to true
        //        return;
        //    }
        //}

        //m_bInitialized = true;
    }
    public IRSLookupTable(IRSLookupTableRow* Table, uint NumEntries)
    {
        m_bInitialized = false;

        for (uint i = 0; i < NumEntries; i++)
        {
            m_IRSTableData[Table[i].Key] = Table[i].Value;
        }

        m_bInitialized = true;
    }

    ~IRSLookupTable() { }

    public bool LookupValueByKey(uint Key, double* Value) { return true; }
    public virtual bool ParseLine(char* Buffer, uint* Key, double* Value) { return true; }
    public bool IsInitialized() { return true; }


    private SortedDictionary<uint, double> m_IRSTableData;
    private bool m_bInitialized;
}

谁能帮我解决这个问题,我是 c# 的新手。

最佳答案

我没看到你在哪里导出 C++ 函数???您将 t72...func 定义为“静态”???您希望它只在定义它的 .cpp 文件中可见???

我建议将您的 C++ 函数定义为:

extern "C" bool WINAPI t72CalculateRMDMethodResult( const double     AccountBalanceAtSetup,
                                     const long       ClientCurrentAge,
                                     const char       Frequency,
                                     double *         pRMDMethodResult,
                                     IRSLookupTable & LookupTable)

编辑您的 .def 文件并为 t72CalculateRMDMethodResult 添加一个导出条目。

您可以使用“dumpbin/exports mydll.dll”来确定函数是否正确导出。 (显然用你的 dll 替换 mydll.dll)

关于C# 代码没有调用它应该调用的 c++ dll 函数(它有一个类指针作为参数),我不应该更改 c++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10536349/

相关文章:

JAVA - 正则表达式模式不匹配。 regex101.com 中的匹配项

c++ - 如何配置Clion来编译gtkmm项目

c++ - 为什么 decltype 在这里工作,但不是 auto?

c++ - 在 C++ 中编译 C 库。链接问题。穆PDF

visual-c++ - 如何将字符串传递给 CreateProcess 函数?

c# - 当我在 MVC View 中单击图像时如何打开另一个页面

c# - 确定是否设置了特定标志的最佳表达式是什么

c# - LINQ 连接字符串位置

c++ - 如何制作一个无边框的winform应用程序?

c++ - 为什么我的代码会产生语法错误?