From: Bob Yang on
Hi, I have this in C++ and I like to call it from c# to get the value
but I fail. it will be good if you can give me some information. I
tried it in VB.net it works but I use almost the same way as VB in C#
but it doens't work.



c++: (csp2.dll)
NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char
*Stamp, char *value, long nMaxLength);




VB.net: (this works correctly)

Declare Function csp2TimeStamp2Str Lib "csp2.dll" (ByRef Stamp As
Byte, ByVal value As String, ByVal nMaxLength As Integer) As Integer

Dim nRC As Integer
Dim arrbyteBarcode(99) As Byte '100 elements
Dim nBytesRead As Integer
Dim bstrTmp As New VB6.FixedLengthString(50)

nBytesRead = csp2GetPacket(arrbyteBarcode(0), i, 100)

nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4), bstrTmp.Value,
Len(bstrTmp.Value))
TextBox1.text= VB.Left(bstrTmp.Value, 20)




C#: (this doesn't work :( )

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

int nRC, nBytesRead;
byte[] arrbyteBarcode= new byte[100];
FixedLengthString bstrTmp = new FixedLengthString(50);

nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
bstrTmp=" ";

nRC = csp2TimeStamp2Str(arrbyteBarcode[nBytesRead-4],
bstrTmp.Value, bstrTmp.Value.Length);
TextBox1.text= bstrTmp.Value.toString();

From: Willy Denoyette [MVP] on
"Bob Yang" <bobyang3(a)gmail.com> wrote in message
news:1191407842.969233.15230(a)d55g2000hsg.googlegroups.com...
> Hi, I have this in C++ and I like to call it from c# to get the value
> but I fail. it will be good if you can give me some information. I
> tried it in VB.net it works but I use almost the same way as VB in C#
> but it doens't work.
>
>
>
> c++: (csp2.dll)
> NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char
> *Stamp, char *value, long nMaxLength);
>
>
>
>
> VB.net: (this works correctly)
>
> Declare Function csp2TimeStamp2Str Lib "csp2.dll" (ByRef Stamp As
> Byte, ByVal value As String, ByVal nMaxLength As Integer) As Integer
>
> Dim nRC As Integer
> Dim arrbyteBarcode(99) As Byte '100 elements
> Dim nBytesRead As Integer
> Dim bstrTmp As New VB6.FixedLengthString(50)
>
> nBytesRead = csp2GetPacket(arrbyteBarcode(0), i, 100)
>
> nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4), bstrTmp.Value,
> Len(bstrTmp.Value))
> TextBox1.text= VB.Left(bstrTmp.Value, 20)
>
>
>
>
> C#: (this doesn't work :( )
>
> [System.Runtime.InteropServices.DllImport("csp2.DLL")] static extern
> int csp2TimeStamp2Str(byte value, string Stamp, int nMaxLength);
>
> int nRC, nBytesRead;
> byte[] arrbyteBarcode= new byte[100];
> FixedLengthString bstrTmp = new FixedLengthString(50);
>
> nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
> bstrTmp=" ";
>
> nRC = csp2TimeStamp2Str(arrbyteBarcode[nBytesRead-4],
> bstrTmp.Value, bstrTmp.Value.Length);
> TextBox1.text= bstrTmp.Value.toString();
>



..... 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();

First you have to pass a byte[] as first parameter to the function, you are
passing the first byte of the array by value. Note that the VB code is
flawed too, you should pass a byte[] not a refrence to a byte, this code
will fail on 64 bit Windows!
Second [1]get rid of the VB6 dependency and use a StringBuilder to pass a
fixed string buffer. Not sure why you are passing a " " char when calling
this function though.
[2] and [3] are used to get a byte array segment out of the original array.

Willy.



From: Ben Voigt [C++ MVP] on

> .... 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: Willy Denoyette [MVP] on
"Ben Voigt [C++ MVP]" <rbv(a)nospam.nospam> wrote in message
news:%23SWjrKeBIHA.748(a)TK2MSFTNGP04.phx.gbl...
>
>> .... 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.
>
>

Very true, ArraySegment is of little use in general and especially here.
Important point is that a byte[] must be passed as an argument, like this...
....
nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
byte[] ba = new byte[4];
Array.Copy(arrbyteBarcode, nBytesRead - 4, ba, 0, 4);
nRC = csp2TimeStamp2Str(ba, sb, sb.Length);
...

The same applies to the csp2GetPacket function, which is wrong too.

Willy.

From: Bob Yang on
Thank you! However, it doens't really work. sb.ToString() turns a
space " "

I capture some screens and code here: http://docs.google.com/Doc?id=dd5djd97_44d5nz2p

another question, this is not really related, how does it handle
access the value to "sb" wiht out "ref" or "out"? does c# handle this
by itself? I just wonder how to use it in the pure c# without c++
without global variables nor ref or out.



> "Bob Yang" <bobya...(a)gmail.com> wrote in message
>
> news:1191407842.969233.15230(a)d55g2000hsg.googlegroups.com...
>
>
>
>
>
> > Hi, I have this in C++ and I like to call it from c# to get the value
> > but I fail. it will be good if you can give me some information. I
> > tried it in VB.net it works but I use almost the same way as VB in C#
> > but it doens't work.
>
> > c++: (csp2.dll)
> > NoMangle long DLL_IMPORT_EXPORT csp2TimeStamp2Str(unsigned char
> > *Stamp, char *value, long nMaxLength);
>
> > VB.net: (this works correctly)
>
> > Declare Function csp2TimeStamp2Str Lib "csp2.dll" (ByRef Stamp As
> > Byte, ByVal value As String, ByVal nMaxLength As Integer) As Integer
>
> > Dim nRC As Integer
> > Dim arrbyteBarcode(99) As Byte '100 elements
> > Dim nBytesRead As Integer
> > Dim bstrTmp As New VB6.FixedLengthString(50)
>
> > nBytesRead = csp2GetPacket(arrbyteBarcode(0), i, 100)
>
> > nRC = csp2TimeStamp2Str(arrbyteBarcode(nBytesRead - 4), bstrTmp.Value,
> > Len(bstrTmp.Value))
> > TextBox1.text= VB.Left(bstrTmp.Value, 20)
>
> > C#: (this doesn't work :( )
>
> > [System.Runtime.InteropServices.DllImport("csp2.DLL")] static extern
> > int csp2TimeStamp2Str(byte value, string Stamp, int nMaxLength);
>
> > int nRC, nBytesRead;
> > byte[] arrbyteBarcode= new byte[100];
> > FixedLengthString bstrTmp = new FixedLengthString(50);
>
> > nBytesRead = csp2GetPacket(arrbyteBarcode[0], i, 100);
> > bstrTmp=" ";
>
> > nRC = csp2TimeStamp2Str(arrbyteBarcode[nBytesRead-4],
> > bstrTmp.Value, bstrTmp.Value.Length);
> > TextBox1.text= bstrTmp.Value.toString();
>
> .... 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();
>
> First you have to pass a byte[] as first parameter to the function, you are
> passing the first byte of the array by value. Note that the VB code is
> flawed too, you should pass a byte[] not a refrence to a byte, this code
> will fail on 64 bit Windows!
> Second [1]get rid of the VB6 dependency and use a StringBuilder to pass a
> fixed string buffer. Not sure why you are passing a " " char when calling
> this function though.
> [2] and [3] are used to get a byte array segment out of the original array.
>
> Willy.- -
>
> - -