Re: PInvoke Question - C to C#

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Jeff Winn wrote:
Makes sense, it's been so long since I had to use them I'm not
suprised I forgot how to do it. Wouldn't you need to (or want to)
manually define the size of the structure as well?

I don't think so. I'd be using the C++/CLI compiler to talk to such native
functions from .NET anyway, since it fully understands native union types.


"Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx> wrote in message
news:e5TOh9d4IHA.4272@xxxxxxxxxxxxxxxxxxxxxxx
Jeff Winn wrote:
Unfortunately C# doesn't directly support unions in structs, I
forget how I worked around this issue the last time I had to work
with the ipv6_addr structure in the Windows SDK. You might try
using an Explicit struct layout and the FieldOffset attribute to
define where the fields are at in the struct. I just remember
working with unions was a royal pain.

You set the FieldOffset of every member to zero to make a union.


"Ciaran O''Donnell" <CiaranODonnell@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote
in message
news:DE59E613-E948-408B-B507-5ED7B646C70A@xxxxxxxxxxxxxxxx
If you create the structs in the exact same way in C# and decorate
them with
the
[System.Runtime.InteropServices.StructLayout(
LayoutKind.Sequential)] attribute then they should marshall back
ok as far as i'm aware. Its been a little while so I might be
forgetting something


--
Ciaran O'Donnell
http://wannabedeveloper.spaces.live.com


"Jessica" wrote:

Hello,

I have a pInvoke question.

This is the C function that is exported from one of the C dll,

extern __declspec(dllexport) IM_RET_CODE ST_import (IM_MODE mode,
char
*filename,
ST_TYPES
**st_type, ST_ERROR **st_error);

this C function has to be called from a C# code by means of
pInvoke calls.


the parameters ST_TYPES and ST_ERROR are out parameters.
they are allocated and filled by the callee.

the ST_TYPES is a structure of union of structure. the definition
is given
below.

the question here is how to marshal it back from the C code to the
C# code?

Thanks,
J




typedef struct st_types {
IM_TYPE typ; /*
Unionkennzeichnung */ union { /*
Hier folgen die verschiedenen Unions */
BlockType blocktype;
InVarType invartype;
OutVarType outvartype;
InOutVarType inoutvartype;
BlockTypeSource blocktypesource;
Task task;
CPU_RESOURCE resource;
ProgramType programtype;
BlockInstance blockinstance;
BlockConnection blockconnection;
DataType datatype;
ComponentType componenttype;
VersionType versiontype;
BlockIntegerParameterType blockintegerparamtype;
BlockStringParameterType blockstringparamtype;
} u;
} ST_TYPES;

typedef struct {
char name[SIZEBLOCKTYPENAME]; /* Name des
Bausteintypes */
char comment[SIZEBLOCKTYPECOMM]; /* freier Kommentar
zum Typ */
char description[SIZEPARACOMM]; /* freier
Kommentar zum
Typ */
enum GRAF_TYPE_ST blockView ; /*
Bausteindarstellung groá/klein */
CFC_BOOL generic; /* Baustein ist
erweiterbar */
CFC_BOOL visible; /* 0 f?r
Linkbausteine */ int ticks; /*
Ticks f?r Baustein */ int gen_ticks; /*
Ticks pro generischen Eingang */
char classname[SIZEANYNAME]; /*
Bausteinkatalogordner */ } BlockType;

typedef struct {
char name[SIZEPARANAME]; /* Name des
Bausteintypes */
char comment[SIZEPARACOMM]; /* freier Kommentar
zum Typ */
char description[SIZEPARACOMM]; /* freier
Kommentar zum
Typ */
enum DATA_TYPE_ST dataType; /* Datentyp des
Parametertyps
*/
CFC_BOOL programmable; /* Parameter ist
parametrierbar */
CFC_BOOL link; /* Parameter ist
verquellbar */
CFC_BOOL visible; /* Parameter ist
sichtbar */
CFC_BOOL dynamic; /* Parameter wird
dynamisiert
*/
char value[SIZELONGVALUEMAX]; /*
Default-Parametrierung */
int ticks; /* Ticks f?r
Eingangsverschaltung */
int gen_ticks; /* reserved */
char typeName[SIZEANYNAME]; /* Name, falls
benutzerdefiniert */
} InVarType;


typedef struct {
char name[SIZEPARANAME]; /* Name des
Bausteintypes */
char comment[SIZEPARACOMM]; /* freier Kommentar
zum Typ */
char description[SIZEPARACOMM]; /* freier
Kommentar zum
Typ */
enum DATA_TYPE_ST dataType; /* Datentyp des
Parametertyps
*/
CFC_BOOL programmable; /* Parameter ist
parametrierbar */
CFC_BOOL visible; /* Parameter ist
sichtbar */
CFC_BOOL dynamic; /* Parameter wird
dynamisiert
*/
char value[SIZELONGVALUEMAX]; /*
Default-Parametrierung */
char typeName[SIZEANYNAME]; /* Name, falls
benutzerdefiniert */
} OutVarType;


