Solved

Use of Structure Array between C++ DLL and C# application

Posted on 2014-01-20
4
1,570 Views
Last Modified: 2014-01-27
Hi,
I would like to  call structure  in a C++ DLL from C# application.  I have tried it as in the following code snippet.

C++ DLL code as follows:
////////////////
struct Setting
{
    char* chName ;    
    char* chValue;
    char* chUnit;
};
struct SettingGroup
{
    char* Name;
    Setting *settings;
};
struct SettingCategory
{
    char* Name;
    SettingGroup *settingGroups;
};
//DLL function to get Settings
extern "C"__declspec(dllexport) SettingCategory *GetSettings(bool bSet);
///////////////////////////

//From C# code implementation where the DLL function is called.
/////////////////
//Structure definitions
 public struct Setting
    {
        public string Name;
        public string Value;
        public string Unit;
    }
   
    public struct SettingGroup
    {
        public string Name;              
        public Setting[] settings;        
    }

    public struct SettingCategory
    {
        public string Name;
        public SettingGroup[] SettingGroups;
    }

//.............
//DLL Import functions

[DllImport("Settings.dll")]
       public static extern IntPtr GetIEDSettings(bool bSet);    

//..
//... Calling the function
SettingCategory sc1= new SettingCategory();
sc1= (SettingCategory)Marshal.PtrToStructure(inp, typeof(SettingCategory));
////////////////////////////////////////////

When I tried the above code I am not able to read the SettingGroup & Setting  variable values from the variable sc1. How to proceed on this ? what might be wrong with this ?
0
Comment
Question by:Rooh
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39794806
I think that might be an issue with the charset you are using. Try

//Structure definitions
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi), Serializable]
 public struct Setting
    {
        public string Name;
        public string Value;
        public string Unit;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi), Serializable]   
    public struct SettingGroup
    {
        public string Name;              
        public Setting[] settings;        
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi), Serializable]
    public struct SettingCategory
    {
        public string Name;
        public SettingGroup[] SettingGroups;
    }

Open in new window

0
 

Author Comment

by:Rooh
ID: 39796022
Hi jkr. Thanks for the help. But it didnt work for me.

When I called Marshal as below, I could get the sc1.Name only, not the SettingGroup array.
//.....
SettingCategory sc1= new SettingCategory();
sc1= (SettingCategory)Marshal.PtrToStructure(inp, typeof(SettingCategory));
//.......

//But if I declare the struct array using IntPtr then I am able to read the first array element. like below :

public struct SettingCategory
{
       public string Name;
       public IntPtr SettingGroups;
 }
//But in this case I may have to get array size some how.

Please advice.
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 39802707
strings in c# always have UNICODE what is a double-byte or wchar_t in unmanaged c++.

if you want to match structures, you better would use wchar_t arrays of fixed (maximal) size in c++ structure and not pointers. pointers created in c++ generally cannot used in c# without marshaling and making copies. the array size - better length of string - you could get by searching for the first binary zero char, though.

Sara
0
 

Author Closing Comment

by:Rooh
ID: 39812796
Thank you.
I used IntPtr in place of the array declarations which I type coverted using Marshal.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now