2010-06-09 245 views
0

我有一个DLL(不包括源代码),其中出口是这样的:C#.NET调用C++ DLL

[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@YAXXZ 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@YAXXZ 
[email protected]@YAXXZ 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 
[email protected]@[email protected] 

当我尝试使用它在另一个C++应用程序像这样

#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, "[email protected]@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); 
} 
}; 

这是“包装”(或任何你称呼它)类。要我叫我做

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

然后引发以下错误的功能(注它通过LoadLib()没有任何错误)

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

是的;我检查过“curFunc”得到的地址,不只是一个NULL指针(至少我认为我已经检查过)..

任何人都可以帮忙吗?

编辑:忘记了C#.NET代码,是我的错:3

这里是C++ CLR DLL “包装” 我试图让:

#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, "[email protected]@YAXXZ"); 
     curFunc(); 
    } 

    void xEnd() 
    { 
     FunctionFunc curFunc; 
     curFunc = (FunctionFunc)GetProcAddress(libInstance, "[email protected]@YAXXZ"); 
     curFunc(); 
    } 

    void xUpdate() 
    { 
     FunctionFunc curFunc; 
     curFunc = (FunctionFunc)GetProcAddress(libInstance, "[email protected]@YAXXZ"); 
     curFunc(); 
    } 

    void xMakeRigidBodyStaticBox(int objid) 
    { 
     FunctionFuncInt curFunc; 
     curFunc = (FunctionFuncInt)GetProcAddress(libInstance, "[email protected]@[email protected]"); 
     curFunc(objid); 
    } 

    void xMakeRigidBodyDynamicBox(int objid) 
    { 
     FunctionFuncInt curFunc; 
     curFunc = (FunctionFuncInt)GetProcAddress(libInstance, "[email protected]@[email protected]"); 
     curFunc(objid); 
    } 

    void xSetRigidBodyPosition(int objid, float x, float y, float z) 
    { 
     FunctionIntFloatFloatFloat curFunc; 
     curFunc = (FunctionIntFloatFloatFloat)GetProcAddress(libInstance, "[email protected]@[email protected]"); 
     curFunc(objid, x, y, z); 
    } 

    void xSetGravity(float grav) 
    { 
     FunctionFuncFloat curFunc; 
     curFunc = (FunctionFuncFloat)GetProcAddress(libInstance, "[email protected]@[email protected]"); 
     curFunc(grav); 
    } 

}; 

extern XENOWRAPPERWIN32_API int nxenowrapperwin32; 

XENOWRAPPERWIN32_API int fnxenowrapperwin32(void); 

,这里是我如何使用在C#.NET

class xeno 
     { 
      [DllImport("C:\\Users\\hayer\\Documents\\Visual Studio 2008\\Projects\\xeno wrapper win32\\Debug\\xeno wrapper win32.dll", EntryPoint = "[email protected]@@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 = "[email protected]@@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 = "[email protected]@@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 = "[email protected]@@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 = "[email protected]@@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 = "[email protected]@@[email protected]")] 
      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 = "[email protected]@@[email protected]")] 
      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 = "[email protected]@@[email protected]")] 
      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 = "[email protected]@@[email protected]")] 
      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程序我

xeno tx = new xeno(); 

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

tx.MakeRigidBodyStaticBox(ground.Id); 

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

你的例子中的c#和.net在哪里? – nothrow 2010-06-09 16:09:58

+0

我会建议加载OllyDbg中的程序,看看究竟发生了什么错误。或者,如果您不知道ASM,只需在工作demo + source + .dll的某处发布即可,我会研究它。 – Paya 2010-06-09 16:16:23

+0

我不明白现在如何使用OllyDbg:/ – hayer 2010-06-09 16:20:21

回答

0

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

+0

你能举个快速的例子吗? 或者一些信息的链接? – hayer 2010-06-09 16:33:22