How to unload or release memory when using win32 dll RRS feed

  • Question

  • Hi All,
    I used to track the memory of my application using dot memory tool.
    In this tool, it is  observed that  managed memory is releasing properly but unmanaged memory is keep on increasing.
    so i want to release the unmanged memory in my application.
    I am using the below unmanged win32 dll in our winform application.
    Can someone please suggest a way  how to release memory for these.

          [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool PostMessage(int hhwnd, uint msg, IntPtr wparam, IntPtr lparam);

            [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            static extern uint RegisterWindowMessage(string lpString);

            // This function checks wheather internet connection is avilable or not
            private extern static bool InternetGetConnectedState(out int netConnection, int val);

            // Sets an event hook function for a range of events.
            static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

            // This Function is used to get Active Window Title.
            [DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
            public static extern int GetWindowText(IntPtr hwnd, string lpString, int cch);

            // This Function is used to get Handle for Active Window.
            [DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
            private static extern IntPtr GetForegroundWindow();

            // This Function is used to get Active window process ID.
            [DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
            private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out Int32 lpdwProcessId);

            [DllImport("user32.dll", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern IntPtr SendMessage(
                IntPtr hWnd,
                IntPtr Msg,
                IntPtr wParam,
                [MarshalAs(UnmanagedType.LPWStr)] StringBuilder lParam

            public static extern IntPtr FindWindowEx(
                IntPtr hWndParent,
                IntPtr hWndChild,
                string WndClass,
                string WndTitle

            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool UnhookWinEvent(IntPtr hWinEventHook);

            // This Function is used to retrieves the name of the class to which the specified window belongs.
            private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

            // This Function is used to receive the child window handles.
            private static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildCallback lpEnumFunc, ref IntPtr lParam);

            // This Function is used to retrieves the address of the specified interface for the object associated with the specified window.
            private static extern int AccessibleObjectFromWindow(IntPtr hwnd, uint dwObjectID, byte[] riid, ref Excel.Window ptr);


    Padma Yeddula

    Tuesday, September 2, 2014 10:47 AM


  • Probably you can call Marshal.FinalReleaseComObject for objects returned by AccessibleObjectFromWindow, when they are not needed anymore.

    Tuesday, September 2, 2014 11:01 AM