2011-09-29 77 views
1

我被一个问题困住了两天没能解决。在c#中声明一个指针指向c中的非托管结构dll

错误消息看起来如下:

无法获取的地址,获取的大小,或宣布一个指向托管类型 (“Control_Vertilon.Imports.PHOTONIQ_CONFIG_TABLE”)

下面是代码,

  UInt16[] Arguments = new UInt16[PHOTONIQ_ARG_BUFF_SZ]; 

     PHOTONIQ_CONFIG_TABLE PhotoniqConfigTable = new PHOTONIQ_CONFIG_TABLE(); 

     UInt16 *cfgPtr = (UInt16*)&PhotoniqConfigTable;  

     for(int i = 0; i < Imports.CONFIG_TABLE_SZ; i++){ 
      Arguments[i+1] = *cfgPtr++; 
     } 

     Imports.ControlInterface(Opcode, Arguments, 0x1, Imports.TimeoutMs, ref errInNoErrorStruct, ref NumRetArguments, ref PhotoniqConfigTable, Imports.CONFIG_TABLE_SZ, ref errOutStruct); 

在那里,我封送PhotoniqConfigTable看起来如下,

 [StructLayout(LayoutKind.Sequential)] 
     unsafe public struct PHOTONIQ_CONFIG_TABLE 
     { 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SystemMode;  //Indicates current system mode, acquire or standby mode 0 = Standby Mode 1 = Acquire Mode 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVLimit0; // Maximum allowed voltage on HV supply 1 Range = 500 ?9250 (50 ?925V) 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVLimit1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 NumChannelsB0; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 NumChannelsB1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 NumChannelsB2; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 NumChannelsB3; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVEnabled; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVSetpoint0; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVSetpoint1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 UserConfigID; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 DCRD_AOut_0; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 BandEnables; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band0StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band0StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band1StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band1StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band2StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band2StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band3StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band3StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band4StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band4StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band5StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band5StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band6StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band6StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band7StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band7StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 FlagEnables;  // pterm flag enables 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] Flag0Operands; // flag 0 operands: 64 bits 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] Flag1Operands; // flag 0 operands: 64 bits 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] Flag2Operands; // flag 0 operands: 64 bits 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] Flag3Operands; // flag 0 operands: 64 bits 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] Flag4Operands; // flag 0 operands: 64 bits 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] Flag5Operands; // flag 0 operands: 64 bits 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] Flag6Operands; // flag 0 operands: 64 bits 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] Flag7Operands; // flag 0 operands: 64 bits 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm0;    // flag 0 - 7 product terms 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm2; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm3; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm4; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm5; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm6; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm7; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 DataFilterEnable; // 1 = enable spectral filtering, 0 = disable 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 ProcessingEnables; // bit 0 = spect filt, bit 1 = gain, 2 = bkgnd sub. 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 TimestampEnable; // 1 = enable time stamp, 0 = disable 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 DACspare; 
      [MarshalAs(UnmanagedType.I4)] 
      public Int32 TimestampInterval; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 CustomWordsEnable; // 1/0 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 EventCustomCount; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 RESERVED; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 ImageAcqMode;  // 1 = image, 0 = particle 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 InputTrigThresh; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 InputTrigChannel; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 RangeErrorEnable; // 1 = enable range error, 0 = disable 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 CrossBankConfig; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 ReportPackingMode; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 GPOutputEnable;  // 
      [MarshalAs(UnmanagedType.I4)] 
      public Int32 GPOutputDelay;  // 
      [MarshalAs(UnmanagedType.I4)] 
      public Int32 GPOutputPeriod;  // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 IntBoxcarEnable; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 BoxcarWidthEnable; 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public Int32[] ResetDelay; 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] TrigSource; 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public Int32[] TrigPeriod; 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public Int32[] IntegPeriod; 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public Int32[] IntegDelay; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel0; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel2; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel3; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel4; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel5; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel6; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel7; 
      [MarshalAs(UnmanagedType.I4)] 
      public Int32 TriggerEndCount; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 TrigStampSelect; 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public UInt16[] DataFormat;  // 4 banks: 0 = 17bit sign/mag, 1 = 16bit 2's, comp, 2 = 16b, 2's 1/2 scale 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 7)] 
      public UInt16[] RESERVED1; 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 256)] 
      public UInt16[] GainArray; 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 256)] 
      public UInt16[] TrigThreshArray; 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)] 
      public UInt16[] TrigEnableArray; // 256 bits, 1/0 for each channel (max of 256 channels) 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 322)] 
      public UInt16[] RESERVED2;   // 678-999 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 250)] 
      public UInt16[] CustomArray;   // user custom space: 1000-1249 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 567)] 
      public UInt16[] FactoryArray;   // start of factory table 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32)] 
      public SByte[] ModelNumber;   // model number 
      [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 151)] 
      public UInt16[] FactoryArray1;  // last piece of factory table 
     } 

