2012-03-22 10 views
0

このコードを実行してこのエラーが発生した場合、ここで何が起こっているか把握するのに役立つでしょうか? sessionDatanullあるため.NET C/C++ P/INVOKE例外

enter image description here

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using System.Runtime.InteropServices; 

namespace LSATest 
{ 
class Program 
{ 


    public static List<string> listBox1 = new List<string>(); 
    static void Main(string[] args) 
    { 
     DateTime systime = new DateTime(1601, 1, 1, 0, 0, 0, 0); //win32 systemdate 

     UInt64 count; 
     IntPtr luidPtr = IntPtr.Zero; 
     LSAClass.LsaEnumerateLogonSessions(out count, out luidPtr); //gets an array of pointers to LUIDs 

     IntPtr iter = luidPtr;  //set the pointer to the start of the array 

     for (ulong i = 0; i < count; i++) //for each pointer in the array 
     { 
      IntPtr sessionData; 

      LSAClass.LsaGetLogonSessionData(iter, out sessionData); 

      LSAClass.SECURITY_LOGON_SESSION_DATA data = (LSAClass.SECURITY_LOGON_SESSION_DATA)Marshal.PtrToStructure(sessionData, typeof(LSAClass.SECURITY_LOGON_SESSION_DATA)); 

      //if we have a valid logon 
      if (data.PSiD != IntPtr.Zero) 
      { 
       //get the security identifier for further use 
       System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(data.PSiD); 

       //extract some useful information from the session data struct 
       string username = Marshal.PtrToStringUni(data.Username.buffer).Trim();  //get the account username 
       string domain = Marshal.PtrToStringUni(data.LoginDomain.buffer).Trim(); //domain for this account 
       string authpackage = Marshal.PtrToStringUni(data.AuthenticationPackage.buffer).Trim(); //authentication package 

       LSAClass.SECURITY_LOGON_TYPE secType = (LSAClass.SECURITY_LOGON_TYPE)data.LogonType; 
       DateTime time = systime.AddTicks((long)data.LoginTime);     //get the datetime the session was logged in 

       //do something with the extracted data, ie, add to a display control.... 
       listBox1.Add("User: " + username + " *** Domain: " + domain + " *** Login Type: (" + data.LogonType + ") " + secType.ToString() + " *** Login Time: " + time.ToLocalTime().ToString()); 

      } 
      iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LSAClass.LUID))); //move the pointer forward 
      LSAClass.LsaFreeReturnBuffer(sessionData); //free the SECURITY_LOGON_SESSION_DATA memory in the struct 
     } 
     LSAClass.LsaFreeReturnBuffer(luidPtr); //free the array of LUIDs 

    } 
} 

}

+0

これはCでもC++でもありません。これはC#です。 –

答えて

1

エラーです。これは、LsaGetLogonSessionDataが失敗しているためです。これをさらに診断するには、API関数の戻り値に注意を払い、エラーが発生した場合にエラーコードをチェックする必要があります。それはあなたの次のステップです。

脇に、count変数が誤って宣言されています。 Cヘッダーファイルでは、と宣言されています。それはC#でuintになる符号なし32ビット整数です。私はこれ以上何もチェックしていない。

関連する問題