C# 调用API,实现注销远程登录本机的用户。

None.gif using  System;
None.gif
using  System.Management;
None.gif
using  System.Runtime;
None.gif
using  System.Runtime.InteropServices;
None.gif
None.gif
namespace  TSConsoleApplication
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// TSControl 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class TSControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Terminal Services API Functions,The WTSEnumerateSessions function retrieves a list of sessions on a specified terminal server,
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="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</param>
InBlock.gif        
/// <param name="Reserved">Reserved; must be zero</param>
InBlock.gif        
/// <param name="Version">[in] Specifies the version of the enumeration request. Must be 1. </param>
InBlock.gif        
/// <param name="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. 
InBlock.gif        
/// To be able to enumerate a session, you need to have the Query Information permission.</param>
InBlock.gif        
/// <param name="pCount">[out] Pointer to the variable that receives the number of WTS_SESSION_INFO structures returned in the ppSessionInfo buffer. </param>
ExpandedSubBlockEnd.gif        
/// <returns>If the function succeeds, the return value is a nonzero value.    If the function fails, the return value is zero</returns>

InBlock.gif        [DllImport("wtsapi32", CharSet=CharSet.Auto, SetLastError=true)]
InBlock.gif        
private static  extern bool WTSEnumerateSessions(int hServer, int Reserved,int Version, ref long ppSessionInfo,ref int pCount); 
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Terminal Services API Functions,The WTSFreeMemory function frees memory allocated by a Terminal Services function.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="pMemory">[in] Pointer to the memory to free</param>

InBlock.gif        [DllImport("wtsapi32.dll")]
InBlock.gif        
private static extern void WTSFreeMemory(System.IntPtr pMemory);
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Terminal Services API Functions,The WTSLogoffSession function logs off a specified Terminal Services session.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="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. </param>
InBlock.gif        
/// <param name="SessionId">[in] A Terminal Services session identifier. To indicate the current session, specify WTS_CURRENT_SESSION. You can use the WTSEnumerateSessions function to retrieve the identifiers of all sessions on a specified terminal server. 
InBlock.gif        
/// To be able to log off another user's session, you need to have the Reset permission    </param>
InBlock.gif        
/// <param name="bWait">[in] Indicates whether the operation is synchronous.
InBlock.gif        
/// If bWait is TRUE, the function returns when the session is logged off.
InBlock.gif        
/// If bWait is FALSE, the function returns immediately.</param>
InBlock.gif        
/// <returns>If the function succeeds, the return value is a nonzero value.
ExpandedSubBlockEnd.gif        
/// If the function fails, the return value is zero.</returns>

InBlock.gif        [DllImport("wtsapi32.dll")]
InBlock.gif        
public static extern bool WTSLogoffSession(int hServer, long SessionId, bool bWait);
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The WTS_CONNECTSTATE_CLASS enumeration type contains INT values that indicate the connection state of a Terminal Services session.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public enum WTS_CONNECTSTATE_CLASS
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            WTSActive,
InBlock.gif            WTSConnected,
InBlock.gif            WTSConnectQuery,
InBlock.gif            WTSShadow,
InBlock.gif            WTSDisconnected,
InBlock.gif            WTSIdle,
InBlock.gif            WTSListen,
InBlock.gif            WTSReset,
InBlock.gif            WTSDown,
InBlock.gif            WTSInit,
ExpandedSubBlockEnd.gif        }
                                                                                                      
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The WTS_SESSION_INFO structure contains information about a client session on a terminal server.
InBlock.gif        
/// if the WTS_SESSION_INFO.SessionID==0, it means that the SESSION is the local logon user's session.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public struct WTS_SESSION_INFO
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public int SessionID;
InBlock.gif            [MarshalAs(UnmanagedType.LPTStr)] 
public string pWinStationName;
InBlock.gif            
public WTS_CONNECTSTATE_CLASS state;            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The SessionEnumeration function retrieves a list of WTS_SESSION_INFO on a current terminal server.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>a list of WTS_SESSION_INFO on a current terminal server</returns>

InBlock.gif        public static WTS_SESSION_INFO[] SessionEnumeration() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
//Set handle of terminal server as the current terminal server
InBlock.gif
            int hServer = 0;
InBlock.gif            
bool RetVal;
InBlock.gif            
long lpBuffer=0;
InBlock.gif            
int Count=0;
InBlock.gif            
long p;
InBlock.gif            WTS_SESSION_INFO Session_Info
=new WTS_SESSION_INFO();
InBlock.gif            WTS_SESSION_INFO[] arrSessionInfo;
InBlock.gif            RetVal 
= WTSEnumerateSessions(hServer, 01,ref lpBuffer, ref Count);
InBlock.gif            arrSessionInfo
=new WTS_SESSION_INFO[0];
InBlock.gif            
if(RetVal)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                arrSessionInfo
=new WTS_SESSION_INFO[Count];
InBlock.gif                
int i;
InBlock.gif                p 
= lpBuffer;
InBlock.gif                
for( i = 0;i< Count ;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    arrSessionInfo[i] 
= (WTS_SESSION_INFO)Marshal.PtrToStructure(new IntPtr(p), Session_Info.GetType());
InBlock.gif                    p 
+= Marshal.SizeOf(Session_Info.GetType());                    
ExpandedSubBlockEnd.gif                }

InBlock.gif                WTSFreeMemory(
new IntPtr(lpBuffer));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Insert Error Reaction Here            
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
return arrSessionInfo;            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public TSControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
InBlock.gif

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Class1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加代码以启动应用程序
InBlock.gif            
//
InBlock.gif

InBlock.gif            TSControl.WTS_SESSION_INFO[] pSessionInfo
=TSControl.SessionEnumeration();
InBlock.gif            
InBlock.gif            
for(int i=0;i<pSessionInfo.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(pSessionInfo[i].SessionID!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
while(TSControl.WTSLogoffSession(0,pSessionInfo[i].SessionID, true))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            System.Threading.Thread.Sleep(
3000);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Console.WriteLine(ex.Message);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
    
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

 

 

转载于:https://www.cnblogs.com/skyfei/archive/2005/03/30/128436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值