From: Bob Yang on
thank you. you are right so I change to this but I still not able to
get the value for "sb". any recommadation? thank you!

byte[] bb2 = new byte[100];
nBytesRead2 = nBytesRead - 4;

for (i = 0; i < 4; i++)
{
bb2[i] = arrbyteBarcode[nBytesRead2];
nBytesRead2++;
}


nRC = csp2TimeStamp2Str(bb2, sb, sb.Length);






> > .... csp2TimeStamp2Str(byte[] value, StringBuilder Stamp, int nMaxLength);
>
> > int nRC, nBytesRead;
> > byte[] arrbyteBarcode= new byte[100];
> > StringBuilder sb = new StringBuilder(" ", 100);
> > nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
> > ArraySegment<byte> as = new ArraySegment<byte>(arrbyteBarcode,
> > nBytesRead-4, 4); // [2]
> > nRC = csp2TimeStamp2Str(as.Array, sb, sb.Length); // [3]
> > TextBox1.text= sb.toString();
>
> [snip]
>
> > [2] and [3] are used to get a byte array segment out of the original
> > array.
>
> No, it doesn't. ArraySegment<T>.Array is the entire array, not a subset.


From: Bob Yang on
I changed something and start to read value now. even it is not what I
want yet I think I may pass the wrong bb2.. I will try to test more.
and thank you to all of you! once I am done I will post the final
codes and share with everyone.

by the way, if someone can tell me how does c# assign a value to a
parameter without "ref" or "out" it will be great. thank you!



> thank you. you are right so I change to this but I still not able to
> get the value for "sb". any recommadation? thank you!
>
> byte[] bb2 = new byte[100];
> nBytesRead2 = nBytesRead - 4;
>
> for (i = 0; i < 4; i++)
> {
> bb2[i] = arrbyteBarcode[nBytesRead2];
> nBytesRead2++;
> }
>
> nRC = csp2TimeStamp2Str(bb2, sb, sb.Length);
>
>
>
> > > .... csp2TimeStamp2Str(byte[] value, StringBuilder Stamp, int nMaxLength);
>
> > > int nRC, nBytesRead;
> > > byte[] arrbyteBarcode= new byte[100];
> > > StringBuilder sb = new StringBuilder(" ", 100);
> > > nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
> > > ArraySegment<byte> as = new ArraySegment<byte>(arrbyteBarcode,
> > > nBytesRead-4, 4); // [2]
> > > nRC = csp2TimeStamp2Str(as.Array, sb, sb.Length); // [3]
> > > TextBox1.text= sb.toString();
>
> > [snip]
>
> > > [2] and [3] are used to get a byte array segment out of the original
> > > array.
>
> > No, it doesn't. ArraySegment<T>.Array is the entire array, not a subset.- -
>
> - -


From: Bob Yang on
thank you to Willy and Ben! yes, the key point is using "unsigned char
*Stamp, char *value" in c# to the right type.

1. by the way... how to make a method able to assign the value to the
parameter without using "ref" or "out"? thank you! I was surprice, it
can assign "arrbyteBarcode" and "sb" values wihtout using those
keyword ref and out!!

2. moreover, how come "char *value" =StringBuilder but "unsigned char
*Stamp" is NOT = to StringBuilder and must use byte array?






for everyone's information. here is the finally code in c#: (please
see previous post for the c++ and VB.net part)



[System.Runtime.InteropServices.DllImport("csp2.DLL")]
static extern int csp2TimeStamp2Str(byte[] value, StringBuilder
Stamp, int nMaxLength);


int nRC;
int nBytesRead, nBytesRead2;
byte[] arrbyteBarcode = new byte[100];
byte[] bb2 = new byte[100];

StringBuilder sb = new StringBuilder(" ", 100);

nBytesRead = csp2GetPacket(arrbyteBarcode, 1, 100); //get
the packages's byte data into arrbyteBarcode; return the array size to
nBytesRead

nBytesRead2 = nBytesRead - 4; // find the starting point
for the last 4 byte

for (i = 0; i < 4; i++) // assing last
4 bytes data to the new byte array
{
bb2[i] =
arrbyteBarcode[nBytesRead2];
nBytesRead2++;
}

////get timestamp

nRC = csp2TimeStamp2Str(bb2, sb,
100);
richTextBox1.Text = richTextBox1.Text
+ "Time: " + sb.ToString() + "\n";

From: Willy Denoyette [MVP] on
See inline
Willy.

"Bob Yang" <bobyang3(a)gmail.com> wrote in message
news:1191469653.346854.29590(a)w3g2000hsg.googlegroups.com...
> thank you to Willy and Ben! yes, the key point is using "unsigned char
> *Stamp, char *value" in c# to the right type.
>
> 1. by the way... how to make a method able to assign the value to the
> parameter without using "ref" or "out"? thank you! I was surprice, it
> can assign "arrbyteBarcode" and "sb" values wihtout using those
> keyword ref and out!!
>

When you pass a *reference type* like byte[] (or any other array type), the
interop layer pins the array instance and passes a pointer to the first
element in the array to the callee.
It's important that you pass an array when the C function is expecting a
pointer (to an array), you should not pass a *reference* to an array
element.
// OK
[DllImport...] ..... Test(byte[] ar, int size);
byte[] ba =...
Test(ba, ...);

// NOK
[DllImport...] ... Test(ref byte, int size);
byte[] ba = ...;
Test(ref ba[0], ...);

The latter may work on 32 bit windows, but fails on 64 bit windows due to an
optimization in the interop layer of the 64 bit CLR


