Board index » delphi » Problems with "var pointerVariable: Pointer" parameters

Problems with "var pointerVariable: Pointer" parameters

In the imported type library definition for the DirectX 7 COM components I discovered that several of the imported functions were created with "var pointerVariable: Pointer" (ie, pointer pass-by-reference). But whenever I attempt to pass a pointer to the function defined in this way it always returned in error (and causes memory access violations).  

After quite a bit of trial-and-error I discovered that if I modified the function definition in the type library so that it was no longer requesting that the pointer be passed-by-reference everything worked great!

Does anyone happen to know why the original function definition that passed-by-reference doesn't work?  Would this be a bug in Delphi or a bug in the type library definition?

I've attached some code snippets for reference purposes:

<snip>

var
   DIDeviceCount: Integer;
   DIDeviceEnum: DirectInputEnumDevices;
   DIDeviceInstance: DirectInputDeviceInstance;
   DIDeviceInstanceGuid: WideString;
   DIPropertyDeadZone: ^DIPROPLONG;

<snip>

     { Initialize the "dead zone" structure. }
     New(DIPropertyDeadZone);
     DIPropertyDeadZone^.lSize := SizeOf(DIPROPLONG);
     DIPropertyDeadZone^.lHeaderSize := 0;
     DIPropertyDeadZone^.lHow := DIPH_BYOFFSET;

     { Set the "dead zone" for the X axis at 15% of the range travel. }
     DIPropertyDeadZone^.lData := 1500;
     DIPropertyDeadZone^.lObj := DIJOFS_X;

     { A problem occurs here if this function is defined as passing a var: Pointer. }
     DIDevice.SetProperty('DIPROP_DEADZONE', Pointer(DIPropertyDeadZone));

     { Free resources no longer in use. }
     Dispose(DIPropertyDeadZone);

<snip>

    { This modified definition from the type library works fine. }
    function SetProperty(const guid: WideString; propertyInfo: Pointer): HResult; stdcall;

    { This original type library definition causes memory access violations. }
    function SetProperty(const guid: WideString; var propertyInfo: Pointer): HResult; stdcall;

Thanks for the help!

Wendell W. Pinegar

 

Re:Problems with "var pointerVariable: Pointer" parameters


your passing your referrences wrong for VAR paramaters
 Var is looking for the address of the object.
what in have done in effect is force the compiler to generate an address
of the
pointers 4 byte storage plus the offset of your field..
you want to pass it like this..
DIpropertyDeadZone^

Quote
"Wendell W. Pinegar" wrote:
> In the imported type library definition for the DirectX 7 COM
> components I discovered that several of the imported functions were
> created with "var pointerVariable: Pointer" (ie, pointer
> pass-by-reference). But whenever I attempt to pass a pointer to the
> function defined in this way it always returned in error (and causes
> memory access violations).

> After quite a bit of trial-and-error I discovered that if I modified
> the function definition in the type library so that it was no longer
> requesting that the pointer be passed-by-reference everything worked
> great!

> Does anyone happen to know why the original function definition that
> passed-by-reference doesn't work?  Would this be a bug in Delphi or a
> bug in the type library definition?

> I've attached some code snippets for reference purposes:

> <snip>

> var
>    DIDeviceCount: Integer;
>    DIDeviceEnum: DirectInputEnumDevices;
>    DIDeviceInstance: DirectInputDeviceInstance;
>    DIDeviceInstanceGuid: WideString;
>    DIPropertyDeadZone: ^DIPROPLONG;

> <snip>

>      { Initialize the "dead zone" structure. }
>      New(DIPropertyDeadZone);
>      DIPropertyDeadZone^.lSize := SizeOf(DIPROPLONG);
>      DIPropertyDeadZone^.lHeaderSize := 0;
>      DIPropertyDeadZone^.lHow := DIPH_BYOFFSET;

>      { Set the "dead zone" for the X axis at 15% of the range travel.
> }
>      DIPropertyDeadZone^.lData := 1500;
>      DIPropertyDeadZone^.lObj := DIJOFS_X;

>      { A problem occurs here if this function is defined as passing a
> var: Pointer. }
>      DIDevice.SetProperty('DIPROP_DEADZONE',
> Pointer(DIPropertyDeadZone));

