473,379 Members | 1,252 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,379 software developers and data experts.

using a structure data from C function returning pointer to struct

Hi, i have big subsystem written in old C and published by dll (and lib). Dll
functions do:
1) allocate global memory for internal structures
2) controls dll subsystem (communicate by sockets, read a files, etc). These
control functions triggers change of a values in the subsystem internal
structures.
3) dealocate memory for internal structures.

Application only processes values in the internal structures, addressed by
pointer returned as result of dll function.

Now, I need write .NET application in C# which use old DLL subsystem. But, I
can not find the way to make accessible the values of subsystem's internal
structures. InteropServices and Marshal functions fails for converting
returned IntPtr to structure. Is there some way to solve this problem ? Thank
for help.
------------------------------------
Declarations in the old C DLL library:
typedef struct my_data_rec{
unsigned char type; // offset 0, length 1
unsigned short len; // offset 2, length 2 (default align 2 on WORD
boundary)
unsigned short elem; // offset 4, length 2
char fill[2];// 2 bytes - force alignment on double boundary
char value[65520];// ASCIZ string variable length
} MY_DATA_STRUCT;
MY_DATA_STRUCT * __cdecl SomeDllFunc();

Application in C# (in class Form1) declarations:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MY_DATA_STRUCT
{
[MarshalAs(UnmanagedType.I1)]
public byte typ;
[MarshalAs(UnmanagedType.U2)]
public ushort len;
[MarshalAs(UnmanagedType.U2)]
public ushort elem;
[MarshalAs(UnmanagedType.I1, SizeConst = 2)]
public string fill; // only 2 bytes, value ignored
[MarshalAs(UnmanagedType.I1, SizeConst = 65520)]
public string textValue;
}
[DllImport("Select.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Cdecl)]
static extern IntPtr SomeDllFunc();

Application code (in button1_Click procedure):
MY_DATA_STRUCT ReturnedData = new MY_DATA_STRUCT();
IntPtr p;

p = SomeDllFunc();
Marshal.PtrToStructure(p,ReturnedData); // runtime error occurs here in
the moment of the calling

Error reported is some as : "Structure can't be class of values"
-------------------------------------------------------------------
Where is a error in my code fragment, or the solution of the problem do not
exists ? Thank you for your idee.

Jun 27 '08 #1
2 1888
On 2008-06-20, vladimir <vl******@discussions.microsoft.comwrote:
Hi, i have big subsystem written in old C and published by dll (and lib). Dll
functions do:
1) allocate global memory for internal structures
2) controls dll subsystem (communicate by sockets, read a files, etc). These
control functions triggers change of a values in the subsystem internal
structures.
3) dealocate memory for internal structures.

Application only processes values in the internal structures, addressed by
pointer returned as result of dll function.

Now, I need write .NET application in C# which use old DLL subsystem. But, I
can not find the way to make accessible the values of subsystem's internal
structures. InteropServices and Marshal functions fails for converting
returned IntPtr to structure. Is there some way to solve this problem ? Thank
for help.
------------------------------------
Declarations in the old C DLL library:
typedef struct my_data_rec{
unsigned char type; // offset 0, length 1
unsigned short len; // offset 2, length 2 (default align 2 on WORD
boundary)
unsigned short elem; // offset 4, length 2
char fill[2];// 2 bytes - force alignment on double boundary
char value[65520];// ASCIZ string variable length
} MY_DATA_STRUCT;
MY_DATA_STRUCT * __cdecl SomeDllFunc();

