Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1958
  • Last Modified:

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

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
Rooh
Asked:
Rooh
  • 2
1 Solution
 
jkrCommented:
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
 
RoohAuthor Commented:
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
 
sarabandeCommented:
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
 
RoohAuthor Commented:
Thank you.
I used IntPtr in place of the array declarations which I type coverted using Marshal.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now