typedef struct {
char name[SIZEPARANAME]; /* Name des
Bausteintypes */
char comment[SIZEPARACOMM]; /* freier Kommentar
zum Typ */
char description[SIZEPARACOMM]; /* freier
Kommentar zum
Typ */
enum DATA_TYPE_ST dataType; /* Datentyp des
Parametertyps
*/
CFC_BOOL programmable; /* Parameter ist
parametrierbar */
CFC_BOOL link; /* Parameter ist
verquellbar */
CFC_BOOL visible; /* Parameter ist
sichtbar */
CFC_BOOL dynamic; /* Parameter wird
dynamisiert
*/
char value[SIZELONGVALUEMAX]; /*
Default-Parametrierung */
int ticks; /* Ticks f?r
Eingangsverschaltung */
int gen_ticks; /* reserved */
char typeName[SIZEANYNAME]; /* Name, falls
benutzerdefiniert */
} InOutVarType;

typedef struct {
long size; /* L"nge des
Quelltextes */
char *pSource; /* Zeiger auf den
Quelltext */
} BlockTypeSource;

typedef struct {
char name[SIZETASKNAME]; /* Name der Task */
char comment[SIZETASKCOMM]; /* freier Kommentar
zum Typ */
char description[SIZEPARACOMM]; /* freier
Kommentar zum
Typ */
int ticks; /* Ticks f?r
maxinal zul. Ticks */
int gen_ticks; /* reserved */
} Task;

typedef struct {
char name[SIZECPUNAME]; /* Name der CPU
(Plancontainer,
ST-Syntax) */
char comment[SIZECPUNAME]; /* Name der CPU
(Plancontainer,
CFC-Syntax) */
char description[SIZEPARACOMM]; /* freier
Kommentar zum
Typ */
enum { TYP_TASK, TYP_FUNCTION_BLOCK } typ; /* Typ der
Ressource. TASK:
es gibt zugeordnete Tasks */
/* FUNCTION_BLOCK: diese
Resource wird durch Funktionsbausteine belegt */
} CPU_RESOURCE;


typedef struct {
char name[SIZECHARTNAME]; /* Name des
Programmtyps (ST-Syntax) */
char comment[SIZECHARTNAME]; /* freier Kommentar
zum Typ
(CFC-Syntax) */
char description[SIZEPARACOMM]; /* freier
Kommentar zum
Typ */
char task[SIZETASKNAME]; /* freier Kommentar
zum Typ
(CFC-Syntax, in der das Prog. abl"uft */
} ProgramType;

typedef struct {
char name[SIZEGLOBALVARNAME]; /* Name der
Instanz (ST-Syntax) */
char comment[SIZEGLOBALVARNAME]; /* freier
Kommentar zur
Instanz (CFC-Syntax) */
char description[SIZEPARACOMM]; /* freier
Kommentar zum
Typ */
char insttypename[SIZEBLOCKTYPENAME]; /* Name
(CFC-Syntax) des
erzeugenden Typs (Kommentar) */
int x, y; /* Position der
Instance */
CFC_BOOL visible; /* 0 f?r
Linkbausteine */
} BlockInstance;

typedef struct {
char src_instance[SIZEGLOBALVARNAME]; /* Name der
Instanz */ char src_output[SIZELONGVALUEMAX]; /*
Name der Instanz */ char dst_instance[SIZEGLOBALVARNAME]; /*
Name der Instanz */
char dst_input[SIZEPARANAME]; /* Name der Instanz */ }
BlockConnection;

typedef struct {
char name[SIZEANYNAME]; /* Name des
Datentyps */ enum DATA_TYPE_ST dataType; /*
Datentyp */ //TODO } DataType;

typedef struct {
char name[SIZEANYNAME]; /* Name der
Strukturkomponente */
enum DATA_TYPE_ST dataType; /* Datentyp */
//TODO char typeName[SIZEANYNAME]; /* Name, falls
benutzerdefiniert */
} ComponentType;

typedef struct {
char version[SIZESTRING8]; /* Versionskennung
*/ } VersionType;

typedef struct {
char name[SIZEPARANAME]; /* Name der Parameters
*/ int value; /* Zahlwert */
} BlockIntegerParameterType;


typedef struct {
char name[SIZEPARANAME]; /* Name der
Parameters */ char value[SIZEPARACOMM]; /*
Zeichenkette */ } BlockStringParameterType;


.



Relevant Pages

  • Re: PInvoke Question - C to C#
    ... the fields are at in the struct. ... typedef struct st_types { ... zum Typ */ ... Kommentar zum ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [patch] Re: newer version of HTC Dream support
    ... new file mode 100644 ... without even the implied warranty of ... +struct rpcsvr_platform_device ...
    (Linux-Kernel)
  • Re: PInvoke Question - C to C#
    ... You might try using an Explicit struct layout and the FieldOffset attribute to define where the fields are at in the struct. ... typedef struct st_types { ... char comment[SIZEBLOCKTYPECOMM]; /* freier Kommentar zum Typ */ ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Setfocus / killfocus on cbutton
    ... Because it is a static struct, declared at the global scope level in this compilation ... The problem is that if I install the typedef struct as protected in ... declaration (cpp file) as well as if I try to add some class variables ... CStaticField m_Field; ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH 1/4] staging: cpc-usb: convert comment style in cpc.h
    ... -// representation of the CAN parameters for the PCA82C200 controller ... typedef struct CPC_PCA82C200_PARAMS { ... -// In general two types of overrun may occur. ...
    (Linux-Kernel)