none
关于C#中调用DLL文件 RRS feed

  • 问题

  • 这有一段已经可以正常运行的代码,VS2008 下用VB.NET写的:

    Public Class Sensor
      Private Declare Sub ShockproofGetAccelerometerData Lib "sensor.dll" (ByRef data As AccelData)
    
      Private data As AccelData
    
      Public Structure AccelData
        Dim stat As Integer
        Dim LatestRawAccelDataX As Short  ' latest acceleration data of X axis
        Dim LatestRawAccelDataY As Short  ' latest acceleration data of Y axis
        Dim LatestAccelDataX As Short    ' latest acceleration data of X axis (average in 40ms)  <-- Works even better?
        Dim LatestAccelDataY As Short    ' latest acceleration data of Y axis (average in 40ms)  <-- Works even better?
        Dim Temperatureas As Char      ' latest temperature
        Dim LatestZeroG_X As Short     ' latest zero-G offset of X axis  <-- Seems to be the current notion of "center"
        Dim LatestZeroG_Y As Short     ' latest zero-G offset of Y axis  <-- ""
      End Structure
    
      Public Function getData() As AccelData
        ShockproofGetAccelerometerData(data)
        Return data
      End Function
    
    End Class

    现在我要把这个类转到VS2010的C#上来,于是我在VS2010中把代码改成这样:

      class gSensor
      {
        [DllImport("C:\\Windows\\System32\\Sensor.DLL")]
        private extern void ShockproofGetAccelerometerData(ref AccelData data);
    
        AccelData data;
    
        struct AccelData
        {
           Int16 stat;
           short LatestRawAccelDataX;  //* latest acceleration data of X axis
           short LatestRawAccelDataY;  //* latest acceleration data of Y axis
           short LatestAccelDataX;    //* latest acceleration data of X axis (average in 40ms)  <-- Works even better?
           short LatestAccelDataY;    //* latest acceleration data of Y axis (average in 40ms)  <-- Works even better?
           char Temperatureas;      //* latest temperature
           short LatestZeroG_X;     //* latest zero-G offset of X axis  <-- Seems to be the current notion of "center"
           short LatestZeroG_Y;     //* latest zero-G offset of Y axis  <-- ""
        }
    
        private void getData()
        {
          ShockproofGetAccelerometerData(data);
        }
      }

    可是VS2010提示我的这个方法的参数不对:

    请问,这个是怎么回事啊?

    2010年5月4日 3:00

答案

  •  private void getData()
        {
          ShockproofGetAccelerometerData(ref data);
        }

    将getData函数申明如上,在VS2008下没问题。至于2010,目前还未使用。

     

    2010年5月4日 5:44

全部回复

  • 你好!

         你这样修改试试,希望对你有帮助:

      struct AccelData
      {
        int stat;
        short LatestRawAccelDataX; //* latest acceleration data of X axis
        short LatestRawAccelDataY; //* latest acceleration data of Y axis
        short LatestAccelDataX;  //* latest acceleration data of X axis (average in 40ms) <-- Works even better?
        short LatestAccelDataY;  //* latest acceleration data of Y axis (average in 40ms) <-- Works even better?
        char Temperatureas;   //* latest temperature
        short LatestZeroG_X;   //* latest zero-G offset of X axis <-- Seems to be the current notion of "center"
        short LatestZeroG_Y;   //* latest zero-G offset of Y axis <-- ""
      }
    


    周雪峰
    2010年5月4日 5:23
    版主
  •  private void getData()
        {
          ShockproofGetAccelerometerData(ref data);
        }

    将getData函数申明如上,在VS2008下没问题。至于2010,目前还未使用。

     

    2010年5月4日 5:44
  • 谢谢!  
    2010年5月4日 7:41