C# to unmanaged C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MottetCCSF
    New Member
    • Jul 2009
    • 1

    C# to unmanaged C++

    I have created 2 structures in my C# code :
    Code:
            [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
            public class RollInformationCSharp
            {
                [MarshalAs(UnmanagedType.R8)]
                public double rollDiameter;
    
                [MarshalAs(UnmanagedType.R8)]
                public double initialRoughness;
    
                [MarshalAs(UnmanagedType.R8)]
                public double finalRoughness;
    
                [MarshalAs(UnmanagedType.R8)]
                public double accumulateCombination;
    
                [MarshalAs(UnmanagedType.R8)]
                public double critialRollLength;
    
                [MarshalAs(UnmanagedType.R8)]
                public double rolledLength;
    
                [MarshalAs(UnmanagedType.R8)]
                public double percentageLifeRoll;
    
                [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 256)]
                public string rollName;
             };
    and :
    Code:
          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
            public class MultiRollCSharp
            {
                [MarshalAs(UnmanagedType.I4)]
                public int nbRoll;
    
                public RollInformationCSharp[] tabRoll;
            }
    In the C# code, i invoke a function of C++ dll :
    Code:
            [DllImport("DLL_RaFTmodel.dll", CharSet = CharSet.Ansi)]
            public static extern IntPtr DLL_FlesCalculation(MultiRollCSharp multiRollInfo,
                                                  CoilInformationCSharp coilInfo,
                                                  RollInformationCSharp rollInfo,
                                                  LimitsTypeCSharp LimitsSteel,
                                                  LimitsTypeCSharp LimitsRegulation,
                                                  LimitsTypeCSharp LimitsMachine,
                                                  FTInputsCsharp forceTensionInfo,
                                                  RaConstantsCSharp RaModelIn,
                                                  FTWeightCsharp FTmodelIn,
                                                  [In, MarshalAs(UnmanagedType.I4)] int strategy,
                                                  [In, MarshalAs(UnmanagedType.I4)] int rollLifeMaximization,
                                                  RaInputsCsharp RaDataIn,
                                                  char Version,
                                                  [In, MarshalAs(UnmanagedType.R4)] float errMax,
                                                  [Out, MarshalAs(UnmanagedType.I4)] out int error);
    In C++, I have also 2 structures :
    Code:
    struct RollInformation
    {
    	double rollDiameter;
    	double initialRoughnessRoll;
    	double finalRoughnessRoll;
    	double accumulateCombination;
    	double percentageLifeRoll;
    	double criticalRollLength;
    	double rolledLength;
    	char rollName[256];
    };
    and
    Code:
    struct MultiRollInformation
    {
    	int nbRoll;
    	RollInformation* tabRoll;
    };
    The function is also declared as follow :
    Code:
    extern EXPORTTOTEM_API 
    MULTICURVETYPE* DLL_FlesCalculation( struct MultiRollInformation *multiRollInfo,									 struct CoilInformation *coilInfo, 
    			 struct RollInformation *rollInfo,
    			 struct LimitsType *LimitsSteel,
    			 struct LimitsType *LimitsRegulation,
    			 struct LimitsType *LimitsMachine,
    			 struct FTInputs *forceTensionInfo,
    			 struct RaConstants *constRaIn,
    			 struct FTWeight *modelFTIn,
    			 int strategy,
    			 int rollLifeMaximization,
    			 struct RaInputs *dataRaIn,
    		                 char Version,
    			 float errMax,
    			 int &error);

    Example to fill the structure in C# :
    Code:
                MultiRollCSharp multiRollInfo = new MultiRollCSharp();
                for(int i = 0; i < 5; i++)
                {
                    RollInformationCSharp rollInfo1 = GetRollInformation();
                    int taille = 0;
                    if (multiRollInfo.tabRoll != null)
                        taille = multiRollInfo.tabRoll.Length;
    
                    RollInformationCSharp[] tab = new RollInformationCSharp[taille +1];
                    if (taille > 0)
                    {
                        multiRollInfo.tabRoll.CopyTo(tab, 0);
                    }
                    tab[tab.Length-1] = rollInfo1;
                    multiRollInfo.tabRoll = tab;
    
                    multiRollInfo.nbRoll += 1;
                }
    In debug mode, just before the call to the DLL, the both structures in C# are right (multiRollInfo and rollInfo)
    .
    In C++, the rollInfo is good. But the the multiroll info has 5 elements but there values are wrong .

    What is wrong?
    How can I correct this?

    thank you very much for your help
Working...