A bytearray

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

A bytearray

Dimitri Minich
Hello,
a need a byte-array for an ocx function. In C it looks like this:
BYTE pKey[8];
pKey[0] = 0x77;
pKey[1] = 0xFB;
pKey[2] = 0xD9;
pKey[3] = 0x15;
pKey[4] = 0x82;
pKey[5] = 0xE8;
pKey[6] = 0x1D;
pKey[7] = 0xD1;

In dolphin I build this one:
a1 := 16r77. a2 :=16rFB.a3 :=16rD9.a4 :=16r15.a5 :=16r82.a6 :=16rE8.a7
:=16r1D.a8 := 16rD1.

ba := ByteArray new:8.

ba at:1 put:a1;at:2 put:a2;at:3 put:a3;at:4 put:a4;at:5 put:a5;at:6
put:a6;at:7 put:a7;at:8 put:a8.

It doesn't work, I got an error: Invalid parameter passed to API function.
Does anybody know how to build the right array?

Thanks
Dimitri Minich


Reply | Threaded
Open this post in threaded view
|

Re: A bytearray

Ian Bartholomew-18
Dimitri,

> In dolphin I build this one:
> a1 := 16r77. a2 :=16rFB.a3 :=16rD9.a4 :=16r15.a5 :=16r82.a6 :=16rE8.a7
> :=16r1D.a8 := 16rD1.

There is a shortcut that you can use for creating a literal ByteArray -

ba := #[16r77 16rFB 16rD9 16r15 16r82 16rE8 16r1D 16rD1].

NB. This is now a *literal* so if anything changes the array then all the
occurrences will change. This could obviously not be what you want and so
you might then need to use

ba := #[16r77 16rFB 16rD9 16r15 16r82 16rE8 16r1D 16rD1] copy.

It's still easier than the longhand method though.

> It doesn't work, I got an error: Invalid parameter passed to API
> function. Does anybody know how to build the right array?

Your original function used "pKey" which would imply that it is a pointer to
a ByteArray that is needed.  Try passing the address of the ByteArray, as in

ba yourAddress

It also depends on how you have defined the external function's interface in
Dolphin.  If this still doesn't work then you should post your definition as
well, it will make suggesting solutions a bit easier.

--
Ian


Reply | Threaded
Open this post in threaded view
|

Re: A bytearray

Dimitri Minich
> Your original function used "pKey" which would imply that it is a pointer
to
> a ByteArray that is needed.  Try passing the address of the ByteArray, as
in
>
> ba yourAddress
>
> It also depends on how you have defined the external function's interface
in
> Dolphin.  If this still doesn't work then you should post your definition
as
> well, it will make suggesting solutions a bit easier.

Hello Ian,
I use an ocx for a cardreader, I didn't build this ocx. It is a
BasicCard-Reader ,a smart-BasicCard  and Basiccard-OCX (www.basiccard.com).
In the manual are some examples for V-Basic. I would like to do this with
dolphin. Some function work very well, but the function "setKey: _: "
doesn't like to work with my array.
Example From manual:
Example:

Dim zc as New zcBasicCard

Dim Error as Long

Dim MyDesKey(1 To 8) as Byte

....

REM Setup des key to same value as in card

MyDesKey(1)=&HA5

MyDesKey(2)=&H17

MyDesKey(3)=&H8D

MyDesKey(4)=&H35

MyDesKey(5)=&H62

MyDesKey(6)=&H96

MyDesKey(7)=&HEF

MyDesKey(8)=&HC4

zc.SetKey(1)=MyDesKey

Error=zc.LastErr

if Error<>0 then

' Do error handling here

end if

....

REM Start encryption with key 1 and some simple random

Randomize

Error=zc.StartEncryption(1, MyDesKey, Int(Rnd))

if Error<>0 then

' Do error handling here

end if

....

Error=zc.EndEncryption

if Error<>0 then

' Do error handling here

end if

I use this code :
zc := ZCBCardLibIzcBasicCard new.
ba := [16r77 16rFB 16rD9 16r15 16r82 16rE8 16r1D 16rD1]copy.
zc setKey:128 _:ba.
error := lastError.
(Content of this error: Invalid parameter passed to BasicCard API function)
I tried it also with "ba yourAddress", but I got the same result.
Any suggestions?

