473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

help on calling C functions from C# involving structures

Hi,
I have been trying to marshal data from C# onto a C function which accepts a pointer to a structure.

The structure is as below...

=====================
struct Details
{
int ssno;
char name[50];
char hobbies[10][30];
ADDRESS addr[3];
}DETAILS;

struct Address
{
int hno;
char street[20];
}ADDRESS;

int updateDetails(DETAILS *);
=======================

But I am having problem with sending double dimensional arrays e.g: the hobbies field.
I am not aware to find a proper match to Marshal it.
Even I have given LPArray, ByValArray etc., but it doesn't work.

I am looking for a equivalent structure that I can define in C# code.
The structures that I have created in C# and that do not work
===================
[StructLayout(LayoutKind.Sequential,CharSet=CharSet .Ansi)]
public struct DETAILS
{
public int ssno;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 50)]
public String name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
public String[] hobbies;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public ADDRESS[] addr;
}

[StructLayout(LayoutKind.Sequential,CharSet=CharSet .Ansi)]
public struct ADDRESS
{
public int hno;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 20)]
public String street;
}
======================

I tried creating my own C dll and created a function to read and print the data.
The data comes fine until ssno and name.
But all other hobbies and Address details are junk characters.
Any inputs would be of great help.
Jan 31 '08 #1
1 951
Finally I pulled this off... But I had to severely go through all the odds and possibilities to get to it.

Key:

1. When passed in structures, array of strings do not go as intended and it was creating problems, because it was sending as pointers or some other weird reason. I would be grateful, if somebody can explain that.

So, finally, I put that string in a structure and instead of array of strings, made it into a array of structures.

2. Never try to send your Int16 to C int, thinking both are 2 byte in length. Though the documentation clearly says Int32 of C# is ported to int in C, while dumping the memory, I had my own doubts and tried to tinker with it.

Ultimately, my final code looked this way... Hope this would help somebody and save their time.

Details.cpp

=========================

#include <iostream>
#include <conio>
#include <stdio>
#include <windows>

typedef struct Address
{
int hno;
char street[10];
}ADDRESS;

typedef struct Details
{
int ssno;
char name[50];
char hobbies[10][30];
ADDRESS addr[2];
}DETAILS;

extern "C" __declspec(dllexport) void WINAPI printDetails(DETAILS *d)
{
std::cout<<"Social sec no : " << d->ssno << std::endl;
std::cout<<"Name : " << d->name << std::endl;

for(int i=0;i<10;i++)
std::cout << "Hobby[" << i << "]: " << d->hobbies << std::endl;

for(int i=0;i<2;i++)
{
std::cout << "Address[" << i << "]:" << std::endl;
std::cout << "Hno : " << d->addr.hno << std::endl;
std::cout << "Street : " << d->addr.street << std::endl;
}
}

=====================



TestMarshal.cs

=====================

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
namespace MarshalProject
{
class TestMarshal
{
#region DeclareStructs
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DETAILS
{
public int ssno;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public String name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public HOBBIES[] hobbies;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public ADDRESS[] addr;
}

[StructLayout(LayoutKind.Sequential,CharSet = CharSet.Ansi)]
public struct HOBBIES
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=30)]
public String hobby;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct ADDRESS
{
public int hno;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public String street;
}
#endregion DeclareStructs

#region DLLFuncs
[DllImport("details.dll")]
public static extern void printDetails(ref DETAILS d);
#endregion DLLFuncs

static void Main(string[] args)
{
try
{
DETAILS d = new DETAILS();
d.ssno = 100;
d.name = new String((char)'A',50);
d.hobbies = new HOBBIES[10];

for (int i = 0; i < d.hobbies.Length; i++)
d.hobbies.hobby = "";

d.hobbies[0].hobby = new String((char)'C', 50);
d.hobbies[1].hobby = new String((char)'D', 50);
d.hobbies[2].hobby = new String((char)'E', 50);

d.addr = new ADDRESS[2];
d.addr[0].hno = 10;
d.addr[0].street = "abcd";
d.addr[1].hno = 20;
d.addr[1].street = "efgh";

printDetails(ref d);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}

=========================

Cheers all
Feb 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
2
by: leo2100 | last post by:
Hi, I need help with this program. The program is supposed to take a text file and identify the words in it, then it should print them and count how many times a word is repeated. At first main...
22
by: SQACSharp | last post by:
I'm trying to get the control name of an editbox in another window. The following code set the value "MyPassword" in the password EditBox but it fail to return the control name of the EditBox. ...
0
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.