我想用WTSEnumerateSessions()函数枚举会话,就如同net session命令和[共享文件夹-会话]功能一样。但是只能枚举出一个本机的,不能枚举出其它远程用户的,这是为什么呢?我是vs2008 c# winxp环境。附程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;//using System.Management;
using System.Runtime;
using System.Runtime.InteropServices;namespace AutoLogOffSession
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            TSControl.WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration();            for (int i = 0; i < pSessionInfo.Length; i++)
            {
                //if (pSessionInfo[i].SessionID != 0)
                {
                    try
                    {
                        int count = 0;
                        IntPtr buffer = IntPtr.Zero;
                        StringBuilder sb = new StringBuilder();
                        bool bsuccess = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSUserName, out sb, out count);
                        if (bsuccess)
                        {
                            MessageBox.Show(sb.ToString());
                        }
                         bsuccess = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, TSControl.WTSInfoClass.WTSConnectState, out sb, out count);
                        if (bsuccess)
                        {
                            MessageBox.Show(sb.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }
    public class TSControl
    {
         [DllImport("wtsapi32", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool WTSEnumerateSessions(int hServer, int Reserved, int Version, ref long ppSessionInfo, ref int pCount);        [DllImport("wtsapi32.dll")]
        public static extern void WTSFreeMemory(System.IntPtr pMemory);        [DllImport("wtsapi32.dll")]
        public static extern bool WTSLogoffSession(int hServer, long SessionId, bool bWait);
        [DllImport("Wtsapi32.dll")]
        public static extern bool WTSQuerySessionInformation(
            System.IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out StringBuilder ppBuffer, out int pBytesReturned);        public enum WTSInfoClass
        {
            WTSInitialProgram,
            WTSApplicationName,
            WTSWorkingDirectory,
            WTSOEMId,
            WTSSessionId,
            WTSUserName,
            WTSWinStationName,
            WTSDomainName,
            WTSConnectState,
            WTSClientBuildNumber,
            WTSClientName,
            WTSClientDirectory,
            WTSClientProductId,
            WTSClientHardwareId,
            WTSClientAddress,
            WTSClientDisplay,
            WTSClientProtocolType
        }        public enum WTS_CONNECTSTATE_CLASS
        {
            WTSActive,
            WTSConnected,
            WTSConnectQuery,
            WTSShadow,
            WTSDisconnected,
            WTSIdle,
            WTSListen,
            WTSReset,
            WTSDown,
            WTSInit,
        }        public struct WTS_SESSION_INFO
        {
            public int SessionID;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string pWinStationName;
            public WTS_CONNECTSTATE_CLASS state;
        }        public static WTS_SESSION_INFO[] SessionEnumeration()
        {
            //Set handle of terminal server as the current terminal server 
            int hServer = 0;
            bool RetVal;
            long lpBuffer = 0;
            int Count = 0;
            long p;
            WTS_SESSION_INFO Session_Info = new WTS_SESSION_INFO();
            WTS_SESSION_INFO[] arrSessionInfo;
            RetVal = WTSEnumerateSessions(hServer, 0, 1, ref lpBuffer, ref Count);
            arrSessionInfo = new WTS_SESSION_INFO[0];
            if (RetVal)
            {
                arrSessionInfo = new WTS_SESSION_INFO[Count];
                int i;
                p = lpBuffer;
                for (i = 0; i < Count; i++)
                {
                    arrSessionInfo[i] = (WTS_SESSION_INFO)Marshal.PtrToStructure(new IntPtr(p), Session_Info.GetType());
                    p += Marshal.SizeOf(Session_Info.GetType());
                }
                WTSFreeMemory(new IntPtr(lpBuffer));
            }
            else
            {
                //Insert Error Reaction Here 
            }
            return arrSessionInfo;
        }
    }
}

解决方案 »

  1.   

    WTSEnumerateSessions
    The WTSEnumerateSessions function retrieves a list of sessions on a specified terminal server.BOOL WTSEnumerateSessions(
      HANDLE hServer,
      DWORD Reserved,
      DWORD Version,
      PWTS_SESSION_INFO* ppSessionInfo,
      DWORD* pCount
    );
    Parameters
    hServer 
    [in] Handle to a terminal server. Specify a handle opened by the WTSOpenServer function, or specify WTS_CURRENT_SERVER_HANDLE to indicate the terminal server on which your application is running. 
    Reserved 
    Reserved; must be zero. 
    Version 
    [in] Specifies the version of the enumeration request. Must be 1. 
    ppSessionInfo 
    [out] Pointer to a variable that receives a pointer to an array of WTS_SESSION_INFO structures. Each structure in the array contains information about a session on the specified terminal server. To free the returned buffer, call the WTSFreeMemory function. 
    To be able to enumerate a session, you need to have the Query Information permission. To modify permissions on a session, use the Terminal Services Configuration administrative tool. pCount 
    [out] Pointer to the variable that receives the number of WTS_SESSION_INFO structures returned in the ppSessionInfo buffer. 
    请检查你传入的一个参数:pCount 
      

  2.   

    count是1 我强制改成2出错 应该是只枚举出1个msdn我看过了,没啥东西,也没找到sample
      

  3.   

    可以采用抓包的方式,获取网内的计算机信息, 利用winpcap sdk !!!
      

  4.   

    LS:
    额 这个太高难了,俺现在还做不了俺想用netsessionenum 或WTSEnumerateSessions来实现不知道行不行
      

  5.   

    并贴,讨论请到这里
    http://topic.csdn.net/u/20090311/11/9d4ac638-ec8c-4db5-8c34-9541e8c808c7.html