2016-08-17 28 views
0

私はdllを管理することができます。このdllには、カスタム構造を返すメソッドが含まれています。vbscriptのカスタム.net dllからの呼び出し方法

方法と構造

[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) 

私はこのスクリプトを実行すると、私は例外を持っています。

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

しかし

、私は構造、構造のみのフィールドを返すことができないのはなぜ return spi.TimeRemainingとして GetSystemPowerInformation方法書き換えは、このスクリプトは正しい TimeRemaining

を返す場合は?

+0

'set'キーワードが欠けていますか?すなわち、 'Set spi = calc.GetSystemPowerInformation()' –

答えて

1

VBScriptはVariant型のみをサポートし、Variant型は構造体をサポートしていません。 spiへの値割当後、VarType(spi)の結果は何ですか?バイト配列の場合は、TimeRemaining値を表す4バイトを取得し、それらをuintに変換しようとします。

関連する問題