Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Borland Delphi > Re: PSafeArray
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 11 Topic 3834 of 3952
Post > Topic >>

Re: PSafeArray

by Amir <eghtedari.amir@[EMAIL PROTECTED] > Jul 13, 2008 at 01:38 AM

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?
 




 11 Posts in Topic:
PSafeArray
Amir <eghtedari.amir@[  2008-07-10 23:18:52 
Re: PSafeArray
Rob Kennedy <me3@[EMAI  2008-07-11 08:22:28 
Re: PSafeArray
Amir <eghtedari.amir@[  2008-07-12 20:14:50 
Re: PSafeArray
Rob Kennedy <me3@[EMAI  2008-07-13 00:07:56 
Re: PSafeArray
Amir <eghtedari.amir@[  2008-07-13 01:38:38 
Re: PSafeArray
Amir <eghtedari.amir@[  2008-07-13 04:41:31 
Re: PSafeArray
Rob Kennedy <me3@[EMAI  2008-07-13 13:08:10 
Re: PSafeArray
Amir <eghtedari.amir@[  2008-07-13 22:28:28 
Re: PSafeArray
Rob Kennedy <me3@[EMAI  2008-07-14 01:15:15 
Re: PSafeArray
MisterX <kadishmal@[EM  2008-08-28 05:24:12 
Re: PSafeArray
Rob Kennedy <me3@[EMAI  2008-08-28 23:39:49 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 20:17:38 CDT 2008.