Specify ordinals of C ++ exported functions in DLL

I am writing a DLL with mixed C / C ++ code. I want to specify the ordinals of the functions that I am exporting. So I created a .DEF file that looks like this:

LIBRARY LEONMATH

EXPORTS

    sca_alloc   @1
    vec_alloc   @2
    mat_alloc   @3

    sca_free    @4
    vec_free    @5
    mat_free    @6

    ...

      

I would like to specify the ordinals of my C ++ class functions and methods. I tried using Dependency Walker to add my changed function names to the .DEF file:

    ??0CScalar@@QAE@XZ      @25
    ??0CScalar@@QAE@O@Z     @26
    ??0CScalar@@QAE@ABV0@@Z @27
    ??1CScalar@@QAE@XZ      @28

      

But it failed. Any ideas why this might be happening?


EDIT: kauppi made a good observation, so I'm adding more information to the question.

  • Platform: Windows (and I'm not interested in portability)
  • Compiler: Microsoft C ++ Compiler (I am using VS2005)
  • Why do I want to do this? Using ordinals has the advantage of allowing me to call exported C ++ functions from within C code.
0


source to share


2 answers


Well, I have no experience with ordinals (which look ugly, compiler specific), but I can help you with C ++ / C compatibility.

Suppose in C ++ your header file looks like this:

class MyClass
{
    void foo(int);
    int bar(int);
    double bar(double);
    void baz(MyClass);
};

      

You can make it C compatible by doing the following:



#ifdef __cplusplus
#define EXTERN_C extern "C"
// Class definition here; unchanged
#else
#define EXTERN_C
typedef struct MyClass MyClass;
#endif

EXTERN_C void MyClass_foo (MyClass*, int);
EXTERN_C int MyClass_bar_int (MyClass*, int);
EXTERN_C double MyClass_bar_double (MyClass*, double);
EXTERN_C void MyClass_baz (MyClass*, MyClass*);

      

In your C ++ source file, you simply define various functions extern "C"

to navigate to the required member functions like this (this is just one, and the others work similarly)

extern "C" void MyClass_foo (MyClass* obj, int i)
{
  obj->foo(i);
}

      

The code will then have a C interface, with no need to modify the C ++ code at all (other than the declarations in the header, but they can also be moved to another file "myclass_c.h"

or the like). All functions declared / defined extern "C" will not be mangled, so you can easily perform other operations on them. You will also probably need functions to create / destroy instances of MyClass (you can of course use new

/ for this delete

).

+4


source


You said, โ€œUsing ordinals has the advantage of allowing me to call exported C ++ functions from C code.โ€ I regret this is not true.

C ++ class member functions have a special calling convention that requires an invisible this value passed in an implementation-specific register / parameter. And also you need an instance of a class, which you cannot execute in C.



The only thing I know is a faster dynamic link DLL and a smaller import table. Just check the mfc70.dll in the system32 directory with the dependency walker.

+2


source







All Articles