2017-06-01 3 views
-1

画面がロックされたときにキャッチしようとしていて、PowerShellに取り込もうとするたびにC#のスニペットが表示されます。異なるエラー:PowerShellからC#を実行しようとしたときにエラーが発生しました:無効なトークン '使用中'

$Assemblies = @('System', 'System.Messaging') 

$type = @" 
using System; 
using System.Messaging; 

public class Win32Session 
{ 
    private const int NOTIFY_FOR_THIS_SESSION = 0; 
    private const int WM_WTSSESSION_CHANGE = 0x2b1; 
    private const int WTS_SESSION_LOCK = 0x7; 
    private const int WTS_SESSION_UNLOCK = 0x8; 

    public event EventHandler MachineLocked; 
    public event EventHandler MachineUnlocked; 

    public Win32Session() 
    { 
     ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage; 
    } 

    void ComponentDispatcher_ThreadFilterMessage(ref MSG msg, ref bool handled) 
    { 
     if (msg.message == WM_WTSSESSION_CHANGE) 
     { 
      int value = msg.wParam.ToInt32(); 
      if (value == WTS_SESSION_LOCK) 
      { 
       OnMachineLocked(EventArgs.Empty); 
      } 
      else if (value == WTS_SESSION_UNLOCK) 
      { 
       OnMachineUnlocked(EventArgs.Empty); 
      } 
     } 
    } 

    protected virtual void OnMachineLocked(EventArgs e) 
    { 
     EventHandler temp = MachineLocked; 
     if (temp != null) 
     { 
      temp(this, e); 
     } 
    } 

    protected virtual void OnMachineUnlocked(EventArgs e) 
    { 
     EventHandler temp = MachineUnlocked; 
     if (temp != null) 
     { 
      temp(this, e); 
     } 
    } 
} 
"@ 

Add-Type -MemberDefinition $type -Name 'Eventing' -ReferencedAssemblies $Assemblies 

... here-stringに 'using'ステートメントを残しておけば、無効なトークンを使用しているというエラーが表示されます。私が 'using'ステートメントを削除して再実行すると、コンパイラはMSGタイプが何であるかを知らないという文句を言います。誰も助けることができますか?どうも!

+0

使用法が無効です(これは、 '-ReferencedAssemblies'がそこにあります)。私はMSGタイプが何であるかも知らない。それは参照されているアセンブリのいずれにも定義されていない。 – Adrian

+0

@Adrianアセンブリを参照することは、.csファイルに 'using'ディレクティブを追加することと同じではありません。 –

+0

@EdPlunkett true、これはPowerShellファイルであり、C#ファイルではありません。私はいくつかのより良い情報と答えを追加しました。 – Adrian

答えて

2

-UsingNamespace parameterを使用して、usingディレクティブがC#ファイル内にあるという目的に役立てることができます。ただし、MSGタイプは、参照される名前空間では未定義のように見えます。

関連する問題