0

I imported a dll file named swedll64.dll in main function of a c++ program 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#?

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

I add this simple code below for C#.this is exactly what i want to do in c++

    public class Swisseph
    {
    Swisseph() { }
     ~Swisseph() { }
    [DllImport("swedll64.dll", CharSet = CharSet.Ansi, EntryPoint = "swe_julday")]
    private extern static double _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 _swe_julday(year, month, day, hour, 1);
    }
    }
14
  • Sorry, your question is confusing, what has C# has to do with writing this code? And what way did the Dll get build and expose it's symbols? Commented Aug 18, 2019 at 6:02
  • There nothing you can do to put those functions in a class. But you can call those functions from a class if that's what you want to do. Just call them like regular functions. Commented Aug 18, 2019 at 6:14
  • I want to call them from a class . Anyway shouldn't put them in a class as static methods in order to call them? Commented Aug 18, 2019 at 6:21
  • @Wolf You can call them from a static method if that's what you want to do. You can call them from a regular method if that's what you want to do. You can call them from outside a class if that's what you want to do. They are just functions (strictly they are function pointers), treat them like any other function. But you cannot put them in a class, you can call them from a class. Commented Aug 18, 2019 at 6:37
  • @john this file has about hundred of functions and i don't want to copy thousands of code lines in main() each time i want to use them in my programs.Clearly the question is how can i call them from a separate class file?I don't think the question be so strange Commented Aug 18, 2019 at 6:59

2 Answers 2

1

This is how you would import a function using compiler extension which seems to be what you're asking in the comments.

extern "C" {
    __declspec(dllimport) long IoCreateDriver
    (
        UNICODE_STRING* driver_name,
        DRIVER_INITIALIZE* initialization_fn
    );
}
Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you want to use a C library in c++.

The best way of doing this is by using extern "C" as indicated on the isocpp website

To copy the example:

// This is C++ code
extern "C" {
  // Get declaration for f(int i, char c, float x)
  #include "my-C-code.h"
}
int main()
{
   f(7, 'x', 3.14);   // Note: nothing unusual in the call
  // ...
}

Off course you need to update this based on your lib needs. Though it allows you to use the functions as free functions like you are used to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.