Solved

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

Posted on 2014-01-20
4
1,555 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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 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

18 Experts available now in Live!

Get 1:1 Help Now