Dimitri Minich


Reply | Threaded
Open this post in threaded view
|

Re: A bytearray

Blair McGlashan
"Dimitri Minich" <[hidden email]> wrote in message
news:3e37b26e$[hidden email]...
>...
> I use an ocx for a cardreader, I didn't build this ocx. It is a
> BasicCard-Reader ,a smart-BasicCard  and Basiccard-OCX
(www.basiccard.com).

> In the manual are some examples for V-Basic. I would like to do this with
> dolphin. Some function work very well, but the function "setKey: _: "
> doesn't like to work with my array.
>...
> I use this code :
> zc := ZCBCardLibIzcBasicCard new.
> ba := [16r77 16rFB 16rD9 16r15 16r82 16rE8 16r1D 16rD1]copy.
> zc setKey:128 _:ba.
> error := lastError.
> (Content of this error: Invalid parameter passed to BasicCard API
function)
> I tried it also with "ba yourAddress", but I got the same result.
> Any suggestions?

Did you generate the interface for the OCX from its type library? If so make
sure to use the method selector exactly as generated, if not (and the
interface is not marked as nonextensible) then Dolphin has to assume that
the object can add methods at run time, and so it falls back on attempting
to invoke by name through IDispatch when a #doesNotUnderstand: error occurs.
This mechanism is simplistic, and does not always work.

If you are going through a method in the generated interface, how has it
been defined by the type-library analyzer? Specifically what type of
argument is expected for the array argument? Since it is probably designed
to be called from VB, I suspect it will be a SAFEARRAY, in which case you
probably need an #asSAFEARRAY in place of the #copy.



Regards

Blair


Reply | Threaded
Open this post in threaded view
|

Re: A bytearray

Ian Bartholomew-18
In reply to this post by Dimitri Minich
Dimitri,

> I use this code :
> zc := ZCBCardLibIzcBasicCard new.
> ba := [16r77 16rFB 16rD9 16r15 16r82 16rE8 16r1D 16rD1]copy.
> zc setKey:128 _:ba.
> error := lastError.
> (Content of this error: Invalid parameter passed to BasicCard API
> function) I tried it also with "ba yourAddress", but I got the same
> result.
> Any suggestions?

No suggestions yet, but a couple of questions.