问题是,我想我做了结构非托管类型,但编译器说它是托管类型。

或者它可能是指向这种方式的UInt16指针在托管代码中是错误的。

缓冲区参数[]是在16位,这是在dll函数中的传递参数,所以我坚持使用16位无论是使用指针或缓冲区来移动数据。

如果有人能够帮助解决这个问题,这将是一个巨大的帮助。

我已经搜索了网页,却找不到确切的答案。

在此先感谢。

============================================== ====================================

感谢您的答复嗨,

在这里,我收到不同的错误与下面的代码:

 fixed (UInt16 *cfgPtr = (UInt16*)&PhotoniqConfigTable){ 
      //modify config table, then send it back up to PhotoniQ 
      PhotoniqConfigTable.NumChannelsB0 = 6; 
      PhotoniqConfigTable.IntegPeriod[0] = per; 
      PhotoniqConfigTable.TrigPeriod[0] = 100000; 
      PhotoniqConfigTable.TrigStampSelect = 1; 
      PhotoniqConfigTable.TimestampEnable = 0; 
      PhotoniqConfigTable.RangeErrorEnable = 0; 
      PhotoniqConfigTable.DataFormat[0] = 0; 
      PhotoniqConfigTable.GPOutputEnable = 1; 
      PhotoniqConfigTable.GPOutputDelay = 10; 
      PhotoniqConfigTable.GPOutputPeriod = 1000; 

      for (int i = 0; i < Imports.CONFIG_TABLE_SZ; i++) 
      { 
       Arguments[i + 1] = *cfgPtr++; 
      } 
     }  

错误消息是:

不能分配给“cfgPtr”,因为它是一个“固定变量”。

固定语句赋值的右侧可能不是转换表达式。

不能使用固定表达式中包含的固定大小缓冲区。尝试使用固定语句。 (对于不同的线路,有三个相同的错误)

任何能向我解释的人都会帮助很大,因为我时间不够。

结构现在看起来如下:

 [StructLayout(LayoutKind.Sequential)] 
     unsafe public struct PHOTONIQ_CONFIG_TABLE 
     { 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SystemMode;  //Indicates current system mode, acquire or standby mode 0 = Standby Mode 1 = Acquire Mode 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVLimit0; // Maximum allowed voltage on HV supply 1 Range = 500 ?9250 (50 ?925V) 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVLimit1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 NumChannelsB0; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 NumChannelsB1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 NumChannelsB2; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 NumChannelsB3; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVEnabled; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVSetpoint0; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 HVSetpoint1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 UserConfigID; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 DCRD_AOut_0; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 BandEnables; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band0StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band0StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band1StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band1StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band2StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band2StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band3StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band3StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band4StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band4StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band5StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band5StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band6StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band6StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band7StartIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 Band7StopIndex; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 FlagEnables;  // pterm flag enables 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 Flag0Operands[4]; // flag 0 operands: 64 bits 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 Flag1Operands[4]; // flag 0 operands: 64 bits 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 Flag2Operands[4]; // flag 0 operands: 64 bits 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 Flag3Operands[4]; // flag 0 operands: 64 bits 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 Flag4Operands[4]; // flag 0 operands: 64 bits 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 Flag5Operands[4]; // flag 0 operands: 64 bits 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 Flag6Operands[4]; // flag 0 operands: 64 bits 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 Flag7Operands[4]; // flag 0 operands: 64 bits 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm0;    // flag 0 - 7 product terms 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm2; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm3; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm4; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm5; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm6; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 PTerm7; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 DataFilterEnable; // 1 = enable spectral filtering, 0 = disable 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 ProcessingEnables; // bit 0 = spect filt, bit 1 = gain, 2 = bkgnd sub. 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 TimestampEnable; // 1 = enable time stamp, 0 = disable 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 DACspare; 
      [MarshalAs(UnmanagedType.I4)] 
      public Int32 TimestampInterval; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 CustomWordsEnable; // 1/0 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 EventCustomCount; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 RESERVED; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 ImageAcqMode;  // 1 = image, 0 = particle 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 InputTrigThresh; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 InputTrigChannel; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 RangeErrorEnable; // 1 = enable range error, 0 = disable 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 CrossBankConfig; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 ReportPackingMode; // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 GPOutputEnable;  // 
      [MarshalAs(UnmanagedType.I4)] 
      public Int32 GPOutputDelay;  // 
      [MarshalAs(UnmanagedType.I4)] 
      public Int32 GPOutputPeriod;  // 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 IntBoxcarEnable; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 BoxcarWidthEnable; 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed Int32 ResetDelay[4]; 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 TrigSource[4]; 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed Int32 TrigPeriod[4]; 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed Int32 IntegPeriod[4]; 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed Int32 IntegDelay[4]; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel0; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel1; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel2; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel3; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel4; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel5; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel6; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 SibSel7; 
      [MarshalAs(UnmanagedType.I4)] 
      public Int32 TriggerEndCount; 
      [MarshalAs(UnmanagedType.U2)] 
      public UInt16 TrigStampSelect; 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)] 
      public fixed UInt16 DataFormat[4];  // 4 banks: 0 = 17bit sign/mag, 1 = 16bit 2's, comp, 2 = 16b, 2's 1/2 scale 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 7)] 
      public fixed UInt16 RESERVED1[7]; 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 256)] 
      public fixed UInt16 GainArray[256]; 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 256)] 
      public fixed UInt16 TrigThreshArray[256]; 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)] 
      public fixed UInt16 TrigEnableArray[16]; // 256 bits, 1/0 for each channel (max of 256 channels) 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 322)] 
      public fixed UInt16 RESERVED2[322];   // 678-999 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 250)] 
      public fixed UInt16 CustomArray[250];   // user custom space: 1000-1249 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 567)] 
      public fixed UInt16 FactoryArray[567];   // start of factory table 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32)] 
      public fixed SByte ModelNumber[32];   // model number 
      //[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 151)] 
      public fixed UInt16 FactoryArray1[151]; 
      //public fixed UInt16 FactoryArray1[151];  // last piece of factory table 
     }; 
