develooper Front page | perl.perl6.users | Postings from December 2022

Re: NativeCall and pointers question

Thread Previous
ToddAndMargo via perl6-users
December 1, 2022 07:12
Re: NativeCall and pointers question
Message ID:
On 11/30/22 12:53, ToddAndMargo via perl6-users wrote:
> Let me ask this question a little simpler:

To answer my own questions, which I figured out
the hard way.

> 1) how to I tell NativeCall I only want the
> C pointer back, not what it points to?

By declaring it as a pointer and creating it with
".new".   No ".new", and you get back garbage.

sub GetWTSEnumerateSession(
        BOOL WTSEnumerateSessionsA(
          [in]  HANDLE             hServer,
          [in]  DWORD              Reserved,
          [in]  DWORD              Version,
          [out] PWTS_SESSION_INFOA *ppSessionInfo,
          [out] DWORD              *pCount
        Returns zero if this function fails.
    DWORD $hServer,               # [in]  HANDLE
    DWORD $Reserved,              # [in] always 0
    DWORD $Version,               # [in] always 1
    Pointer $ppSessionInf is rw,  # [out] <-------------------
    DWORD $pCount         is rw   # [out] DWORD
    is native("Wtsapi32.dll")
    is symbol("WTSEnumerateSessionsA")
    returns DWORD  # If the function fails, the return value is zero.
    { * };

    my $ppSessionInfo =;

> 2) how do I tell NativeCall I am sending it
> a C pointer?

Declare it a Pointer, same as above.  See

    my $ppSessionInfo

Do not forget the .new.  There are no
error warnings if you forget.  The resulting
garbage yo get back will drive you nuts,
especially when the next API call that
reuses that point will cause your program to
exit with not explanation.

Yours in confusion (but not this, finally),

Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About