0

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() Commented Aug 18, 2016 at 9:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.