這個dll中有一個類,類裡面有幾個函數:
// This class is exported from the testdrv.dll
class TESTDRV_API CDeviceManage {
public:
ULONG GetDevHandle(ULONG para1,ULONG para2,ULONG para3);
int GetDeviceState(char Volume);
bool UnloadDeviceDriver();
bool LoadDeviceDriver();
char * GetDevicesByType(int devtype);
char * GetAllDevices(void);
bool SetDeviceState(char Volume,int State);
CDeviceManage(void);
private:
HANDLE lphDevice;
};
我現在在c#裡面調用,寫成如下的樣子:
public class DeviceManager
{
    [DllImport("testdrv.dll", EntryPoint="?LoadDeviceDriver@CDeviceManage@@QAE_NXZ")]
     public static extern bool LoadDeviceDriver();    [DllImport("testdrv.dll", EntryPoint="?UnloadDeviceDriver@CDeviceManage@@QAE_NXZ")]
     public static extern bool UnloadDeviceDriver();
    
    [DllImport("testdrv.dll", EntryPoint="?GetDevHandle@CDeviceManage@@QAEKKKK@Z")]
    public static extern ulong GetHandle(ulong i,ulong j,ulong k);
    //...
}
EntryPoint是我通過工具Dependency Walker來看到的函數名字.-----------------------------
現在的問題是:
1.我給GetHandle傳入值1,2,3,但debug vc的代碼發現得到的值卻變成了1,0,2  這真是讓人費解.
2.我掉用了LoadDeviceDriver來load一個diver 程序,返回的結果表示load成功了,但是這個時候我掉用
GetHandle(這個方法只是簡單地返回一個變量的值而已),就會報錯:未將對象引用到實例.
c#代碼:
bool success = DeviceManager.LoadDeviceDriver();
Assert.IsTrue(success, "Load Device Driver Failed");
ulong l = DeviceManager.GetHandle(1,2,3);
DeviceManager.UnloadDeviceDriver();哪位能告訴我這是怎麼回事? 我已經快被折磨死了!!!

解决方案 »

  1.   

    猜猜: 既然報錯:未將對象引用到實例,也许GetHandle是个实例方法.  静态方法才能像你这样调用吧.
    试试看吧.DeviceManager dm =new DeviceManage();
    ulong l = dm.GetHandle(1,2,3);
      

  2.   

    我從dll中調用api,我在上面聲明為public static extern 的.我最感到奇怪的是,傳入的值,為何到了dll中就變了呢?
      

  3.   

    是否是调用C++编写的标准DLL?
      

  4.   

    是怎么回事啊,楼主说说怎么解决的好不?正在用vc写一个要供c#调用的dll,急学习!
      

  5.   

    推薦一個wiki
    http://www.pinvoke.net