简体   繁体   中英

C# code is not calling the c++ dll function (which has a class pointer as parameter) that it is supposed to call and i should not change the c++ code

Below mentioned is my dll function in c++

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

Below mentioned is my c++ class which has a pointer passed as a parameter to the above function

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;
};

Below is how i called the the c++ dll function in c# and i am not sure it is correct or not and i am unable to enter the dll function

[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);

this the definition of the c++ class that i have written in 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;
}

can anyone please help me with this i am totally new to c#.

I don't see where you export the C++ function??? You define the t72... func as "static" ??? You want it to only be visible in the .cpp file it is defined in???

I suggest defining your C++ func as:

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

Edit your .def file and add an export entry for t72CalculateRMDMethodResult.

You can use "dumpbin /exports mydll.dll" to determine if the function was exported properly. (obviously substitute your dll for mydll.dll)

If you can't change the C++ code, then you may be able to write a C++/CLI layer that will expose it to the C# code. This way you can avoid any ugly DLL imports and exports (I never learned how to do that).m With this technique you can leave your C++ code completely untouched.

I'm assuming you're using at least VS2005 since you mentioned C#.

Check out this post C++/CLI Mixed Mode DLL Creation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM