0
votes

I can managed dll. This dll contains method such return custom structure.

Method and Structure

    [DllImport("powrprof.dll")]
            private static extern uint CallNtPowerInformation(
                int InformationLevel,
                IntPtr lpInputBuffer,
                int nInputBufferSize,
                ref SystemPowerInformation spi,
                int nOutputBufferSize
            );

public SystemPowerInformation GetSystemPowerInformation()
        {
            SystemPowerInformation spi = new SystemPowerInformation();

            CallNtPowerInformation(12,
                IntPtr.Zero,
                0,
                ref spi,
                Marshal.SizeOf(spi));

            return spi;
        }

[ComVisible(true)]
    [StructLayout(LayoutKind.Sequential)]
    [Guid("A79B0F9F-00C7-46C8-A3AE-D371E09ADB0C")]
    public struct SystemPowerInformation
    {
        public uint MaxIdlenessAllowed;
        public uint Idleness;
        public uint TimeRemaining;
        public byte CoolingMode;
    }

VbScript

set calc = CreateObject("PowerStateManagement.PowerStateManagement")

sleepTime = calc.GetLastSleepTime()
WScript.Echo(sleepTime)

wakeTime = calc.GetLastWakeTime()
WScript.Echo(wakeTime)

spi = calc.GetSystemPowerInformation()

WScript.Echo(spi.TimeRemaining)

I have exception when I run this script.

0x800a01a8 - Microsoft VBScript runtime error: Object required: 'spi'

But if GetSystemPowerInformation method rewrite as return spi.TimeRemainingthen this script return correct TimeRemaining value

Why I can't return structure, only structure's field?

1
Are you just missing the set keyword? ie Set spi = calc.GetSystemPowerInformation()oracle certified professional

1 Answers

1
votes

VBScript only supports the Variant type, and the Variant type does not support structures. After the value assignment to spi, what is the result of VarType(spi)? If it's a byte array you can grab the 4 bytes representing the TimeRemaining value and attempt to convert them into a uint.