>      { Free resources no longer in use. }
>      Dispose(DIPropertyDeadZone);

> <snip>

>     { This modified definition from the type library works fine. }
>     function SetProperty(const guid: WideString; propertyInfo:
> Pointer): HResult; stdcall;

>     { This original type library definition causes memory access
> violations. }
>     function SetProperty(const guid: WideString; var propertyInfo:
> Pointer): HResult; stdcall;

> Thanks for the help!

> Wendell W. Pinegar

Re:Problems with "var pointerVariable: Pointer" parameters


Hi Wendell,

I've checked the generated assembler-code for your stdcall var-transfered parameter and it looks just fine. A pointer to a pointer is pushed onto the stack as it is your code suggests.

However. Are you sure, that the parameters shouldn't have been passed as pointer to DIPROPLONG (an not pointer to ^DIPROPLONG, the way you do). In this way it would make sence, that your redefinition of the Delphi-interface to the typelib works.

- Madsen -
    Wendell W. Pinegar skrev i meddelelsen <7p1c8h$i...@forums.borland.com>...
    In the imported type library definition for the DirectX 7 COM components I discovered that several of the imported functions were created with "var pointerVariable: Pointer" (ie, pointer pass-by-reference). But whenever I attempt to pass a pointer to the function defined in this way it always returned in error (and causes memory access violations).  

    After quite a bit of trial-and-error I discovered that if I modified the function definition in the type library so that it was no longer requesting that the pointer be passed-by-reference everything worked great!

    Does anyone happen to know why the original function definition that passed-by-reference doesn't work?  Would this be a bug in Delphi or a bug in the type library definition?

    I've attached some code snippets for reference purposes:

    <snip>

    var
       DIDeviceCount: Integer;
       DIDeviceEnum: DirectInputEnumDevices;
       DIDeviceInstance: DirectInputDeviceInstance;
       DIDeviceInstanceGuid: WideString;
       DIPropertyDeadZone: ^DIPROPLONG;

    <snip>

         { Initialize the "dead zone" structure. }
         New(DIPropertyDeadZone);
         DIPropertyDeadZone^.lSize := SizeOf(DIPROPLONG);
         DIPropertyDeadZone^.lHeaderSize := 0;
         DIPropertyDeadZone^.lHow := DIPH_BYOFFSET;

         { Set the "dead zone" for the X axis at 15% of the range travel. }
         DIPropertyDeadZone^.lData := 1500;
         DIPropertyDeadZone^.lObj := DIJOFS_X;

         { A problem occurs here if this function is defined as passing a var: Pointer. }
         DIDevice.SetProperty('DIPROP_DEADZONE', Pointer(DIPropertyDeadZone));

         { Free resources no longer in use. }
         Dispose(DIPropertyDeadZone);

    <snip>

        { This modified definition from the type library works fine. }
        function SetProperty(const guid: WideString; propertyInfo: Pointer): HResult; stdcall;

        { This original type library definition causes memory access violations. }
        function SetProperty(const guid: WideString; var propertyInfo: Pointer): HResult; stdcall;

    Thanks for the help!

    Wendell W. Pinegar

Re:Problems with "var pointerVariable: Pointer" parameters


Hi Wendell,

I've checked the generated assembler-code for your stdcall var-transfered
parameter and it looks just fine. A pointer to a pointer is pushed onto the
stack as it is your code suggests.

However. Are you sure, that the parameters shouldn't have been passed as
pointer to DIPROPLONG (an not pointer to ^DIPROPLONG, the way you do). In
this way it would make sence, that your redefinition of the Delphi-interface
to the typelib works.

- Madsen -
    Wendell W. Pinegar skrev i meddelelsen
<7p1c8h$i...@forums.borland.com>...
    In the imported type library definition for the DirectX 7 COM components
I discovered that several of the imported functions were created with "var
pointerVariable: Pointer" (ie, pointer pass-by-reference). But whenever I
attempt to pass a pointer to the function defined in this way it always
returned in error (and causes memory access violations).

    After quite a bit of trial-and-error I discovered that if I modified the
function definition in the type library so that it was no longer requesting
that the pointer be passed-by-reference everything worked great!

    Does anyone happen to know why the original function definition that
