Marshalling problem a C structure

G

Guest

Hi Guys,
I am trying to marshal the following C structure in my C# code
but getting error message 'NotSupportedException'

C Structure is

struct TReport
{
enum TType { EVirus, EError } iType;
TCHAR iFileName[MAX_PATH]; /* Name including nesting inside archives. */

/* Only applicable if iType == EVirus. */
TCHAR iVirusName[64]; /* 16 is enough in most cases. */
enum TScannerAction iAction; /* Type of action taken upon detection. */

/* Only applicable if iType == EError. */
int iErrCode;
};


enum TScannerAction
{
EReportOnly,
EDisinfect,
EDelete
};

I am creating following C# strcture corresponding to this structure

unsafe public struct TReportUnmanaged
{
public enum RType : int{ EVirus, EError };
public char* fileName;
public char* virusName;
public enum TScannerAction : int
{
EReportOnly,
EDisinfect,
EDelete
};
int errCode;
}

The C function I am suppose to call is

extern "C" _declspec(dllexport)
int GetReport (int aInstance, struct TReport * aReport)
{
....
}

C# code used for calling is


StringBuilder fileName = new StringBuilder(200);
StringBuilder virusName = new StringBuilder(100);

char[] fileNameBuffer = MarshalHelper(fileName);
char[] virusNameBuffer = MarshalHelper(virusName);

TReportUnmanaged repUn = new TReportUnmanaged();

unsafe
{
fixed (char* pFirst = &fileNameBuffer[0],
pMiddle = &virusNameBuffer[0])
{
repUn.fileName = pFirst;
repUn.virusName = pMiddle;
GetReport(0, repUn);
}
}



public char[] MarshalHelper(StringBuilder s)
{
s.Append("\0");
return s.ToString().ToCharArray();
}


Can anybody help me out of this or point out how to marshall that C strcture ?

Thanks in Advance

Santosh
 
P

Paul G. Tobey [eMVP]

You wouldn't want to marshal character arrays (especially Unicode ones), as
char*! Look at the source for OpenNETCF SDF 1.4. There are tons of
structures marshaled there, with various different contents. In the code
that I wrote, I almost always did it as an array of bytes, where the right
properties were extracted by get and set methods based on the offset into
the array of bytes.

Paul T.
 
G

Guest

To add more, I am using .Net compact framework 1.0.

Paul G. Tobey said:
You wouldn't want to marshal character arrays (especially Unicode ones), as
char*! Look at the source for OpenNETCF SDF 1.4. There are tons of
structures marshaled there, with various different contents. In the code
that I wrote, I almost always did it as an array of bytes, where the right
properties were extracted by get and set methods based on the offset into
the array of bytes.

Paul T.

Santosh said:
Hi Guys,
I am trying to marshal the following C structure in my C# code
but getting error message 'NotSupportedException'

C Structure is

struct TReport
{
enum TType { EVirus, EError } iType;
TCHAR iFileName[MAX_PATH]; /* Name including nesting inside archives. */

/* Only applicable if iType == EVirus. */
TCHAR iVirusName[64]; /* 16 is enough in most cases. */
enum TScannerAction iAction; /* Type of action taken upon detection.
*/

/* Only applicable if iType == EError. */
int iErrCode;
};


enum TScannerAction
{
EReportOnly,
EDisinfect,
EDelete
};

I am creating following C# strcture corresponding to this structure

unsafe public struct TReportUnmanaged
{
public enum RType : int{ EVirus, EError };
public char* fileName;
public char* virusName;
public enum TScannerAction : int
{
EReportOnly,
EDisinfect,
EDelete
};
int errCode;
}

The C function I am suppose to call is

extern "C" _declspec(dllexport)
int GetReport (int aInstance, struct TReport * aReport)
{
....
}

C# code used for calling is


StringBuilder fileName = new StringBuilder(200);
StringBuilder virusName = new StringBuilder(100);

char[] fileNameBuffer = MarshalHelper(fileName);
char[] virusNameBuffer = MarshalHelper(virusName);

TReportUnmanaged repUn = new TReportUnmanaged();

unsafe
{
fixed (char* pFirst = &fileNameBuffer[0],
pMiddle = &virusNameBuffer[0])
{
repUn.fileName = pFirst;
repUn.virusName = pMiddle;
GetReport(0, repUn);
}
}



public char[] MarshalHelper(StringBuilder s)
{
s.Append("\0");
return s.ToString().ToCharArray();
}


Can anybody help me out of this or point out how to marshall that C
strcture ?

Thanks in Advance

Santosh
 
P

Paul G. Tobey [eMVP]

Perfect match. SDF 1.4 is for .NET CF 1.0. So, anything that you see in
the source code would be a workable base for what you want to do. The time
zone information structure might be a suitable model and there are tons of
things with strings and ints and enumerations in the Net namespace.

Paul T.

Santosh said:
To add more, I am using .Net compact framework 1.0.

