Solved

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

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 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