Click here to Skip to main content
14,386,126 members
Rate this:
Please Sign up or sign in to vote.
See more:
I imported a dll file named swedll64.dll in main function of a c++ program.

#include<iostream>
#include<math.h>
#include<windows.h>

using namespace  std;

typedef double(*_swe_julday)(int, int, int, double, int);
typedef int(*_swe_calc)(double, int, int, double[], char[]);
typedef void(*_swe_revjul)(double, int, int&, int&, int&, double&);

int main()
{
HINSTANCE hInst = LoadLibrary(L"C:\\swedll64.dll");

_swe_julday swe_julday = reinterpret_cast<_swe_julday>(GetProcAddress(hInst, "swe_julday"));
_swe_calc swe_calc = reinterpret_cast<_swe_calc>(GetProcAddress(hInst, "swe_calc"));
_swe_revjul swe_revjul = reinterpret_cast<_swe_revjul>(GetProcAddress(hInst, "swe_revjul"));
return 0;
}

public class Swisseph
{
    public Swisseph()
    {
    }
    [DllImport("swedll32.dll", CharSet = CharSet.Ansi, EntryPoint = "swe_julday")]
    public extern static double xyz_swe_julday(int year, int month, int day, double hour, int gregflag);
    public static double swe_julday(int year, int month, int day, double hour)
    {
        return xyz_swe_julday(year, month, day, hour, 1);
    }

    [DllImport("swedll32.dll", CharSet = CharSet.Ansi, EntryPoint = "swe_calc_ut")]
    public extern static int xyz_swe_calc_ut(double tjd_ut, int ipl, int iflag, double[] xx, StringBuilder serr);
    public static void swe_calc_ut(double tjd_ut, int ipl, int addFlags, double[] xx)
    {
        StringBuilder serr = new StringBuilder(256);
        int ret = xyz_swe_calc_ut(tjd_ut, ipl, addFlags, xx, serr);
    }

    [DllImport("swedll32.dll", CharSet = CharSet.Ansi, EntryPoint = "swe_close")]
    public extern static void xyz_swe_close();
}


What I have tried:

 how can i build a class for imported methods?in other word where should i put this codes in class ? 
Is there another tricks to do so like Dllimport() and extern in c#(i want to do exact whatever written in  below c# code)?
Posted
Updated 19-Aug-19 8:22am
Rate this:
Please Sign up or sign in to vote.

Solution 2

Here is one possibility :
typedef double (*_swe_julday)( int, int, int, double, int );
typedef int    (*_swe_calc)( double, int, int, double[], char[] );
typedef void   (*_swe_revjul)( double, int, int&, int&, int&, double& );

class SWE
{
public:
    static inline _swe_julday  JulDay = nullptr;
    static inline _swe_calc    Calc   = nullptr;
    static inline _swe_revjul  RevJul = nullptr;

    static bool Initialize()
    {
        if( m_Initialized )
           return true;

        HINSTANCE hInst = LoadLibrary( _T( "swedll64.dll" ) );
        if( ! hInst )
            return false;

        JulDay = reinterpret_cast<_swe_julday>( GetProcAddress( hInst, "swe_julday" )  );
        Calc = reinterpret_cast<_swe_calc>( GetProcAddress( hInst, "swe_calc" ) );
        RevJul = reinterpret_cast<_swe_revjul>( GetProcAddress( hInst, "swe_revjul" ) );

        m_Initialized = true;
        return true;
    }

    static bool IsInitialized() const    { return m_Initialized; }

protected:
    static inline bool m_Initialized = false;
};
This might look weird because there is some C++17 stuff in there but it will compile and link with VS17. I tried it. To use this the code would like this:
SWE::Initialize();
SWE::JulDay( int1, int2, int3, double1, int4 );
One thing to note is a class with all static methods and members is essentially the same thing as having those functions and data in a namespace. Even the usage syntax is the same.
   
v4
Comments
Rick York 20-Aug-19 2:44am
   
If that library was written in C this solution needs to be adjusted a bit.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You have build a class for it, because the imported function are static or global.

The usage in C# is now
Swisseph.xyz_swe_julday(...);
because you renamed them. It isnt a good idea.

Ofcourse you should consider build some class which are using the functions. Some formatting, checking or buffer providing is very handy.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100