Click here to Skip to main content
16,007,885 members
Home / Discussions / C#
   

C#

 
AnswerRe: Array get/set problem Pin
Ravadre21-Mar-09 6:16
Ravadre21-Mar-09 6:16 
GeneralRe: Array get/set problem Pin
Jastons21-Mar-09 6:24
Jastons21-Mar-09 6:24 
GeneralRe: Array get/set problem Pin
Ravadre21-Mar-09 6:27
Ravadre21-Mar-09 6:27 
AnswerRe: Array get/set problem Pin
harold aptroot21-Mar-09 6:34
harold aptroot21-Mar-09 6:34 
GeneralRe: Array get/set problem Pin
Jastons21-Mar-09 6:40
Jastons21-Mar-09 6:40 
QuestionFinding out file(s) being used by a process Pin
andrei_ciobanu21-Mar-09 5:21
andrei_ciobanu21-Mar-09 5:21 
AnswerRe: Finding out file(s) being used by a process Pin
Giorgi Dalakishvili21-Mar-09 6:28
mentorGiorgi Dalakishvili21-Mar-09 6:28 
GeneralRe: Finding out file(s) being used by a process Pin
andrei_ciobanu23-Mar-09 8:31
andrei_ciobanu23-Mar-09 8:31 
Thanks for the reply, Giorgi.
I've managed to find a way to obtain file handles for a process. Here is the code:

[DllImport("ntdll.dll", SetLastError = true)]
        public static extern uint ZwQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, IntPtr SystemInformation, int SystemInformationLength, out int ReturnLength);


        [StructLayout(LayoutKind.Sequential)]
        public struct SYSTEM_HANDLE_INFORMATION
        {
            public int ProcessId; //id of the process whom handle this is
            public byte ObjectTypeNumber; //type of the object
            public byte Flags; // 1 = PROTECT_FROM_CLOSE, 2 = INHERIT
            public short Handle;
            public int Object; //address of the handle
            public int GrantedAccess; //access for the handle
        }

        public const uint STATUS_INFO_LENGTH_MISMATCH = 0xc0000004;

        public static SYSTEM_HANDLE_INFORMATION[] EnumHandles()
        {
            int retLength = 0;
            int handleCount = 0;
            SYSTEM_HANDLE_INFORMATION[] returnHandles;
            int allocLength = 0x1000;
            IntPtr data = Marshal.AllocHGlobal(allocLength);

            // This is needed because ZwQuerySystemInformation with SystemHandleInformation doesn't
            // actually give a real return length when called with an insufficient buffer. This code
            // tries repeatedly to call the function, doubling the buffer size each time it fails.
            while (ZwQuerySystemInformation(SYSTEM_INFORMATION_CLASS.SystemHandleInformation, data,
                allocLength, out retLength) == STATUS_INFO_LENGTH_MISMATCH)
            {
                data = Marshal.ReAllocHGlobal(data, new IntPtr(allocLength *= 2));
            }

            // The structure of the buffer is the handle count plus an array of SYSTEM_HANDLE_INFORMATION
            // structures.
            handleCount = Marshal.ReadInt32(data);
            returnHandles = new SYSTEM_HANDLE_INFORMATION[handleCount];

            for (int i = 0; i < handleCount; i++)
            {
                returnHandles[i] = (SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(
                    new IntPtr(data.ToInt32() + 4 + i * Marshal.SizeOf(typeof(SYSTEM_HANDLE_INFORMATION))),
                    typeof(SYSTEM_HANDLE_INFORMATION));
            }

            Marshal.FreeHGlobal(data);

            return returnHandles;
        }


Then I'd like to obtain the handles for a specific process:

SYSTEM_HANDLE_INFORMATION[] handles = EnumHandles();
            var winampHandles = from handle in handles
                                where handle.ProcessId == 10004
                                select handle;
            foreach (var winampHandle in winampHandles)
            {
                //do something here...
            }


As we can see, each handle is an object of type SYSTEM_HANDLE_INFORMATION, which does not contain a field specifying the name of the handle. To obtain the name of the handle (e.g. the file path), I think I need to call some Win API.
MSDN has an example of how to to this: http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx[^], but it is written in C, and I need it for C#.

Is there some other way around this thing? I've tryed using the FileStream class, and pass to it's ctor the handle:
FileStream fs = new FileStream(new IntPtr(winampHandle.Handle), FileAccess.ReadWrite, false);, which throws an IO Exception ("Invalid handle"). Passing a SafeFileHandle to the ctor didn't solve the problem.

Any ideas?
QuestionFilling gridview with multiple combobox Pin
haroon198021-Mar-09 5:13
haroon198021-Mar-09 5:13 
QuestionLittle help with string and converting it to uppercase or lowercase [modified] Pin
maxflair21-Mar-09 3:32
maxflair21-Mar-09 3:32 
AnswerRe: Little help with string and converting it to uppercase or lowercase Pin
harold aptroot21-Mar-09 3:44
harold aptroot21-Mar-09 3:44 
GeneralRe: Little help with string and converting it to uppercase or lowercase Pin
maxflair21-Mar-09 4:08
maxflair21-Mar-09 4:08 
GeneralRe: Little help with string and converting it to uppercase or lowercase Pin
fly90421-Mar-09 4:20
fly90421-Mar-09 4:20 
GeneralRe: Little help with string and converting it to uppercase or lowercase Pin
maxflair21-Mar-09 4:32
maxflair21-Mar-09 4:32 
GeneralRe: Little help with string and converting it to uppercase or lowercase Pin
fly90421-Mar-09 4:38
fly90421-Mar-09 4:38 
GeneralRe: Little help with string and converting it to uppercase or lowercase Pin
maxflair21-Mar-09 5:46
maxflair21-Mar-09 5:46 
GeneralRe: Little help with string and converting it to uppercase or lowercase Pin
Yusuf21-Mar-09 4:34
Yusuf21-Mar-09 4:34 
GeneralRe: Little help with string and converting it to uppercase or lowercase Pin
harold aptroot21-Mar-09 4:23
harold aptroot21-Mar-09 4:23 
AnswerRe: Little help with string and converting it to uppercase or lowercase Pin
Yusuf21-Mar-09 4:33
Yusuf21-Mar-09 4:33 
GeneralRe: Little help with string and converting it to uppercase or lowercase Pin
maxflair21-Mar-09 5:47
maxflair21-Mar-09 5:47 
AnswerRe: Little help with string and converting it to uppercase or lowercase Pin
maxflair21-Mar-09 6:40
maxflair21-Mar-09 6:40 
QuestionBorland C DLL in C#.NET Pin
dataminers21-Mar-09 3:28
dataminers21-Mar-09 3:28 
AnswerRe: Borland C DLL in C#.NET Pin
harold aptroot21-Mar-09 3:43
harold aptroot21-Mar-09 3:43 
GeneralRe: Borland C DLL in C#.NET Pin
dataminers21-Mar-09 3:48
dataminers21-Mar-09 3:48 
AnswerRe: Borland C DLL in C#.NET Pin
Ravi Mori21-Mar-09 3:46
Ravi Mori21-Mar-09 3:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.