Application in C# (in class Form1) declarations:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MY_DATA_STRUCT
{
[MarshalAs(UnmanagedType.I1)]
public byte typ;
[MarshalAs(UnmanagedType.U2)]
public ushort len;
[MarshalAs(UnmanagedType.U2)]
public ushort elem;
[MarshalAs(UnmanagedType.I1, SizeConst = 2)]
public string fill; // only 2 bytes, value ignored
[MarshalAs(UnmanagedType.I1, SizeConst = 65520)]
public string textValue;
}
[DllImport("Select.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Cdecl)]
static extern IntPtr SomeDllFunc();

Application code (in button1_Click procedure):
MY_DATA_STRUCT ReturnedData = new MY_DATA_STRUCT();
IntPtr p;

p = SomeDllFunc();
Marshal.PtrToStructure(p,ReturnedData); // runtime error occurs here in
the moment of the calling

Error reported is some as : "Structure can't be class of values"
-------------------------------------------------------------------
Where is a error in my code fragment, or the solution of the problem do not
exists ? Thank you for your idee.
You can't use that overload of PtrToStructure for value types. You need to
change that to:

MY_DATA_STRUCT ReturnData = (MY_DATA_STRUCT)Marshal.PtrToStructure (p, typeof(MY_DATA_STRUCT));
Also, I would change your CharSet declarations to Ansi rather then Auto.

--
Tom Shelton
Jun 27 '08 #2
"Tom Shelton" wrote:
On 2008-06-20, vladimir <vl******@discussions.microsoft.comwrote:
Hi, i have big subsystem written in old C and published by dll (and lib). Dll
functions do:
1) allocate global memory for internal structures
2) controls dll subsystem (communicate by sockets, read a files, etc). These
control functions triggers change of a values in the subsystem internal
structures.
3) dealocate memory for internal structures.

Application only processes values in the internal structures, addressed by
pointer returned as result of dll function.

Now, I need write .NET application in C# which use old DLL subsystem. But, I
can not find the way to make accessible the values of subsystem's internal
structures. InteropServices and Marshal functions fails for converting
returned IntPtr to structure. Is there some way to solve this problem ? Thank
for help.
------------------------------------
Declarations in the old C DLL library:
typedef struct my_data_rec{
unsigned char type; // offset 0, length 1
unsigned short len; // offset 2, length 2 (default align 2 on WORD
boundary)
unsigned short elem; // offset 4, length 2
char fill[2];// 2 bytes - force alignment on double boundary
char value[65520];// ASCIZ string variable length
} MY_DATA_STRUCT;
MY_DATA_STRUCT * __cdecl SomeDllFunc();

Application in C# (in class Form1) declarations:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct MY_DATA_STRUCT
{
[MarshalAs(UnmanagedType.I1)]
public byte typ;
[MarshalAs(UnmanagedType.U2)]
public ushort len;
[MarshalAs(UnmanagedType.U2)]
public ushort elem;
[MarshalAs(UnmanagedType.I1, SizeConst = 2)]
public string fill; // only 2 bytes, value ignored
[MarshalAs(UnmanagedType.I1, SizeConst = 65520)]
public string textValue;
}
[DllImport("Select.dll", CharSet = CharSet.Auto, CallingConvention =
CallingConvention.Cdecl)]
static extern IntPtr SomeDllFunc();

Application code (in button1_Click procedure):
MY_DATA_STRUCT ReturnedData = new MY_DATA_STRUCT();
IntPtr p;

p = SomeDllFunc();
Marshal.PtrToStructure(p,ReturnedData); // runtime error occurs here in
the moment of the calling

Error reported is some as : "Structure can't be class of values"
-------------------------------------------------------------------
Where is a error in my code fragment, or the solution of the problem do not
exists ? Thank you for your idee.

You can't use that overload of PtrToStructure for value types. You need to
change that to:

MY_DATA_STRUCT ReturnData = (MY_DATA_STRUCT)Marshal.PtrToStructure (p, typeof(MY_DATA_STRUCT));
Also, I would change your CharSet declarations to Ansi rather then Auto.

--
Tom Shelton
Tom, thank you for your suggestion ! It works well.
Vladimir
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
15
by: John Alway | last post by:
Hello, I'm using a DLL I wrote in C++, and am attempting to call and use it from VB. This works fine for functions where I pass parameters by value, but I can't get pointers to work. I get...
7
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I...
12
by: mohan | last post by:
Hi All, How to implement virtual concept in c. TIA Mohan
30
by: junky_fellow | last post by:
I was looking at the source code of linux or open BSD. What I found that lots of typedefs were used. For example, consider the offset in a file. It was declared as off_t offset; and off_t is...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
16
by: pkoniusz | last post by:
Hello everybody, Been just thinking how actually one could convert the following unmanaged code to the managed C++: struct JustAnExample { char value1; int value2; // etc ....
1
by: Beorne | last post by:
I have a cpp application with this structure: //////////////C++/////////////// typedef struct StatusStructure { char FixedLenString; long LongVariable; double DoubleVariable; BOOL...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.