> 2. moreover, how come "char *value" =StringBuilder but "unsigned char
> *Stamp" is NOT = to StringBuilder and must use byte array?
>
>
The underlying buffer of StringBuilder is a (UNICODE) char array, the
interop layer will apply the necessary conversions as defined by the
marshaling attributes (MarshalAs) applied to the parameter.


>
>
>
>
> for everyone's information. here is the finally code in c#: (please
> see previous post for the c++ and VB.net part)
>
>
>
> [System.Runtime.InteropServices.DllImport("csp2.DLL")]
> static extern int csp2TimeStamp2Str(byte[] value, StringBuilder
> Stamp, int nMaxLength);
>
>
> int nRC;
> int nBytesRead, nBytesRead2;
> byte[] arrbyteBarcode = new byte[100];
> byte[] bb2 = new byte[100];
>
> StringBuilder sb = new StringBuilder(" ", 100);
>
> nBytesRead = csp2GetPacket(arrbyteBarcode, 1, 100); //get
> the packages's byte data into arrbyteBarcode; return the array size to
> nBytesRead
>
> nBytesRead2 = nBytesRead - 4; // find the starting point
> for the last 4 byte
>
> for (i = 0; i < 4; i++) // assing last
> 4 bytes data to the new byte array
> {
> bb2[i] =
> arrbyteBarcode[nBytesRead2];
> nBytesRead2++;
> }
>
> ////get timestamp
>
> nRC = csp2TimeStamp2Str(bb2, sb,
> 100);
> richTextBox1.Text = richTextBox1.Text
> + "Time: " + sb.ToString() + "\n";
>


From: Bob Yang on
thank you! great information.

1. in DllImport situation, C++ is able to change value for a private
variable (I think it is because c++ will locate the variable's address
and change the value there?). what's about in the pure c# without
using any dll? for example, like the code below. is it possible to
change the passing parameters value without using "out" or "ref"?
thank you!


class OutReturnExample
{
static void Method(out int i, out string s1, out string s2)
{
i = 44;
s1 = "I've been returned";
s2 = null;
}
static void Main()
{
int value;
string str1, str2;
Method(out value, out str1, out str2);
// value is now 44
// str1 is now "I've been returned"
// str2 is (still) null;
}
}





2. if I have a c++ as (unsigned char[] ca1, char[] ca2), how can I
call it correctly in c#? is it equal to (byte[] ca1, byte[] ca2)?
Moreover, ca1 and ca2 are only passing IN the the value and c++ cannot
change the value because it is not a pointer in c++?

thank you!





> See inline
> Willy.
>
> "Bob Yang" <bobya...(a)gmail.com> wrote in message
>
> news:1191469653.346854.29590(a)w3g2000hsg.googlegroups.com...
>
> > thank you to Willy and Ben! yes, the key point is using "unsigned char
> > *Stamp, char *value" in c# to the right type.
>
> > 1. by the way... how to make a method able to assign the value to the
> > parameter without using "ref" or "out"? thank you! I was surprice, it
> > can assign "arrbyteBarcode" and "sb" values wihtout using those
> > keyword ref and out!!
>
> When you pass a *reference type* like byte[] (or any other array type), the
> interop layer pins the array instance and passes a pointer to the first
> element in the array to the callee.
> It's important that you pass an array when the C function is expecting a
> pointer (to an array), you should not pass a *reference* to an array
> element.
> // OK
> [DllImport...] ..... Test(byte[] ar, int size);
> byte[] ba =...
> Test(ba, ...);
>
> // NOK
> [DllImport...] ... Test(ref byte, int size);
> byte[] ba = ...;
> Test(ref ba[0], ...);
>
> The latter may work on 32 bit windows, but fails on 64 bit windows due to an
> optimization in the interop layer of the 64 bit CLR
>
> > 2. moreover, how come "char *value" =StringBuilder but "unsigned char
> > *Stamp" is NOT = to StringBuilder and must use byte array?
>
> The underlying buffer of StringBuilder is a (UNICODE) char array, the
> interop layer will apply the necessary conversions as defined by the
> marshaling attributes (MarshalAs) applied to the parameter.
>
>
>
>
>
> > for everyone's information. here is the finally code in c#: (please
> > see previous post for the c++ and VB.net part)
>
> > [System.Runtime.InteropServices.DllImport("csp2.DLL")]
> > static extern int csp2TimeStamp2Str(byte[] value, StringBuilder
> > Stamp, int nMaxLength);
>
> > int nRC;
> > int nBytesRead, nBytesRead2;
> > byte[] arrbyteBarcode = new byte[100];
> > byte[] bb2 = new byte[100];
>
> > StringBuilder sb = new StringBuilder(" ", 100);
>
> > nBytesRead = csp2GetPacket(arrbyteBarcode, 1, 100); //get
> > the packages's byte data into arrbyteBarcode; return the array size to
> > nBytesRead
>
> > nBytesRead2 = nBytesRead - 4; // find the starting point
> > for the last 4 byte
>
> > for (i = 0; i < 4; i++) // assing last
> > 4 bytes data to the new byte array
> > {
> > bb2[i] =
> > arrbyteBarcode[nBytesRead2];
> > nBytesRead2++;
> > }
>
> > ////get timestamp
>
> > nRC = csp2TimeStamp2Str(bb2, sb,
> > 100);
> > richTextBox1.Text = richTextBox1.Text
> > + "Time: " + sb.ToString() + "\n";- -
>
> - -