积极答复者
谁能帮我翻译一段C++到C#?调用wtsapi和非托管结构的传递和获取的问题。

问题
-
一下是C++源代码
// LogonUserList.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<Windows.h> #include<string.h> #include<WtsApi32.h> #include<iostream> #pragma comment(lib, "wtsapi32.lib") using namespace std; int _tmain(int argc, _TCHAR* argv[]) { DWORD count = 0; PWTS_SESSION_INFOW plBuffer = 0; long p = 0; bool success = WTSEnumerateSessions(0, 0, 1, &plBuffer, &count); WTS_SESSION_INFO * info =(WTS_SESSION_INFO *)malloc(sizeof(WTS_SESSION_INFO) * count); for (int i = 0; i < count; i++) { info[i] = plBuffer[i]; } LPTSTR pData; for (int i = 0; i < count; i++) { DWORD reted = 0; bool ret = WTSQuerySessionInformation(0, info[i].SessionId, WTSClientAddress, &pData, &reted); if(ret) { PWTS_CLIENT_ADDRESS pWTSCA = (PWTS_CLIENT_ADDRESS)pData; char address[100]= {0}; sprintf(address, "address:%d.%d.%d.%d",pWTSCA->Address[2],pWTSCA->Address[3],pWTSCA->Address[4],pWTSCA->Address[5]); printf("%s\r\n", address); printf("%d\r\n", pWTSCA->AddressFamily); printf("\r\n"); } } printf("\r\n"); free(info); }
其他的我都实现了,就是有一句WTSQuerySessionInformation函数我无法再C#里面实现,其中传入的&pData我不知道在C#里面怎么获取,pData在之后转换成PWTS_CLIENT_ADDRESS类型,之后获取里面的一个地址信息。在C#里面我就不知道怎么做了。
一下是我C#代码,其中都实现了,就差获取ClientAddress这个功能了。
public static List<LogonUser> GetLogonUserList() { List<LogonUser> LogonUsers = null; #region 查询代码 WTS_SESSION_INFO[] pSessionInfo = TSControl.SessionEnumeration(); LogonUser cum = null; LogonUsers = new System.Collections.Generic.List<LogonUser>(); for (int i = 0; i < pSessionInfo.Length; i++) { if ("RDP-Tcp" != pSessionInfo[i].pWinStationName) { try { int count = 0; IntPtr buffer = IntPtr.Zero; StringBuilder userName = new StringBuilder(); StringBuilder clientUser = new StringBuilder(); StringBuilder stateType = new StringBuilder(); byte[] protocalType = new byte[2]; byte[] connState = new byte[1]; StringBuilder clientAddress = new StringBuilder(); bool userNameBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSUserName, out userName, out count); bool clientUserBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSClientName, out clientUser, out count); bool stateTypeBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSWinStationName, out stateType, out count); bool protocalTypeBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSClientProtocolType, out protocalType, out count); bool connStateBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSConnectState, out connState, out count); bool clientAddressBool = TSControl.WTSQuerySessionInformation(IntPtr.Zero, pSessionInfo[i].SessionID, WTSInfoClass.WTSClientAddress, out clientAddress, out count); if (userNameBool && clientUserBool && stateTypeBool & connStateBool) { cum = new LogonUser(); cum.SessionId = pSessionInfo[i].SessionID; cum.UserName = userName.ToString(); cum.ClientUserName = clientUser.ToString(); cum.SessionType = stateType.ToString(); cum.ProtocalType = (Silmoon.Windows.Systems.LogonUser.ClientProtocalType)((int)protocalType[0]); cum.ConnectState = (WTS_CONNECTSTATE_CLASS)connState[0]; WTS_CLIENT_ADDRESS ad = new WTS_CLIENT_ADDRESS(); //var aa = clientAddress[1]; 在这里不知道如何获取ClientAddress } LogonUsers.Add(cum); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } #endregion return LogonUsers; }
答案
-
有些函数在一种语言中被定义,在另一种语言并不一定有相应的函数。这个时候就需要你去理解该函数的作用,明白函数的作用之后就不难用另一种语言“重现”出来。
抱歉,我对上述函数并不熟悉。但是我找了一个链接或许有点用处:http://stackoverflow.com/questions/8148949/converting-c-to-c-sharp-program
- 已标记为答案 Lisa ZhuModerator 2012年10月26日 7:13