marshaling more complex structures

E

Elmar Jacobs

Hi folk,

I try to marshal following structure from a c++ dll to a c# smart device
application.
My problem is the BYTE stream. It woud not fill with data from the marshal
function PtrToStructure(). How can I fill the BYTE stream?

Thanks a lot,
elmar


c++ interface

[cpp]

typedef struct pHeaderData{
int spectrumNumber;
int spectrumType;
int spectrumLength;
BYTE startTime[8]; //bcd YYMMDDWWhhmmssHH
BYTE stopTime[8]; //bcd YYMMDDWWhhmmssHH
int realTime;
int liveTime;
int dwellTime;
}headerData;


c# part

public struct pSpectrumHeaderStruct
{
public uint spectrumNumber;
public uint spectrumType;
public uint spectrumLength;
public byte [] startTime;
public byte [] stopTime;
public uint realTime;
public uint liveTime;
public uint dwellTime;

public pSpectrumHeaderStruct(int dummy)
{
spectrumNumber = new uint();
spectrumType = new uint();
spectrumLength = new uint();
startTime = new byte[8]; //the only
possibility for me to allocate memory for the byte streams
stopTime = new byte [8];
realTime = new uint();
liveTime = new uint();
dwellTime = new uint();
}

} //end of namespace pSpectrumHeaderStruct




{
.....
[DllImport("ComInterface.dll", EntryPoint = "SWI")]
......

Int64 rcx;
//pointer of dll data structure
pSpectrumHeaderStruct specHeader = new pSpectrumHeaderStruct(0);
//allocate memory for the data structure

SWI(ref rcx); // get pointer from
the dll
if (rcx != 0)
{
pSpectrumHeaderStruct sHeader;
sHeader = (pSpectrumHeaderStruct) Marshal.PtrToStructure( (IntPtr)
rcx, typeof(pSpectrumHeaderStruct)); // try to marshal get failed
!!!!!!!!!
sHeader = sHeader;
}
....
}
[\cpp]
 
C

Chris Tacke, eMVP

You need to just pass the entire struct as a 40-byte array, then parse the
individual values out of the array on the managed side.
 
E

Elmar Jacobs

Sorry for my bad understanding but how can I "pass the entire struct as a
40-byte array" under c#?

elmar




Chris Tacke said:
You need to just pass the entire struct as a 40-byte array, then parse the
individual values out of the array on the managed side.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
---
Principal Partner
OpenNETCF Consulting
www.OpenNETCF.com



Elmar Jacobs said:
Hi folk,

I try to marshal following structure from a c++ dll to a c# smart device
application.
My problem is the BYTE stream. It woud not fill with data from the marshal
function PtrToStructure(). How can I fill the BYTE stream?

Thanks a lot,
elmar


c++ interface

[cpp]

typedef struct pHeaderData{
int spectrumNumber;
int spectrumType;
int spectrumLength;
BYTE startTime[8]; //bcd YYMMDDWWhhmmssHH
BYTE stopTime[8]; //bcd YYMMDDWWhhmmssHH
int realTime;
int liveTime;
int dwellTime;
}headerData;


c# part

public struct pSpectrumHeaderStruct
{
public uint spectrumNumber;
public uint spectrumType;
public uint spectrumLength;
public byte [] startTime;
public byte [] stopTime;
public uint realTime;
public uint liveTime;
public uint dwellTime;

public pSpectrumHeaderStruct(int dummy)
{
spectrumNumber = new uint();
spectrumType = new uint();
spectrumLength = new uint();
startTime = new byte[8]; //the only
possibility for me to allocate memory for the byte streams
stopTime = new byte [8];
realTime = new uint();
liveTime = new uint();
dwellTime = new uint();
}

} //end of namespace pSpectrumHeaderStruct




{
....
[DllImport("ComInterface.dll", EntryPoint = "SWI")]
.....

Int64 rcx;
//pointer of dll data structure
pSpectrumHeaderStruct specHeader = new pSpectrumHeaderStruct(0);
//allocate memory for the data structure

SWI(ref rcx); // get pointer from
the dll
if (rcx != 0)
{
pSpectrumHeaderStruct sHeader;
sHeader = (pSpectrumHeaderStruct) Marshal.PtrToStructure( (IntPtr)
rcx, typeof(pSpectrumHeaderStruct)); // try to marshal get failed
!!!!!!!!!
sHeader = sHeader;
}
...
}
[\cpp]
 
P

Paul G. Tobey [eMVP]

There are several examples in the OpenNETCF Smart Device Framework (SDF)
(SystemTime is one that comes to mind). Basically, you declare the C++ call
so that it expects a byte array, not a structure type, as its parameter.
Then, you declare a type which will represent the structure, so that your
code can access the data in the 'structure' via properties. You have to
write the accessors for the properties so that when you get, say, the
spectrumLength field, you do that by retrieving the 8th through the 12th
bytes and interpret them as an int.

Paul T.

Elmar Jacobs said:
Sorry for my bad understanding but how can I "pass the entire struct as a
40-byte array" under c#?

elmar




Chris Tacke said:
You need to just pass the entire struct as a 40-byte array, then parse the
individual values out of the array on the managed side.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
---
Principal Partner
OpenNETCF Consulting
www.OpenNETCF.com



Elmar Jacobs said:
Hi folk,

I try to marshal following structure from a c++ dll to a c# smart device
application.
My problem is the BYTE stream. It woud not fill with data from the marshal
function PtrToStructure(). How can I fill the BYTE stream?

Thanks a lot,
elmar


c++ interface

[cpp]

typedef struct pHeaderData{
int spectrumNumber;
int spectrumType;
int spectrumLength;
BYTE startTime[8]; //bcd YYMMDDWWhhmmssHH
BYTE stopTime[8]; //bcd YYMMDDWWhhmmssHH
int realTime;
int liveTime;
int dwellTime;
}headerData;


c# part

public struct pSpectrumHeaderStruct
{
public uint spectrumNumber;
public uint spectrumType;
public uint spectrumLength;
public byte [] startTime;
public byte [] stopTime;
public uint realTime;
public uint liveTime;
public uint dwellTime;

public pSpectrumHeaderStruct(int dummy)
{
spectrumNumber = new uint();
spectrumType = new uint();
spectrumLength = new uint();
startTime = new byte[8]; //the only
possibility for me to allocate memory for the byte streams
stopTime = new byte [8];
realTime = new uint();
liveTime = new uint();
dwellTime = new uint();
}

} //end of namespace pSpectrumHeaderStruct




{
....
[DllImport("ComInterface.dll", EntryPoint = "SWI")]
.....

Int64 rcx;
//pointer of dll data structure
pSpectrumHeaderStruct specHeader = new pSpectrumHeaderStruct(0);
//allocate memory for the data structure

SWI(ref rcx); // get
pointer
from
the dll
if (rcx != 0)
{
pSpectrumHeaderStruct sHeader;
sHeader = (pSpectrumHeaderStruct) Marshal.PtrToStructure( (IntPtr)
rcx, typeof(pSpectrumHeaderStruct)); // try to marshal get failed
!!!!!!!!!
sHeader = sHeader;
}
...
}
[\cpp]
 

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