Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NativeCall and pointers question

1 view
Skip to first unread message

ToddAndMargo via perl6-users

unread,
Nov 30, 2022, 6:00:09 AM11/30/22
to perl6-users
Hi All,

In the following:

use NativeCall;
constant BYTE := uint8;
constant LPDWORD := uint64; # long pointer to a DWORD
constant LPSTR = CArray[BYTE]; # long pointer to a string
constant DWORD := uint32;
constant HANDLE = Pointer[void];

sub WTSOpenServerA(
#`{

https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsopenservera?redirectedfrom=MSDN
C++
HANDLE WTSOpenServerA(
[in] LPSTR pServerName
);
}

LPSTR $pServerName
)

is native("Wtsapi32.dll")
is symbol("WTSOpenServerA")
returns DWORD
{ * };


NativeCall is resolving the pointers automatically
for me. It is taking $pServerName, which is a
Long Pointer to String (LPSTR) and creating the
pointer for me. This is appreciated.

It also returns and resolves the value of “HANDLE”
which is a C Pointer DWORD..

Question 1: how do I tell NativeCall I actually
want the C Pointer of HANDLE back and not what
it resolves to?

Question 2: how do I give NativeCall a pointer
value and tell Native call I do not want it
resolved into another pointer?

Many thanks,
-T


ToddAndMargo via perl6-users

unread,
Nov 30, 2022, 4:00:04 PM11/30/22
to perl6...@perl.org
Let me ask this question a little simpler:

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

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

Many thanks,
-T

ToddAndMargo via perl6-users

unread,
Dec 1, 2022, 2:30:03 AM12/1/22
to perl6...@perl.org
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(
#`{
C++
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 = NativeCall::Types::Pointer.new();



>
> 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),
-T

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 new messages