首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#.Net调用C++ DLL

C#.Net调用C++ DLL
EN

Stack Overflow用户
提问于 2010-06-10 00:02:56
回答 1查看 668关注 0票数 0

我得到了一个DLL(没有源代码),它像这样导出:

代码语言:javascript
复制
?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

当我尝试在另一个类似的C++应用程序中使用它时

代码语言:javascript
复制
#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);
 }
};

这就是“wrapper”(或者你怎么称呼它)类..调用我正在做的函数

代码语言:javascript
复制
XenoPhysics * d = new XenoPhysics();
d->LoadLib();
d->xStart();

然后它抛出下面的错误(注意,它传递LoadLib()时没有任何错误)。

代码语言:javascript
复制
Unhandled exception at 0x50261bc9 in Xeno Test.exe: 0xC0000005: Access violation reading location 0x00000064.

是的,我已经检查了"curFunc“是否获得了地址,而不仅仅是一个空指针(至少我想我已经检查过了)..

有人能帮上忙吗?

编辑:忘记了C#.Net代码,我有多傻:3

下面是我尝试制作的C++ CLR DLL“包装器”:

代码语言:javascript
复制
#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);

下面是我如何在C#.Net中使用它

代码语言:javascript
复制
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);
            }
        }

在主C#.Net程序中,我这样做了

代码语言:javascript
复制
xeno tx = new xeno();

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

tx.MakeRigidBodyStaticBox(ground.Id);

tx.MakeRigidBodyDynamicBox(cube.Id);
tx.SetRigidBodyPosition(cube.Id, 0, 50, 0);
EN

回答 1

Stack Overflow用户

发布于 2010-06-10 00:22:44

您必须使用正确的调用转换和参数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3007616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档