passed-by-reference doesn't work?  Would this be a bug in Delphi or a bug in
the type library definition?

    I've attached some code snippets for reference purposes:

    <snip>

    var
       DIDeviceCount: Integer;
       DIDeviceEnum: DirectInputEnumDevices;
       DIDeviceInstance: DirectInputDeviceInstance;
       DIDeviceInstanceGuid: WideString;
       DIPropertyDeadZone: ^DIPROPLONG;

    <snip>

         { Initialize the "dead zone" structure. }
         New(DIPropertyDeadZone);
         DIPropertyDeadZone^.lSize := SizeOf(DIPROPLONG);
         DIPropertyDeadZone^.lHeaderSize := 0;
         DIPropertyDeadZone^.lHow := DIPH_BYOFFSET;

         { Set the "dead zone" for the X axis at 15% of the range travel. }
         DIPropertyDeadZone^.lData := 1500;
         DIPropertyDeadZone^.lObj := DIJOFS_X;

         { A problem occurs here if this function is defined as passing a
var: Pointer. }
         DIDevice.SetProperty('DIPROP_DEADZONE',
Pointer(DIPropertyDeadZone));

         { Free resources no longer in use. }
         Dispose(DIPropertyDeadZone);

    <snip>

        { This modified definition from the type library works fine. }
        function SetProperty(const guid: WideString; propertyInfo: Pointer):
HResult; stdcall;

        { This original type library definition causes memory access
violations. }
        function SetProperty(const guid: WideString; var propertyInfo:
Pointer): HResult; stdcall;

    Thanks for the help!

    Wendell W. Pinegar

Re:Problems with "var pointerVariable: Pointer" parameters


Madsen,

Thanks!  It ended up being a problem with the type library definition.
Evidentially the type library says it wants a pointer-to-a-pointer when it
simply wants a pointer...how strange.  :o)

Quote
Mads Madsen <mmad...@globalnet.co.uk> wrote in message

news:7p1rag$ijd3@forums.borland.com...
Quote
> Hi Wendell,

> I've checked the generated assembler-code for your stdcall var-transfered
> parameter and it looks just fine. A pointer to a pointer is pushed onto
the
> stack as it is your code suggests.

> However. Are you sure, that the parameters shouldn't have been passed as
> pointer to DIPROPLONG (an not pointer to ^DIPROPLONG, the way you do). In
> this way it would make sence, that your redefinition of the
Delphi-interface
> to the typelib works.

> - Madsen -
>     Wendell W. Pinegar skrev i meddelelsen
> <7p1c8h$i...@forums.borland.com>...
>     In the imported type library definition for the DirectX 7 COM
components
> I discovered that several of the imported functions were created with "var
> pointerVariable: Pointer" (ie, pointer pass-by-reference). But whenever I
> attempt to pass a pointer to the function defined in this way it always
> returned in error (and causes memory access violations).

>     After quite a bit of trial-and-error I discovered that if I modified
the
> function definition in the type library so that it was no longer
requesting
> that the pointer be passed-by-reference everything worked great!

>     Does anyone happen to know why the original function definition that
> passed-by-reference doesn't work?  Would this be a bug in Delphi or a bug
in
> the type library definition?

>     I've attached some code snippets for reference purposes:

>     <snip>

>     var
>        DIDeviceCount: Integer;
>        DIDeviceEnum: DirectInputEnumDevices;
>        DIDeviceInstance: DirectInputDeviceInstance;
>        DIDeviceInstanceGuid: WideString;
>        DIPropertyDeadZone: ^DIPROPLONG;

>     <snip>

>          { Initialize the "dead zone" structure. }
>          New(DIPropertyDeadZone);
>          DIPropertyDeadZone^.lSize := SizeOf(DIPROPLONG);
>          DIPropertyDeadZone^.lHeaderSize := 0;
>          DIPropertyDeadZone^.lHow := DIPH_BYOFFSET;

>          { Set the "dead zone" for the X axis at 15% of the range
travel. }
>          DIPropertyDeadZone^.lData := 1500;
>          DIPropertyDeadZone^.lObj := DIJOFS_X;

>          { A problem occurs here if this function is defined as passing a
> var: Pointer. }
>          DIDevice.SetProperty('DIPROP_DEADZONE',
> Pointer(DIPropertyDeadZone));

