Reputation: 160
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.TimeRemaining
then this script return correct TimeRemaining value
Why I can't return structure, only structure's field?
Upvotes: 0
Views: 287
Reputation: 9704
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.
Upvotes: 1