On Jul 13, 9:07=A0am, Rob Kennedy <m...@[EMAIL PROTECTED]
> wrote:
> Amir wrote:
> > I have a code like this:
>
> > var
> > =A0 rgsa: Array[0..0] of TSafeArrayBound;
> > =A0 I1,V1, V2, V3: SmallInt;
> > =A0 P1, P2: PSmallInt;
> > =A0 P3: Pointer;
> > =A0 Succeed: THandle;
> > =A0 Str1: String;
> > =A0 Vr1: Variant;
> > begin
> > // Creating array
> > =A0 rgsa[0].cElements:=3D 5;
> > =A0 rgsa[0].lLbound:=3D0;
> > =A0 PS1:=3D SafeArrayCreate(VT_I2,1,rgsa);
>
> > //Writing to array
> > =A0 V1:=3D 110;
> > =A0 P1:=3D @[EMAIL PROTECTED]
> > =A0 Succeed:=3D SafeArrayPutElement(PS1,[0,1],P1);
>
> That's not right. The last parameter should be V1, not P1.
>
> The last parameter is an untyped const in the Delphi declaration, which
> means that you should pass the actual *value* you want stored in the
> array. MSDN says to pass a pointer to the value, but the compiler takes
> care of that detail for you.
>
> As it is, you've give a four-byte value (the pointer P1) and the OS
> stores it into space reserved for a two-byte value.
>
> Also, the function returns an HResult, not a THandle, so change the
> declaration of Succeed. And then check the return value before
> proceeding with the rest of the code.
>
> > //Reading from array
> > =A0 Succeed:=3D SafeArrayGetElement(PS1,[0,1],P3);
>
> That's not right, either. Just like before, you should pass the actual
> variable you want to receive the SmallInt value from the array. In this
> case, that's V3. MSDN says to pass a pointer, but the Delphi declaration
> is an untyped var parameter, so the compiler takes care of the pointer
> stuff for you.
>
> Also, why are you passing an array of two indices for the second
> parameter? It's only a one-dimensional array.
>
> > =A0 P2:=3DPSmallInt(P3);
> > =A0 VR1:=3D P2^; =A0 =A0 =A0 =A0// It seems this line has no effect
but=
it's not.
> > =A0 V3:=3D P2^;
> > =A0 Label3.Caption:=3D IntToStr(V3);
>
> > In this case I don't have any problem and the value of V3 is 110, but
> > when I omit the " VR1:=3D P2^;", a line that seems has no effect, the
> > value of V3 will corrupt.
> > What is the resone???
>
> Well, you were calling the functions wrong, so it's nor surprising that
> you didn't get the expected results. And since part of what you were
> doing wrong involved reading and writing memory that wasn't supposed to
> be accessed, it's no surprise that other seemingly unrelated code
> changed the behavior of your program.
>
> --
> Rob- Hide quoted text -
>
> - Show quoted text -
Thanks for your complete writing
All of my problems solved but my plan was creating a one-dimentional
array. What was my mistake for creating that?


|