using the System::Collections::IEnumerator()
-
Hello, I am trying to get current login user name using my own windows service program using VC++.Net 2005, windows XP. The code used to develop windows service contains only unmanaged one. But in order to retrieve the current login name.. i am using the help of System::Management class, for which i included necessary namespaces in its .cpp file. actually i am trying interpret the following statement of c# into c++: System::Management::Collections mc; foreach(System::Management::Object mo in mc){..do this..} my approach was somewhat like this in the following way: CString CUSBActivity::Get_User_Name() { CString Username=_T(""); System::Management::ManagementObjectSearcher^ q = nullptr; System::Management::ManagementObjectCollection^ mc = nullptr; System::Management::ManagementObject^ mo = nullptr; System::Collections::IEnumerator^ enu = nullptr; q = gcnew System::Management::ManagementObjectSearcher("Select * from Win32_Process"); mc = q->Get(); enu = mc->GetEnumerator(); mo = (System::Management::ManagementObject^)enu->Current; while(enu->MoveNext()) { DebugBreak(); mo = (System::Management::ManagementObject ^)enu->GetType(); Username = mo["username"]->ToString(); mc->GetEnumerator()->MoveNext(); } return Username; } But when i run this, i am getting the run time exception and the Username is null. Could anybody... assist me in fixing this thank u. Anee
-
Hello, I am trying to get current login user name using my own windows service program using VC++.Net 2005, windows XP. The code used to develop windows service contains only unmanaged one. But in order to retrieve the current login name.. i am using the help of System::Management class, for which i included necessary namespaces in its .cpp file. actually i am trying interpret the following statement of c# into c++: System::Management::Collections mc; foreach(System::Management::Object mo in mc){..do this..} my approach was somewhat like this in the following way: CString CUSBActivity::Get_User_Name() { CString Username=_T(""); System::Management::ManagementObjectSearcher^ q = nullptr; System::Management::ManagementObjectCollection^ mc = nullptr; System::Management::ManagementObject^ mo = nullptr; System::Collections::IEnumerator^ enu = nullptr; q = gcnew System::Management::ManagementObjectSearcher("Select * from Win32_Process"); mc = q->Get(); enu = mc->GetEnumerator(); mo = (System::Management::ManagementObject^)enu->Current; while(enu->MoveNext()) { DebugBreak(); mo = (System::Management::ManagementObject ^)enu->GetType(); Username = mo["username"]->ToString(); mc->GetEnumerator()->MoveNext(); } return Username; } But when i run this, i am getting the run time exception and the Username is null. Could anybody... assist me in fixing this thank u. Anee
In your while loop you do this: mo = (System::Management::ManagementObject ^)enu->GetType(); That doesn't look like a valid cast to me. Did you mean mo = (System::Management::ManagementObject ^)enu->Current; ?? What line is the exception occurring on? Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder