473,413 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

C# to unmanaged C++

I have created 2 structures in my C# code :
Expand|Select|Wrap|Line Numbers
  1.         [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  2.         public class RollInformationCSharp
  3.         {
  4.             [MarshalAs(UnmanagedType.R8)]
  5.             public double rollDiameter;
  6.  
  7.             [MarshalAs(UnmanagedType.R8)]
  8.             public double initialRoughness;
  9.  
  10.             [MarshalAs(UnmanagedType.R8)]
  11.             public double finalRoughness;
  12.  
  13.             [MarshalAs(UnmanagedType.R8)]
  14.             public double accumulateCombination;
  15.  
  16.             [MarshalAs(UnmanagedType.R8)]
  17.             public double critialRollLength;
  18.  
  19.             [MarshalAs(UnmanagedType.R8)]
  20.             public double rolledLength;
  21.  
  22.             [MarshalAs(UnmanagedType.R8)]
  23.             public double percentageLifeRoll;
  24.  
  25.             [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 256)]
  26.             public string rollName;
  27.          };
and :
Expand|Select|Wrap|Line Numbers
  1.       [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  2.         public class MultiRollCSharp
  3.         {
  4.             [MarshalAs(UnmanagedType.I4)]
  5.             public int nbRoll;
  6.  
  7.             public RollInformationCSharp[] tabRoll;
  8.         }
In the C# code, i invoke a function of C++ dll :
Expand|Select|Wrap|Line Numbers
  1.         [DllImport("DLL_RaFTmodel.dll", CharSet = CharSet.Ansi)]
  2.         public static extern IntPtr DLL_FlesCalculation(MultiRollCSharp multiRollInfo,
  3.                                               CoilInformationCSharp coilInfo,
  4.                                               RollInformationCSharp rollInfo,
  5.                                               LimitsTypeCSharp LimitsSteel,
  6.                                               LimitsTypeCSharp LimitsRegulation,
  7.                                               LimitsTypeCSharp LimitsMachine,
  8.                                               FTInputsCsharp forceTensionInfo,
  9.                                               RaConstantsCSharp RaModelIn,
  10.                                               FTWeightCsharp FTmodelIn,
  11.                                               [In, MarshalAs(UnmanagedType.I4)] int strategy,
  12.                                               [In, MarshalAs(UnmanagedType.I4)] int rollLifeMaximization,
  13.                                               RaInputsCsharp RaDataIn,
  14.                                               char Version,
  15.                                               [In, MarshalAs(UnmanagedType.R4)] float errMax,
  16.                                               [Out, MarshalAs(UnmanagedType.I4)] out int error);
In C++, I have also 2 structures :
Expand|Select|Wrap|Line Numbers
  1. struct RollInformation
  2. {
  3.     double rollDiameter;
  4.     double initialRoughnessRoll;
  5.     double finalRoughnessRoll;
  6.     double accumulateCombination;
  7.     double percentageLifeRoll;
  8.     double criticalRollLength;
  9.     double rolledLength;
  10.     char rollName[256];
  11. };
and
Expand|Select|Wrap|Line Numbers
  1. struct MultiRollInformation
  2. {
  3.     int nbRoll;
  4.     RollInformation* tabRoll;
  5. };
The function is also declared as follow :
Expand|Select|Wrap|Line Numbers
  1. extern EXPORTTOTEM_API 
  2. MULTICURVETYPE* DLL_FlesCalculation( struct MultiRollInformation *multiRollInfo,                                     struct CoilInformation *coilInfo, 
  3.              struct RollInformation *rollInfo,
  4.              struct LimitsType *LimitsSteel,
  5.              struct LimitsType *LimitsRegulation,
  6.              struct LimitsType *LimitsMachine,
  7.              struct FTInputs *forceTensionInfo,
  8.              struct RaConstants *constRaIn,
  9.              struct FTWeight *modelFTIn,
  10.              int strategy,
  11.              int rollLifeMaximization,
  12.              struct RaInputs *dataRaIn,
  13.                          char Version,
  14.              float errMax,
  15.              int &error);   

Example to fill the structure in C# :
Expand|Select|Wrap|Line Numbers
  1.             MultiRollCSharp multiRollInfo = new MultiRollCSharp();
  2.             for(int i = 0; i < 5; i++)
  3.             {
  4.                 RollInformationCSharp rollInfo1 = GetRollInformation();
  5.                 int taille = 0;
  6.                 if (multiRollInfo.tabRoll != null)
  7.                     taille = multiRollInfo.tabRoll.Length;
  8.  
  9.                 RollInformationCSharp[] tab = new RollInformationCSharp[taille +1];
  10.                 if (taille > 0)
  11.                 {
  12.                     multiRollInfo.tabRoll.CopyTo(tab, 0);
  13.                 }
  14.                 tab[tab.Length-1] = rollInfo1;
  15.                 multiRollInfo.tabRoll = tab;
  16.  
  17.                 multiRollInfo.nbRoll += 1;
  18.             }
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
Jul 17 '09 #1
0 1157

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
2
by: Paul Kenny | last post by:
Hi, I am trying to expose the functionality of an unmanaged C++ class to the other languages available in the .NET Framework. I have decided to do this by wrapping the unmanaged C++ class in a...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
6
by: marek | last post by:
Hello All, we are doing a quite a big project that contains at the lowest level an unmenaged c++ classes. Above it there are managed wrappers and at the top there are ASP.NET pages. Can anyone...
6
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
5
by: akash | last post by:
I'm having problems calling an unmanaged class from a managed wrapper. I suspect I'm missing something obvious, as I'm unfamiliar with C++ and classes are very simple. My unmanaged class is as...
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.