473,403 Members | 2,323 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,403 software developers and data experts.

Legacy Struct arguments

Hello guru's,

Please help me out in the following:
How must I write the code in C# to cope with the following
legacy-struct:
typedef struct callback
{
FILE* stream;
char buffer [1024];
}

I've been trying to use the following, to no avail:
[StructLayout(LayoutKind.Sequential)]
public struct CBinfo
{
public IntPtr stream;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)]
public byte[] buffer;
}

I guess the IntPtr can't 'marshal' the complete FILE-structure:
(char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;)

Please advise me with some code examples.
("one line of code equals a hundred words...")
Thanks for your time.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
2 1030
Saya,

If the structure contains a pointer (like it seems it does), then what
you are using should work. Are you trying to interpret the structure that
the pointer points to? If that is the case, then you will need to define
the FILE structure and then call the static PtrToStructure method on the
Marshal class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Saya" <an*******@devdex.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello guru's,

Please help me out in the following:
How must I write the code in C# to cope with the following
legacy-struct:
typedef struct callback
{
FILE* stream;
char buffer [1024];
}

I've been trying to use the following, to no avail:
[StructLayout(LayoutKind.Sequential)]
public struct CBinfo
{
public IntPtr stream;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)]
public byte[] buffer;
}

I guess the IntPtr can't 'marshal' the complete FILE-structure:
(char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;)

Please advise me with some code examples.
("one line of code equals a hundred words...")
Thanks for your time.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Nicholas,

I've pin-pointed my troubles to the following:
The C++ dll requires that a callback argument: <void** buffer> be
returned to it with the address of the buffer used in my C# function.
Explanation:

public delegate int MediaFunction( // the callback fn.
[MarshalAs(UnmanagedType.LPStr)]
string filename,
IntPtr info,
ref int size,
ref IntPtr bufferPtr, // my prob ....!
int first,
ref int last);

// Data read are put into CBinfo.buffer:
[StructLayout(LayoutKind.Sequential)]
public struct CBinfo
{
public IntPtr stream;
public int messageID;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=8192)]
public byte[] buffer;
}
...
public CBinfo cbi;
...

FileStream fs=fi.OpenRead();
nBytesRead=fs.Read(cbi.buffer, 0, nBytes);

Question is: how to deliver the "cbi.buffer" location into the callback
argument: "ref IntPtr bufferPtr" ?
Doing Marshal.nnn ? Please advise - Thank you!


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

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

Similar topics

0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
3
by: masood.iqbal | last post by:
In this day and age, you never say no to any work that is thrown at you ---- so when I was offered this short-term contract to convert legacy C code to C++, I did not say no. Personally I believed...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
2
by: Mark Olbert | last post by:
First off, the sympathy is for all you poor buggers out there who have to figure out how to marry Managed Extensions for C++ onto your legacy code. My condolences; my brief experience with the...
2
by: Ben | last post by:
I'm left with some legacy code using plain old str, and I need to make sure it works with unicode input/output. I have a simple plan to do this: - Run the code with "python -U" so all the string...
1
by: Subodh | last post by:
Hi, We have legacy code in C/C++, I am writing a wrapper component in C++/ CLI that will allow using this legacy code functionality in C#, I have linked all my static libraries to this C++/CLI...
2
by: =?Utf-8?B?U2V0aEluTUk=?= | last post by:
I am a total newb at .net, and I have not been able to search out a best practice answer to what must be a common problem. My app must process binary data from a UDP socket, a MSMQ queue and a...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.