Paul G. Tobey said:
You wouldn't want to marshal character arrays (especially Unicode ones),
as
char*! Look at the source for OpenNETCF SDF 1.4. There are tons of
structures marshaled there, with various different contents. In the code
that I wrote, I almost always did it as an array of bytes, where the
right
properties were extracted by get and set methods based on the offset into
the array of bytes.

Paul T.

Santosh said:
Hi Guys,
I am trying to marshal the following C structure in my C#
code
but getting error message 'NotSupportedException'

C Structure is

struct TReport
{
enum TType { EVirus, EError } iType;
TCHAR iFileName[MAX_PATH]; /* Name including nesting inside archives.
*/

/* Only applicable if iType == EVirus. */
TCHAR iVirusName[64]; /* 16 is enough in most cases. */
enum TScannerAction iAction; /* Type of action taken upon
detection.
*/

/* Only applicable if iType == EError. */
int iErrCode;
};


enum TScannerAction
{
EReportOnly,
EDisinfect,
EDelete
};

I am creating following C# strcture corresponding to this structure

unsafe public struct TReportUnmanaged
{
public enum RType : int{ EVirus, EError };
public char* fileName;
public char* virusName;
public enum TScannerAction : int
{
EReportOnly,
EDisinfect,
EDelete
};
int errCode;
}

The C function I am suppose to call is

extern "C" _declspec(dllexport)
int GetReport (int aInstance, struct TReport * aReport)
{
....
}

C# code used for calling is


StringBuilder fileName = new StringBuilder(200);
StringBuilder virusName = new StringBuilder(100);

char[] fileNameBuffer = MarshalHelper(fileName);
char[] virusNameBuffer = MarshalHelper(virusName);

TReportUnmanaged repUn = new TReportUnmanaged();

unsafe
{
fixed (char* pFirst = &fileNameBuffer[0],
pMiddle = &virusNameBuffer[0])
{
repUn.fileName = pFirst;
repUn.virusName = pMiddle;
GetReport(0, repUn);
}
}



public char[] MarshalHelper(StringBuilder s)
{
s.Append("\0");
return s.ToString().ToCharArray();
}


Can anybody help me out of this or point out how to marshall that C
strcture ?

Thanks in Advance

Santosh
 
G

Guest

Thanks Paul. It helps me a lot.

Cheers,
-Santosh

Paul G. Tobey said:
Perfect match. SDF 1.4 is for .NET CF 1.0. So, anything that you see in
the source code would be a workable base for what you want to do. The time
zone information structure might be a suitable model and there are tons of
things with strings and ints and enumerations in the Net namespace.

Paul T.

Santosh said:
To add more, I am using .Net compact framework 1.0.

Paul G. Tobey said:
You wouldn't want to marshal character arrays (especially Unicode ones),
as
char*! Look at the source for OpenNETCF SDF 1.4. There are tons of
structures marshaled there, with various different contents. In the code
that I wrote, I almost always did it as an array of bytes, where the
right
properties were extracted by get and set methods based on the offset into
the array of bytes.

Paul T.

Hi Guys,
I am trying to marshal the following C structure in my C#
code
but getting error message 'NotSupportedException'

C Structure is

struct TReport
{
enum TType { EVirus, EError } iType;
TCHAR iFileName[MAX_PATH]; /* Name including nesting inside archives.
*/

/* Only applicable if iType == EVirus. */
TCHAR iVirusName[64]; /* 16 is enough in most cases. */
enum TScannerAction iAction; /* Type of action taken upon
detection.
*/

/* Only applicable if iType == EError. */
int iErrCode;
};


enum TScannerAction
{
EReportOnly,
EDisinfect,
EDelete
};

I am creating following C# strcture corresponding to this structure

unsafe public struct TReportUnmanaged
{
public enum RType : int{ EVirus, EError };
public char* fileName;
public char* virusName;
public enum TScannerAction : int
{
EReportOnly,
EDisinfect,
EDelete
};
int errCode;
}

The C function I am suppose to call is

extern "C" _declspec(dllexport)
int GetReport (int aInstance, struct TReport * aReport)
{
....
}

C# code used for calling is


StringBuilder fileName = new StringBuilder(200);
StringBuilder virusName = new StringBuilder(100);

char[] fileNameBuffer = MarshalHelper(fileName);
char[] virusNameBuffer = MarshalHelper(virusName);

TReportUnmanaged repUn = new TReportUnmanaged();

unsafe
{
fixed (char* pFirst = &fileNameBuffer[0],
pMiddle = &virusNameBuffer[0])
{
repUn.fileName = pFirst;
repUn.virusName = pMiddle;
GetReport(0, repUn);
}
}



public char[] MarshalHelper(StringBuilder s)
{
s.Append("\0");
return s.ToString().ToCharArray();
}


Can anybody help me out of this or point out how to marshall that C
strcture ?

Thanks in Advance

Santosh
 

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