Legacy Struct arguments

S

Saya

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
S

Saya

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top