C#.Net Calling a C++ DLL

0

I got a DLL(without the sourcecode) which exports like this:

?ReceiveCoreDataPtr@@YAXPAX@Z
?xenoAddRigidBodyAngularImpulse@@YAXHMMM@Z
?xenoAddRigidBodyForce@@YAXHMMM@Z
?xenoAddRigidBodyForce@@YAXHMMMMMM@Z
?xenoAddRigidBodyLinearImpulse@@YAXHMMM@Z
?xenoAddRigidBodyPointImpulse@@YAXHMMMMMM@Z
?xenoAddRigidBodyTorque@@YAXHMMM@Z
?xenoCharacterControllerCrouch@@YAXH@Z
?xenoCharacterControllerJump@@YAXH@Z
?xenoCharacterDisable@@YAXH@Z
?xenoCharacterEnable@@YAXH@Z
?xenoDeleteRigidBody@@YAXH@Z
?xenoEnd@@YAXXZ
?xenoGetCameraFOV@@YAKH@Z
?xenoGetCameraPointX@@YAKH@Z
?xenoGetCameraPointY@@YAKH@Z
?xenoGetCameraPointZ@@YAKH@Z
?xenoGetCameraPositionX@@YAKH@Z
?xenoGetCameraPositionY@@YAKH@Z
?xenoGetCameraPositionZ@@YAKH@Z
?xenoGetCharacterControllerHeadPosition@@YAKH@Z
?xenoGetCharacterControllerPositionX@@YAKH@Z
?xenoGetCharacterControllerPositionY@@YAKH@Z
?xenoGetCharacterControllerPositionZ@@YAKH@Z
?xenoGetCharacterControllerRotation@@YAKH@Z
?xenoGetRigidBodyAllowedPenetrationDepth@@YAKH@Z
?xenoGetRigidBodyAngularDamping@@YAKH@Z
?xenoGetRigidBodyAngularVelocityX@@YAKH@Z
?xenoGetRigidBodyAngularVelocityY@@YAKH@Z
?xenoGetRigidBodyAngularVelocityZ@@YAKH@Z
?xenoGetRigidBodyFriction@@YAKH@Z
?xenoGetRigidBodyGravityFactor@@YAKH@Z
?xenoGetRigidBodyLinearDamping@@YAKH@Z
?xenoGetRigidBodyLinearVelocityX@@YAKH@Z
?xenoGetRigidBodyLinearVelocityY@@YAKH@Z
?xenoGetRigidBodyLinearVelocityZ@@YAKH@Z
?xenoGetRigidBodyMass@@YAKH@Z
?xenoGetRigidBodyMaxAngularVelocity@@YAKH@Z
?xenoGetRigidBodyMaxLinearVelocity@@YAKH@Z
?xenoGetRigidBodyPointVelocityX@@YAKHMMM@Z
?xenoGetRigidBodyPointVelocityY@@YAKHMMM@Z
?xenoGetRigidBodyPointVelocityZ@@YAKHMMM@Z
?xenoGetRigidBodyRestitution@@YAKH@Z
?xenoIsRigidBodyALadder@@YAHH@Z
?xenoMakeCamera@@YAXHH@Z
?xenoMakeCharacterController@@YAXHMM@Z
?xenoMakeCharacterController@@YAXHMMM@Z
?xenoMakeCharacterController@@YAXHMMMM@Z
?xenoMakeCharacterController@@YAXHMMMMM@Z
?xenoMakeCharacterController@@YAXHMMMMMM@Z
?xenoMakeCharacterController@@YAXHMMMMMMM@Z
?xenoMakeRigidBodyDynamicBox@@YAXH@Z
?xenoMakeRigidBodyDynamicBox@@YAXHM@Z
?xenoMakeRigidBodyDynamicCapsule@@YAXH@Z
?xenoMakeRigidBodyDynamicCapsule@@YAXHM@Z
?xenoMakeRigidBodyDynamicCylinder@@YAXH@Z
?xenoMakeRigidBodyDynamicCylinder@@YAXHM@Z
?xenoMakeRigidBodyDynamicSphere@@YAXH@Z
?xenoMakeRigidBodyDynamicSphere@@YAXHM@Z
?xenoMakeRigidBodyStaticBox@@YAXH@Z
?xenoMakeRigidBodyStaticCapsule@@YAXH@Z
?xenoMakeRigidBodyStaticCylinder@@YAXH@Z
?xenoMakeRigidBodyStaticSphere@@YAXH@Z
?xenoMakeRigidBodyStaticTriangleMesh@@YAXH@Z
?xenoMakeVehicle@@YAXHH@Z
?xenoMoveCharacterControllerBackward@@YAXH@Z
?xenoMoveCharacterControllerForward@@YAXH@Z
?xenoMoveCharacterControllerLeft@@YAXH@Z
?xenoMoveCharacterControllerRight@@YAXH@Z
?xenoSetCharacterControllerPosition@@YAXHMMM@Z
?xenoSetCharacterControllerRotation@@YAXHM@Z
?xenoSetGravity@@YAXM@Z
?xenoSetGravity@@YAXMMM@Z
?xenoSetRigidBodyAllowedPenetrationDepth@@YAXHM@Z
?xenoSetRigidBodyAngularDamping@@YAXHM@Z
?xenoSetRigidBodyAngularVelocity@@YAXHMMM@Z
?xenoSetRigidBodyAsLadder@@YAXHH@Z
?xenoSetRigidBodyFriction@@YAXHM@Z
?xenoSetRigidBodyGravityFactor@@YAXHM@Z
?xenoSetRigidBodyLinearDamping@@YAXHM@Z
?xenoSetRigidBodyLinearVelocity@@YAXHMMM@Z
?xenoSetRigidBodyMass@@YAXHM@Z
?xenoSetRigidBodyMaxAngularVelocity@@YAXHM@Z
?xenoSetRigidBodyMaxLinearVelocity@@YAXHM@Z
?xenoSetRigidBodyPosition@@YAXHMMM@Z
?xenoSetRigidBodyRestitution@@YAXHM@Z
?xenoSetRigidBodyRotation@@YAXHMMM@Z
?xenoSetTimeStep@@YAXM@Z
?xenoStart@@YAXH@Z
?xenoStart@@YAXHM@Z
?xenoStart@@YAXHMH@Z
?xenoStart@@YAXXZ
?xenoUpdate@@YAXXZ
?xenoVehicleAccelerate@@YAXHM@Z
?xenoVehicleHandbrake@@YAXH@Z
?xenoVehicleReverse@@YAXH@Z
?xenoVehicleTurn@@YAXHM@Z