How have you defined the ZCBCardLibIzcBasicCard>>setKey:_:  method? (and I
really can't recommend using _ as a argument name)

How does the documentation for the OCX or DLL define the SetKey function?.
I can't find any documentation for that on the web site and I don't really
want to download and install a 3.5 MB file unless I need to.

The ZCBasic that they use seems to have its own format for String
parameters, 3 bytes pushed onto the P-Code interpreter stack, so that is not
much help without knowing what the OCX /DLL is expecting.  The Delphi
wrapper that is available uses another dll (zcbci.dll) and passes pointers
to that but as I haven't the source code for this extra dll I can't then see
how it uses the basic card OCX/DLL (if it does at all).

I'm afraid a bit more information is needed.

--
Ian


Reply | Threaded
Open this post in threaded view
|

Re: A bytearray

Dimitri Minich
Ian,

> How have you defined the ZCBCardLibIzcBasicCard>>setKey:_:  method? (and I
> really can't recommend using _ as a argument name)

I build this class with ActiveX Component Wizard, so all methods were
generated automatically.

> How does the documentation for the OCX or DLL define the SetKey function?.
> I can't find any documentation for that on the web site and I don't really
> want to download and install a 3.5 MB file unless I need to.

If you like, I can mail you a pdf-file. Here is the documentation of
SetKey - function:
3.1.3.51 SetKey

Set encryption key for use with StartEncryption. Key set by this function
must match key declared in card.

Syntax: (Property, Write only)

Object.SetKey(KeyNum as Integer) as Variant

Content:

Key either as String or array of Bytes. Note that size of key is either 8 or
16 (characters for String or bytes for

array of Byte).

Parameter:

Number Name Direction Description

1 Keynum In Number of key to set

Error values available by LastErr: (see LastErr)

Value Description

BASICCARDERROR.NOERROR No error, function call successful

BASICCARDERROR.BCI_ERROR_PARM Invalid parameter passed to function call

See also: SetPoly, ReadKeyFile

Supported cards: ZC1.X, ZC2.X, ZC3.X

Example: see StartEncryption

May be it doesn't work at all with dolphin !?

Thanks

Dimitri


Reply | Threaded
Open this post in threaded view
|

Re: A bytearray

Ian Bartholomew-18
Dimitri,

> I build this class with ActiveX Component Wizard, so all methods were
> generated automatically.

Yes, after I read Blair's reply I realised that this was probably what you
are doing. I still prefer to do any interfacing myself and, as a result,
haven't ever got round to investigating all this new (!) ActiveX stuff :-).
Because of this self imposed ignorance I tend to forget that all these
wizards and type libraries are available.

> If you like, I can mail you a pdf-file. Here is the documentation of
> SetKey - function:
> 3.1.3.51 SetKey
[]
> May be it doesn't work at all with dolphin !?

The best thing to do is follow Blair's suggestions, if anyone can work out
how to use the OCX he can!.

If it proves not to be possible to interface directly with Dolphin (and I
don't think that is very likely) then you may want to have a look at the
Delphi code that I downloaded from the web site you mentioned.  If you can
do it in Delphi, and it may involve using an intermediate dll, then you can
do the same from Dolphin.

--
Ian


Reply | Threaded
Open this post in threaded view
|

Re: A bytearray

Dimitri Minich
In reply to this post by Blair McGlashan
Blair,

> If you are going through a method in the generated interface, how has it
> been defined by the type-library analyzer? Specifically what type of
> argument is expected for the array argument? Since it is probably designed
> to be called from VB, I suspect it will be a SAFEARRAY, in which case you
> probably need an #asSAFEARRAY in place of the #copy.

the #asSAFERRAY solved my problem, thanks.
But now to get some return value from the card-function, I've to
specify the type of this parameter (Integer, String, Byte, ...). This
parameter should be gived to the card-function, the card-function
fills this parameter with value.

I can do it for String with #String new. How can I do it for Integer
and other types without #new classmethod?

Example:

zc := ZCBCard1LibIzcBasicCard new.
myString := 'Hello'.
myString2 := String new.
myInteger := ??? "here should be an integer like this #Integer new"
zc param1:myInteger.
zc param2:myString.
zc param3:myString2.
zc param4:nil
error := zc transaction: 16r20 ins: 16r15 paramCount: 4.
myString := zcparam2.
myString2 := zc param3.
myInteger := zc param1.

Any suggestions?

Regards

Dimitri Minich


Reply | Threaded
Open this post in threaded view
|

Re: A bytearray

Blair McGlashan
"Dimitri Minich" <[hidden email]> wrote in message
news:[hidden email]...
> Blair,
>
> > If you are going through a method in the generated interface, how has it
> > been defined by the type-library analyzer? Specifically what type of
> > argument is expected for the array argument? Since it is probably
designed
> > to be called from VB, I suspect it will be a SAFEARRAY, in which case
you

> > probably need an #asSAFEARRAY in place of the #copy.
>
> the #asSAFERRAY solved my problem, thanks.
> But now to get some return value from the card-function, I've to
> specify the type of this parameter (Integer, String, Byte, ...). This
> parameter should be gived to the card-function, the card-function
> fills this parameter with value.
>
> I can do it for String with #String new. How can I do it for Integer
> and other types without #new classmethod?

Hmmm, be careful, String new will just create an empty string containing a
single null terminator. If the OCX writes off the end of this, then memory
will be corrupted. This may not show up immediately, but the damage will
eventually cause some kind of strange behaviour or crash. If the OCX is
expecting a string buffer, then it may need to be presized appropriately.
Actually though I suspect (since it is designed for use from VB)  that it
needs a BSTR, not a String. An in-out BSTR parameter would not need to be
presized as it is not allocated off the Dolphin heap, therefore the OCX can
reallocate it as necessary.

As for Integers, well you probably need to use one of the relevant
ExternalInteger subclasses. Integers in VB are 16-bit, and the corresponding
Dolphin ExternalInteger class is SWORD. VB longs are 32-bit, the
corresponding Dolphin class being SDWORD.

Regards

Blair