[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Marshalling trouble: pointer to struct in another struct

Ruben

3/15/2007 4:41:00 PM

Hi all,

I'm writing a C# wrapper around an old C dll. It exports the following
function:

int WINAPI StartUpdate(unsigned
long,LPSTR,LPSTR,LPSTR,int,PSERR_STRUCT*);

The structs are defined as:

typedef SERR_STRUCT *PSERR_STRUCT;
typedef SERR_ARRAY *PSERR_ARRAY;

typedef struct startErrorArray
{
int ea_rdocn;
char *ea_bpiece;
char *ea_tpiece;
long ea_accnum;
long ea_keynum;
unsigned long ea_code;
} SERR_ARRAY;

typedef struct startErrorStruct
{
int es_cnt;
PSERR_ARRAY es_err;
} SERR_STRUCT;

So the PSERR_STRUCT* parameter to the function is a pointer to a
pointer to a number of SERR_STRUCT structs.
Furthermore the SERR_STRUCT structure itself contains a pointer to a
number of SERR_ARRAY structs.

How am I going to marshal this to my C# wrapper?
I tried the following already without much success:

Struct definitions:
====================

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct SERR_ARRAY
{
public int ea_rdocn;
public StringBuilder ea_bpiece;
public StringBuilder ea_tpiece;
public int ea_accnum;
public int ea_keynum;
public int ea_code;
}

[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Ansi)]
public struct SERR_STRUCT
{
public int es_cnt;
public SERR_ARRAY es_err;
}

DllImport of the function:
===========================

[DllImport("int32.dll",
EntryPoint = "StartUpdate",
CharSet = CharSet.Ansi)]
static extern int StartUpdate(int intMode, string strTSpec,
string strBSpec, string strPara,
int intBType,
ref SERR_STRUCT t_Error);

Calling code:
===============
SERR_STRUCT t_Error = new SERR_STRUCT();
t_Error.es_err = new SERR_ARRAY();
int intReturn = StartUpdate(0x2L, "1", null, "HFST", -1, ref t_Error);

All parameters are in parameters except for the last one.


My questions are:

1) I'm having doubts about the StringBuilder members of the SERR_ARRAY
structure.
I think those are fixed length (4 bytes/5 bytes including null char)
strings, so should I use
something like this instead?

[MarshalAs(UnmanagedType.LPStr, SizeConst=5)]
public string ea_bpiece;
[MarshalAs(UnmanagedType.LPStr, SizeConst=5)]
public string ea_tpiece;

2) I realise my function import isn't correct, because the last
parameter should be a double-pointer.
How would I do that? Do I have to implement custom marshalling using
PtrToStructure and the like?

3) How do I marshal the PSERR_ARRAY es_err member of the SERR_STRUCT
struct? I know it should be a pointer
also instead of a SERR_ARRAY instance. So should I use an IntPtr
instead allocating memory for it and passing
that into the member?

As you can see I'm rather new to marshalling issues, so please bare
with me. Any help will be much appreciated.
Thanks in advance.

Best regards,
Ruben.