When I try to use it in another C++ App like this

#include <windows.h>

class XenoPhysics
{
private:
 typedef void (*FunctionFunc)(void);
 typedef void (*FunctionFuncFloat)(float);
 typedef void (*FunctionFuncInt)(int);
 typedef void (*FunctionIntFloatFloatFloat)(int,float,float,float);

 HMODULE libInstance;

public:
 void LoadLib()
 {
  this->libInstance = LoadLibrary(L"F:\\xenophysics.dll");
 }

 void UnloadLib()
 {
  FreeLibrary(libInstance);
 }

 void xStart()
 {
  FunctionFunc curFunc;
  curFunc = (FunctionFunc)GetProcAddress(this->libInstance, "?xenoStart@@YAXXZ");
  curFunc();
 }

 void xEnd()
 {
  FunctionFunc curFunc;
  curFunc = (FunctionFunc)GetProcAddress(libInstance, "xenoEnd");
  curFunc();
 }

 void xUpdate()
 {
  FunctionFunc curFunc;
  curFunc = (FunctionFunc)GetProcAddress(libInstance, "xenoUpdate");
  curFunc();
 }

 void xMakeRigidBodyStaticBox(int objid)
 {
  FunctionFuncInt curFunc;
  curFunc = (FunctionFuncInt)GetProcAddress(libInstance, "xenoMakeRigidBodyStaticBox");
  curFunc(objid);
 }

 void xMakeRigidBodyDynamicBox(int objid)
 {
  FunctionFuncInt curFunc;
  curFunc = (FunctionFuncInt)GetProcAddress(libInstance, "xenoMakeRigidBodyDynamicBox");
  curFunc(objid);
 }

 void xSetRigidBodyPosition(int objid, float x, float y, float z)
 {
  FunctionIntFloatFloatFloat curFunc;
  curFunc = (FunctionIntFloatFloatFloat)GetProcAddress(libInstance, "xenoSetRigidBodyPosition");
  curFunc(objid, x, y, z);
 }

 void xSetGravity(float grav)
 {
  FunctionFuncFloat curFunc;
  curFunc = (FunctionFuncFloat)GetProcAddress(libInstance, "xenoSetGravity");
  curFunc(grav);
 }
};

This is the "wrapper"(or whatever you would call it) class.. To call the functions I'm doing

XenoPhysics * d = new XenoPhysics();
d->LoadLib();
d->xStart();

It then throws the following error at me(Note that it passes the LoadLib() without any errors)

Unhandled exception at 0x50261bc9 in Xeno Test.exe: 0xC0000005: Access violation reading location 0x00000064.

And yes; I've checked that the "curFunc" gets the address, not just a NULL pointer(atleast I think I've checked that)..

Anyone who can help out?

Edit: Forgot the C#.Net code, how stupid of me :3

Here is the C++ CLR DLL "wrapper" I tried to make:

#include <windows.h>
// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the XENOWRAPPERWIN32_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// XENOWRAPPERWIN32_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef XENOWRAPPERWIN32_EXPORTS
#define XENOWRAPPERWIN32_API __declspec(dllexport)
#else
#define XENOWRAPPERWIN32_API __declspec(dllimport)
#endif

typedef void (*FunctionFunc)();
typedef void (*FunctionFuncFloat)(float);
typedef void (*FunctionFuncInt)(int);
typedef void (*FunctionIntFloatFloatFloat)(int,float,float,float);

// This class is exported from the xeno wrapper win32.dll
class XENOWRAPPERWIN32_API Cxenowrapperwin32 {
public:
    Cxenowrapperwin32(void);