+2

我建议在未来,当您对现有问题有后续问题时,*发布新问题*。与编辑旧的问题相比,您将更加关注新问题。 –

回答

6

的第二个答案,来回答你的第二个问题:

错误消息是:

Cannot assign to 'cfgPtr' because it is a 'fixed variable'. 

The right hand side of a fixed statement assignment may not be a cast expression. 

You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement. 

的错误信息,告诉你什么是错的。阅读它们。

无法分配给'cfgPtr',因为它是'固定变量'。

您在这里分配给它:

Arguments[i + 1] = *cfgPtr++; 

一旦你初始化包含指向一个固定的空间块的指针变量,你不能改变的变量因为你可能会改变它指向一个非固定空间块。为了防止这个常见的错误,C#阻止你修改该变量。

如果要修改变量,请创建而不是。设置它等于cfgPtr,然后修改变量。确保你做得对;您现在负责确保您将指针指向固定的某个东西。

The right hand side of a fixed statement assignment may not be a cast expression. 

在我最后的答案,我告诉你,读规范的第18.7,其中规定,“固定”语句采用:

一个引用一个固定的简单名称或成员访问可移动变量的大小缓冲区成员

转换表达式不是“简单名称或成员访问”。演员表达是演员表达。而这个演员甚至没有意义!指向结构的指针不是指向ushort的指针!

删除演员。将初始化程序中的表达式更改为对可移动变量的固定大小缓冲区成员的成员访问。

You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement. 

您正在直接使用固定大小的缓冲区。 你不能那样做。您只能通过获取指向它的指针来使用固定大小的缓冲区。同样,这是安全系统的一部分,可确保您不会意外地访问固定大小的非固定缓冲区。停止尝试直接访问缓冲区。获取指向缓​​冲区的指针并通过指针访问它。

任何能向我解释的人都会有很大的帮助,因为我时间不够。

让我在这一点上非常明确:您已打开管理安全系统关闭,程序的正确性和过程的稳定性,现在是你的问题,不是管理运行时的问题。如果你不明白一切关于如何安全地管理你自己的指针,停止你在做什么,并阅读它,直到你明白它。现在赶着写一大堆你不明白的代码,你就冒着后来处理可怕的错误的危险。现在是时候匆忙了。

首先阅读第18章的全部内容。只有当您彻底了解所有这些内容时,才应该尝试编写复杂的不安全代码。

+0

现在我终于遇到了一位好老师〜! –

+0

我喜欢所有这些原因背后的原因。希望我能听到更多来自其他渠道的消息。 –

5

你说的阵列如何在非托管内存进行布局,但并没有说他们如何在进行布局管理记忆。因此,编译器相信你在这个结构中有很多垃圾收集托管内存的引用 - 所有这些数组 - 所以不会让你指向它。

我怀疑你打算这些数组是固定大小的缓冲区,而不是托管数组,是吗?

您将需要阅读C#规范的第18.7节,以了解固定大小缓冲区如何在结构中工作。

还要注意的是there is a big difference between a "fixed in size" array and a "fixed in place" object pointed to by a pointer;这些是“固定”两个完全不同的用法。许多人会让他们感到困惑,特别是因为正确和安全地使用“固定大小”缓冲区,缓冲区的内存必须“固定到位”。

1

代替

 [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 151)] 
     public UInt16[] FactoryArray1; 

 public unsafe fixed ushort FactoryArray1[151]; 

因为数组 “看” 在管理和非管理存储器中的不同,但是固定大小的缓冲器 “看” 相同(即是blittable)。

相关问题