c++ function call with struct...

B

BMermuys

Hi,
inline

roben said:
Hi,

I need to call a function in a c++ dll with a struct.
It works fine with the integeres - but the char[] variable doesnt.
I also tried using StringBuilder

Not inside a struct.
as I have seen suggested around the net,
but it just crashes my program.

Can anyone help ?

-----------------
C# code
-----------------
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct RBN
{
public int card_expirymonth;
public int card_expiryyear;
public char[] card_number;

Change the last field into :
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)]
public string card_number;

HTH,
greetings
}


[DllImport("myext.dll")] public static extern int Rbnfunc(ref RBN a);


RBN b = new RBN();
b.card_expirymonth = 4;
b.card_expiryyear = 7;
b.card_number = "12345678".ToCharArray();

AuthResult = Rbnfunc(ref b);



-----------------
C++ code
-----------------

struct RBN
{
int rbn_expirymonth;
int rbn_expiryyear;
char card_number[21];
}
 
B

BMermuys

Hi,
inline

roben said:
hi,. Yeah seems to work..

But if I try to add another string... It seems that even if I set the
SizeConst to 4, the string can only contain 3 chars...
Why ???

C-style-strings are terminated with a 0-terminator (0x00).
This is important if you want to use library functions like printf,
sprintf...

Suppose you have a string like this (in c):

char f[4] = "tes"; // then f[3] = '\0' terminator
char f[4] = "test"; // array bounds overflow error, no room for the '\0'
terminator

strcpy (f, "tes"); // will add a '\0' terminator

The size of the array indicates the string's capacity while the '\0'
terminator indicates the end of the string (length).
C# doens't use a 0-terminator in it's string, but since the size and layout
of the c# en C struct must be the same, you must use the same size in c#.
So if you want to store a string with maximum 4 chars, then you should
declare char[5] in C and use string with SizeConst=5 in c#.


It's also possible to see a C character array (eg. char[4]; ) as just an
array of characters and not a c-style-string.
This means:
- there's no '\0' terminator used
- you shouldn't use strcpy, printf (functions that expect a
c-style-string)
- in c# the field should be :
[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
public char[] b;

hth,
greetings
b.mystr1 = "jepp";

Only "jep" is stored....???

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct RBN
{
public int card_expirymonth;
public int card_expiryyear;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)] public string
card_number;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)] public string mystr1;
}



BMermuys said:
Hi,
inline

roben said:
Hi,

I need to call a function in a c++ dll with a struct.
It works fine with the integeres - but the char[] variable doesnt.
I also tried using StringBuilder

Not inside a struct.
as I have seen suggested around the net,
but it just crashes my program.

Can anyone help ?

-----------------
C# code
-----------------
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct RBN
{
public int card_expirymonth;
public int card_expiryyear;
public char[] card_number;

Change the last field into :
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)]
public string card_number;

HTH,
greetings
}


[DllImport("myext.dll")] public static extern int Rbnfunc(ref RBN a);


RBN b = new RBN();
b.card_expirymonth = 4;
b.card_expiryyear = 7;
b.card_number = "12345678".ToCharArray();

AuthResult = Rbnfunc(ref b);



-----------------
C++ code
-----------------

struct RBN
{
int rbn_expirymonth;
int rbn_expiryyear;
char card_number[21];
}
};


int Rbnfunc(struct RBN * b)
{
logerror(LOG_ERR, "RBNCard expmonth:%d",b->rbn_expirymonth);
logerror(LOG_ERR, "RBNCard expyear:%d",b->rbn_expiryyear);
logerror(LOG_ERR, "RBNCard card_number:%s",b->card_number);
b->rbn_expirymonth = 8;
return 0;
}


Thanks in advance!
 
R

roben

Hi,

I need to call a function in a c++ dll with a struct.
It works fine with the integeres - but the char[] variable doesnt.
I also tried using StringBuilder as I have seen suggested around the net,
but it just crashes my program.

Can anyone help ?

-----------------
C# code
-----------------
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct RBN
{
public int card_expirymonth;
public int card_expiryyear;
public char[] card_number;
}


[DllImport("myext.dll")] public static extern int Rbnfunc(ref RBN a);


RBN b = new RBN();
b.card_expirymonth = 4;
b.card_expiryyear = 7;
b.card_number = "12345678".ToCharArray();

AuthResult = Rbnfunc(ref b);



-----------------
C++ code
-----------------

struct RBN
{
int rbn_expirymonth;
int rbn_expiryyear;
char card_number[21];
};


int Rbnfunc(struct RBN * b)
{
logerror(LOG_ERR, "RBNCard expmonth:%d",b->rbn_expirymonth);
logerror(LOG_ERR, "RBNCard expyear:%d",b->rbn_expiryyear);
logerror(LOG_ERR, "RBNCard card_number:%s",b->card_number);
b->rbn_expirymonth = 8;
return 0;
}


Thanks in advance!
 
R

roben

hi,. Yeah seems to work..

But if I try to add another string... It seems that even if I set the
SizeConst to 4, the string can only contain 3 chars...
Why ???

b.mystr1 = "jepp";

Only "jep" is stored....???

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct RBN
{
public int card_expirymonth;
public int card_expiryyear;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)] public string
card_number;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)] public string mystr1;
}



BMermuys said:
Hi,
inline

roben said:
Hi,

I need to call a function in a c++ dll with a struct.
It works fine with the integeres - but the char[] variable doesnt.
I also tried using StringBuilder

Not inside a struct.
as I have seen suggested around the net,
but it just crashes my program.

Can anyone help ?

-----------------
C# code
-----------------
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct RBN
{
public int card_expirymonth;
public int card_expiryyear;
public char[] card_number;

Change the last field into :
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)]
public string card_number;

HTH,
greetings
}


[DllImport("myext.dll")] public static extern int Rbnfunc(ref RBN a);


RBN b = new RBN();
b.card_expirymonth = 4;
b.card_expiryyear = 7;
b.card_number = "12345678".ToCharArray();

AuthResult = Rbnfunc(ref b);



-----------------
C++ code
-----------------

struct RBN
{
int rbn_expirymonth;
int rbn_expiryyear;
char card_number[21];
}
};


int Rbnfunc(struct RBN * b)
{
logerror(LOG_ERR, "RBNCard expmonth:%d",b->rbn_expirymonth);
logerror(LOG_ERR, "RBNCard expyear:%d",b->rbn_expiryyear);
logerror(LOG_ERR, "RBNCard card_number:%s",b->card_number);
b->rbn_expirymonth = 8;
return 0;
}


Thanks in advance!
 

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