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