2011-09-29 81 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)。

相關問題