Solved

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

Posted on 2014-01-20
4
1,727 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.

631 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