    HINSTANCE libInstance;
    // FunctionFunc curFunc;

    // Library base functions
    void LoadLib()
    {
        libInstance = LoadLibrary(L"F:\\xenophysics.dll");
    }

    void UnloadLib()
    {
        FreeLibrary(libInstance);
    }

    // Function calls to the xeno physics
    void xStart()
    {
        FunctionFunc curFunc;
        curFunc = (FunctionFunc)GetProcAddress(libInstance, "?xenoStart@@YAXXZ");
        curFunc();
    }

    void xEnd()
    {
        FunctionFunc curFunc;
        curFunc = (FunctionFunc)GetProcAddress(libInstance, "?xenoEnd@@YAXXZ");
        curFunc();
    }

    void xUpdate()
    {
        FunctionFunc curFunc;
        curFunc = (FunctionFunc)GetProcAddress(libInstance, "?xenoUpdate@@YAXXZ");
        curFunc();
    }

    void xMakeRigidBodyStaticBox(int objid)
    {
        FunctionFuncInt curFunc;
        curFunc = (FunctionFuncInt)GetProcAddress(libInstance, "?xenoMakeRigidBodyStaticBox@@YAXH@Z");
        curFunc(objid);
    }

    void xMakeRigidBodyDynamicBox(int objid)
    {
        FunctionFuncInt curFunc;
        curFunc = (FunctionFuncInt)GetProcAddress(libInstance, "?xenoMakeRigidBodyDynamicBox@@YAXH@Z");
        curFunc(objid);
    }

    void xSetRigidBodyPosition(int objid, float x, float y, float z)
    {
        FunctionIntFloatFloatFloat curFunc;
        curFunc = (FunctionIntFloatFloatFloat)GetProcAddress(libInstance, "?xenoSetRigidBodyPosition@@YAXHMMM@Z");
        curFunc(objid, x, y, z);
    }

    void xSetGravity(float grav)
    {
        FunctionFuncFloat curFunc;
        curFunc = (FunctionFuncFloat)GetProcAddress(libInstance, "?xenoSetGravity@@YAXM@Z");
        curFunc(grav);
    }

};

extern XENOWRAPPERWIN32_API int nxenowrapperwin32;

XENOWRAPPERWIN32_API int fnxenowrapperwin32(void);

and here is how I use it in C#.Net

class xeno
        {
            [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "?LoadLib@Cxenowrapperwin32@@QAEXXZ")]
            public static extern void xLoadLib();
            public void LoadLib()
            {
                xLoadLib();
            }

            [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "?UnloadLib@Cxenowrapperwin32@@QAEXXZ")]
            public static extern void xUnloadLib();
            public void UnloadLib()
            {
                xUnloadLib();
            }

            [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "?xStart@Cxenowrapperwin32@@QAEXXZ")]
            public static extern void xStart();
            public void Start()
            {
                xStart();
            }

            [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "?xUpdate@Cxenowrapperwin32@@QAEXXZ")]
            public static extern void xUpdate();
            public void Update()
            {
                xUpdate();
            }

            [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "?xEnd@Cxenowrapperwin32@@QAEXXZ")]
            public static extern void xEnd();
            public void End()
            {
                xEnd();
            }

            [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "?xMakeRigidBodyDynamicBox@Cxenowrapperwin32@@QAEXH@Z")]
            public static extern void xMakeRigidBodyDynamicBox(int objid);
            public void MakeRigidBodyDynamicBox(int id)
            {
                xMakeRigidBodyDynamicBox(id);
            }

            [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "?xMakeRigidBodyStaticBox@Cxenowrapperwin32@@QAEXH@Z")]
            public static extern void xMakeRigidBodyStaticBox(int objid);
            public void MakeRigidBodyStaticBox(int id)
            {
                xMakeRigidBodyStaticBox(id);
            }

            [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "?xSetGravity@Cxenowrapperwin32@@QAEXM@Z")]
            public static extern void xSetGravity(float grav);
            public void SetGravity(float g)
            {
                xSetGravity(g);
            }

            [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "?xSetRigidBodyPosition@Cxenowrapperwin32@@QAEXHMMM@Z")]
            public static extern void xSetRigidBodyPosition(int obj, float x, float y, float z);
            public void SetRigidBodyPosition(int id, float q, float w, float e)
            {
                xSetRigidBodyPosition(id, q, w, e);
            }
        }

And in the main C#.Net program I do

xeno tx = new xeno();

tx.Start();
tx.SetGravity(-1);

tx.MakeRigidBodyStaticBox(ground.Id);

tx.MakeRigidBodyDynamicBox(cube.Id);
tx.SetRigidBodyPosition(cube.Id, 0, 50, 0);
c++
dll
wrapper
asked on Stack Overflow Jun 9, 2010 by hayer • edited Jun 9, 2010 by hayer

1 Answer

0

You must use proper calling conversion and parameters.

answered on Stack Overflow Jun 9, 2010 by adf88

User contributions licensed under CC BY-SA 3.0