Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

WAD @WINAPI with PINT64 gives only the high DWORD

May
15,174
274
Below, _AGENOW uses GetSystemTimePreciseAsFileTime. @WINAPI gives only the high DWORD.

Code:
v:\> echo %@winapi[kernel32,GetSystemTimePreciseAsFileTime,pint64]
31291232

v:\> echo %@eval[31291232=h]
1DD7760

v:\> echo %@eval[%_agenow=h]
1DD77604DA271FD
 
Yep.

Code:
echo %@winapi[kernel32,GetSystemTimePreciseAsFileTime,pint64] 
31291239

echo %@eval[31291239=h]
1DD7767

echo %{pshell test.ps1} 
134394848605158668

echo %@eval[%{pshell test.ps1}=h]
1DD77671ACCCE74


Code:
if (-not ('PreciseSystemTime' -as [type])) {
    Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;

public static class PreciseSystemTime
{
    [StructLayout(LayoutKind.Sequential)]
    private struct FILETIME
    {
        public uint LowDateTime;
        public uint HighDateTime;
    }

    [DllImport("kernel32.dll", ExactSpelling = true)]
    private static extern void GetSystemTimePreciseAsFileTime(out FILETIME fileTime);

    public static long GetFileTimeTicks()
    {
        FILETIME ft;
        GetSystemTimePreciseAsFileTime(out ft);
        return ((long)ft.HighDateTime << 32) | ft.LowDateTime;
    }
}
'@
}

$ticks = [PreciseSystemTime]::GetFileTimeTicks()

$ticks

Joe
 
@Joe Caverly, nice!. I can (sort of) work around it by using buffer instead of pint64 and deciphering the buffer myself.

Code:
v:\> set x=%@unicode[%@winapi[kernel32,GetSystemTimePreciseAsFileTime,buffer]] & set y=%_agenow

v:\> echo %@eval[(%@word[3,%x] * 2**48) + (%@word[2,%x] * 2**32) + (%@word[1,%x] * 2**16) + %@word[0,%x]=h]
1DD7763CDA7B569

v:\> echo %@eval[%y=h]
1DD7763CDA7C6A2

But (1) with probability about 1/65536 the buffer (a string) will contain a Unicode nul and (2) the buffer is apparently not 0-initialized so you might get extra stuff. Below, expecting 4 characters and getting 7.

Code:
v:\> echo %@ascii[%@winapi[kernel32,GetSystemTimePreciseAsFileTime,buffer]]
51952 47254 30561 477 60496 15122 659
 
Code:
@setlocal
@echo off
set x=%@winapi[kernel32,GetSystemTimePreciseAsFileTime,buffer]
set x=%@unicode[%x]

set low=%@eval[(%@word[1,%x] << 16) + %@word[0,%x]=h]
set high=%@eval[(%@word[3,%x] << 16) + %@word[2,%x]=h]

echo X   : %x
echo Low : %Low
echo High: %High
endlocal

So, if one of the FILETIME characters happens to contain 0000 (nul) then the string would terminate?
If none of the FILETIME characters contain 0000 (nul) then no string termination?

Which may be why I'm getting either 6 or 7 characters returned as you did?

Code:
R:\>test.btm
X   : 1232826 30573 477 16106 18603 32764
Low : 777FCFBA
High: 3EEA01DD

R:\>test.btm
X   : 17194 40907 30573 477 16106 18603 32764
Low : 9FCB432A
High: 1DD776D

R:\>test.btm
X   : 35153 42392 30573 477 16106 18603 32764
Low : A5988951
High: 1DD776D

Joe
 
Given how I'm processing it when I use "buffer", isn't it binary to begin with? How else might it work?

I mean specifically the binary buffers allocated via @BALLOC and read/written with @BPEEK and @BPOKE. If @WINAPI supported those, you could pass and return any kind of structure, and handle them without worrying about whether the data happens to represent nulls, percent signs, backquotes, and so on.

I'm thinking syntax like BBUF=%handle.
 
Interesting! But I dunno.

At least with @WINAPI, the buffers (pint, ... buffer, abuffer) are typed. So you get some help getting useful data out.

Actually, I'm using this.

Code:
set agenow=%@eval[%@makeage[%_date,%_time] + %_tzo * 60 * 10000000]

If pint64 were working correctly @WINAPI would be easier and cleaner. I have my own _AGENOW plugin but I'm writing a BTM that shouldn't rely on plugins.
 
Huh! What's PINT64 all about?

That is for passing a pointer to a 64-bit integer to the Windows API.

And what syntax is wrong?

The syntax is "PINT64=value".

The help says:

"The return value is either a string value returned by the API (if BUFFER or aBUFFER is specified), or the integer value returned by the API."

The help does not say anything about returning a 64-bit pointer or value. And in this instance the API doesn't return anything.

So you have derived an imaginary syntax for a non-existent feature.
 
Then what's happening here? GetSystemTimePreciseAsFileTime is writing something at a location given to it by TCC, and whats being echo'd
is exactly equal to the high DWORD of the precise system time as a file time.

