2010-06-09 103 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