int result = reader.CloseDeviceCS(device);
//reader类的代码 函数
[DllImport("MMREADER.dll")]
 private static extern int CloseDevice(int handle);public int CloseDeviceCS(int DeviceHandle)
        {
            int ret = CloseDevice(DeviceHandle);            
            return ret;
        }
这样调用的时候就说内存受保护,
高手快来指点迷津

解决方案 »

  1.   

    private static extern int CloseDevice(int handle);
    ====================
    函数原型???,举错误提示应该是你传入的参数有误
    问的没头没尾,让别人如何答
      

  2.   

    to hdt(倦怠):
    DLL里面的函数如下
    CANDevice *device;
    int ret = GetCANDevice(handle, &device);
    if (ret == MM_OK)
             {
              if (!device->Running) return ERR_DEVICENOTOPEN;
              device->Running = false;
              if (WaitForSingleObject(device->RecvHandler, 2000) == WAIT_OBJECT_0)    {
                  if (device->RecvHandler != INVALID_HANDLE_VALUE)
                  CloseHandle(device->RecvHandler);
                  VCI_CloseDevice(device->DevType, device->DevIndex);      if (device->RecvSem != INVALID_HANDLE_VALUE) 
                     CloseHandle(device->RecvSem);
            DeleteCriticalSection(&device->Critical);
            delete device;
            DeviceList[handle] = NULL;
            return MM_OK;
    }
    return ERR_THREADRUNNING;
    }
    return ret;
    }
      

  3.   

    dll c++ 
    函数原型
      

  4.   

    int ret = CloseDevice(DeviceHandle);执行到这里就异常
      

  5.   

    dll 函数在C++里如何声明
      

  6.   

    这样申明看看[DllImport("MMREADER.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    private static extern int CloseDevice(int handle);
      

  7.   

    To dll 函数在C++里如何声明MMREADER_API int __stdcall CloseDevice(int handle);
      

  8.   

    错误信息
    Attempted to read or write protected memory
      

  9.   

    要调试c++ DLL才能得到准确的错误,你一直在调试C#代码调试c++ DLL,选择程序->你的C#程序
      

  10.   

    在Dotnet中内存的释放是自动的。如果你将一个对象传递给外部API函数,在此函数执行完毕后,如果此对象的引用仍然保存在外部DLL中,那么此时有可能引发异常。原因是这个对象在Dotnet中已经被标记为释放,而在DLL中伋然去访问它。    这是原因