>          { Free resources no longer in use. }
>          Dispose(DIPropertyDeadZone);

>     <snip>

>         { This modified definition from the type library works fine. }
>         function SetProperty(const guid: WideString; propertyInfo:
Pointer):
> HResult; stdcall;

>         { This original type library definition causes memory access
> violations. }
>         function SetProperty(const guid: WideString; var propertyInfo:
> Pointer): HResult; stdcall;

>     Thanks for the help!

>     Wendell W. Pinegar

Re:Problems with "var pointerVariable: Pointer" parameters


Ooops.  Correction.  This is a bug with the way Delphi 4 defines void *
pointers in the type library import.  The original C prototype for this
function is:

HRESULT SetProperty([in] BSTR guid,[in] void *propertyInfo);

I'll post a new thread on this subject...

Sincerely,

Wendell W. Pinegar

Wendell W. Pinegar <wpine...@worldnet.att.net> wrote in message
news:7p2rhi$ivq16@forums.borland.com...

Quote
> Madsen,

> Thanks!  It ended up being a problem with the type library definition.
> Evidentially the type library says it wants a pointer-to-a-pointer when it
> simply wants a pointer...how strange.  :o)

> Mads Madsen <mmad...@globalnet.co.uk> wrote in message
> news:7p1rag$ijd3@forums.borland.com...
> > Hi Wendell,

> > I've checked the generated assembler-code for your stdcall
var-transfered
> > parameter and it looks just fine. A pointer to a pointer is pushed onto
> the
> > stack as it is your code suggests.

> > However. Are you sure, that the parameters shouldn't have been passed as
> > pointer to DIPROPLONG (an not pointer to ^DIPROPLONG, the way you do).
In
> > this way it would make sence, that your redefinition of the
> Delphi-interface
> > to the typelib works.

> > - Madsen -
> >     Wendell W. Pinegar skrev i meddelelsen
> > <7p1c8h$i...@forums.borland.com>...
> >     In the imported type library definition for the DirectX 7 COM
> components
> > I discovered that several of the imported functions were created with
"var
> > pointerVariable: Pointer" (ie, pointer pass-by-reference). But whenever
I
> > attempt to pass a pointer to the function defined in this way it always
> > returned in error (and causes memory access violations).

> >     After quite a bit of trial-and-error I discovered that if I modified
> the
> > function definition in the type library so that it was no longer
> requesting
> > that the pointer be passed-by-reference everything worked great!

> >     Does anyone happen to know why the original function definition that
> > passed-by-reference doesn't work?  Would this be a bug in Delphi or a
bug
> in
> > the type library definition?

> >     I've attached some code snippets for reference purposes:

> >     <snip>

> >     var
> >        DIDeviceCount: Integer;
> >        DIDeviceEnum: DirectInputEnumDevices;
> >        DIDeviceInstance: DirectInputDeviceInstance;
> >        DIDeviceInstanceGuid: WideString;
> >        DIPropertyDeadZone: ^DIPROPLONG;

> >     <snip>

> >          { Initialize the "dead zone" structure. }
> >          New(DIPropertyDeadZone);
> >          DIPropertyDeadZone^.lSize := SizeOf(DIPROPLONG);
> >          DIPropertyDeadZone^.lHeaderSize := 0;
> >          DIPropertyDeadZone^.lHow := DIPH_BYOFFSET;

> >          { Set the "dead zone" for the X axis at 15% of the range
> travel. }
> >          DIPropertyDeadZone^.lData := 1500;
> >          DIPropertyDeadZone^.lObj := DIJOFS_X;

> >          { A problem occurs here if this function is defined as passing
a
> > var: Pointer. }
> >          DIDevice.SetProperty('DIPROP_DEADZONE',
> > Pointer(DIPropertyDeadZone));

> >          { Free resources no longer in use. }
> >          Dispose(DIPropertyDeadZone);

> >     <snip>

> >         { This modified definition from the type library works fine. }
> >         function SetProperty(const guid: WideString; propertyInfo:
> Pointer):
> > HResult; stdcall;

> >         { This original type library definition causes memory access
> > violations. }
> >         function SetProperty(const guid: WideString; var propertyInfo:
> > Pointer): HResult; stdcall;

> >     Thanks for the help!

> >     Wendell W. Pinegar

Other Threads