Code:
v:\> echo %@eval[%@winapi[kernel32,GetSystemTimePreciseAsFileTime,pint64]=h]^
More? & echo %@eval[%_agenow=h]
1DD77C7
1DD77C7C579123B

It seems I could give a 64-bit value (PINT64=value) but the function doesn't require one. If the buffer is big enough to pass a 64-bit value then it's big enough to to hold a FILETIME. @WINAPI knows I said PINT64; can't it read all 8 bytes?

If I'm way off-base here, please enlighten me by explaining in gory detail what the code above is doing.

Here's what _AGENOW does.

Code:
INT WINAPI _AGENOW ( LPWSTR psz )
{
    ULONGLONG ft;
    GetSystemTimePreciseAsFileTime((FILETIME*) &ft);
    Sprintf(psz, L"%I64u", ft);
    return 0;
}
 
Then what's happening here? GetSystemTimePreciseAsFileTime is writing something at a location given to it by TCC, and whats being echo'd
is exactly equal to the high DWORD of the precise system time as a file time.

Code:
v:\> echo %@eval[%@winapi[kernel32,GetSystemTimePreciseAsFileTime,pint64]=h]^
More? & echo %@eval[%_agenow=h]
1DD77C7
1DD77C7C579123B

It seems I could give a 64-bit value (PINT64=value) but the function doesn't require one. If the buffer is big enough to pass a 64-bit value then it's big enough to to hold a FILETIME. @WINAPI knows I said PINT64; can't it read all 8 bytes?

If I'm way off-base here, please enlighten me by explaining in gory detail what the code above is doing.

You're way off base here.

First, you're passing a (null) pointer to a nonexistent variable to @WINAPI. The only reason it doesn't crash is because TCC is using a string here instead of an _int64.

Second, as the help clearly states, @WINAPI returns the integer return value of the API (unless you're using one of the buffer values). It does not return a pointer to any of the API arguments, which is what you would need for that particular API. That would require a new variable function.

Third, there isn't any point in explaining in gory detail, because the code doesn't do anything remotely like what you imagine it does.

Fourth, the fact that what you get back is half of the longlong you (didn't) pass is purely coincidental (because of TCC trying to retrieve the return value of a void function).

You're trying to spin a user error as a bug, so that you can disguise a feature request. Which you know won't happen in a beta. You can post feature requests in the Suggestions forum.
 
The API wrote 1DD77C7 somewhere and @WINAPI returned it. How did that happen?

I don't mind being wrong (I do that a lot) as long as I learn from it.
 
I don't know enough to comment further. This thread grew out of my desire to use @WINAPI on GetSystemTimePreciseAsFileTime.
 
I don't know enough to comment further. This thread grew out of my desire to use @WINAPI on GetSystemTimePreciseAsFileTime.

You cannot do that. There is no workaround. This is expected Windows behavior.

1. GetSystemTimePreciseAsFileTime is a void function:
VOID GetSystemTimePreciseAsFileTime( [out] LPFILETIME lpSystemTimeAsFileTime);

2. You are passing a null pointer as the lpSystemTimeAsFileTime value.

3. @WINAPI returns the value returned by the Windows API. That is NOT one of the arguments, it is the value returned by the function itself. This is 0 or 1 for most Windows APIs, but for one with a VOID definition, it is nothing at all. Trying to assign a void function return value to a variable will either crash, return a compiler error, or (as in this case) return a 32-bit value off the stack.

4. The returned value from the void function is not "almost what you wanted, except for the truncation." It is "nothing remotely related to what you wanted, except for the semi-random value you erroneously retrieved from the stack." It happens to look somewhat like what you wanted, but that's only because the function takes one argument. If it took two or more, you'd get a completely unrelated value on the stack.

In order for @WINAPI to work the way you apparently expect it to, you need to do this:

1. Contact Microsoft and demand that they change all of the APIs that are defined as VOID to LONGLONG.

2. And because that will now not work with the tens of thousands of APIs that are defined as INT, they all have to be changed to LONGLONG too.

3. Incorporate an AI agent in the API calls that will guess what argument you really wanted the API to return. Simple for GetSystemTimePreciseAsFileTime, because it only has one argument. A bit more complicated for APIs that have multiple arguments.

4. Once Microsoft has done this for you, let me know and I will update TCC accordingly.

Or, you could have written a plugin to query that API. In 1/10 of the time you've spent trying to force @WINAPI to do something it will never do.
 
Thanks for that. I already have a plugin _AGENOW which does it. FWIW ...

I had a little fun getting around my difficulty with another plugin that might come in handy again.

Code:
v:\> help @buftoint

@BUFTOINT[buffer,size] size = 1, 2, 4, 8

It's 10 lines of code with memcpy doing the work.

Code:
v:\> echo %@buftoint[%@winapi[kernel32,GetSystemTimePreciseAsFileTime,buffer],8] & echo %_agenow
134395782729243948
134395782729286430

In a script where I didn't want to use plugins, I used this.

Code:
set agenow=%@eval[%@makeage[%_date,%_time] + %_tzo * 60 * 10000000]
 
Back
Top