0

I am passing an IntPtr to a function SendMessageTimeout as below. SendMessageTimeout belongs to user32.dll.

I dont know how this DLL allcoates memory to the IntPtr passed as reference, i.e result.

How to release memory of IntPtr passed to this function?

[DllImport("user32")]
public static extern int SendMessageTimeout(
    int hwnd, int msg, int wparam, int lparam, 
    int fuFlags, int uTimeout, IntPtr lpdwResult);       

if (handle > 0 && GetWindowText(handle, Buff, nChars) > 0)
{
    this.log("GetForegroundWindow : " +Buff.ToString());
    foreach (string str in this.titleList)
    {
        if (Buff.ToString().ToLower().Contains(str.ToLower()))
        {
            IntPtr result = IntPtr.Zero;
            if (SendMessageTimeout(handle, 0x0010, 0, 0, 0x0002, 2000, result) != 0) 
                DestroyWindow(handle);

            killed = true;
            break;
        }
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Gajukorse
  • 147
  • 2
  • 11
  • 1
    Since the documentation states that the parameter is optional, why not simply omit it? (i.e. pass `IntPtr.Zero`). – Uwe Keim Mar 21 '17 at 05:25
  • Thanks.Yeah i think passing IntPtr.Zero makes sense for me. Btw if I have to use this param ,is there any proper way to release the pointer. I coundn't find any proper documentation for user32.dll functions – Gajukorse Mar 21 '17 at 05:53
  • Me, neither. After googling, I have no real clue about whether I as the caller am responsible for freeing the memory, or if I can rely on "the system" taking care of freeing this memory. – Uwe Keim Mar 21 '17 at 05:57
  • The pointer in question is used to store the result of the message processing. Whether that's a pointer to memory that needs to be freed, and if so, how to free it, *depends upon the message*. You need to look up the documentation for the specific message you're sending. – Harry Johnston Mar 21 '17 at 23:22

1 Answers1

0

Use Marshal.FreeHGlobal once you finish with it.

You can use FreeHGlobal to free any memory from the global heap allocated by AllocHGlobal, ReAllocHGlobal, or any equivalent unmanaged API method.

 Marshal.FreeHGlobal(lpdwResult);
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • Are you sure about that? I do find no evidence in [the documentation of `SendMessageTimeout`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644952.aspx) about how/whether to free the memory at all. – Uwe Keim Mar 21 '17 at 05:27
  • Probably this answers your doubt ? http://stackoverflow.com/questions/4949084/how-marshal-freehglobal-works – CharithJ Mar 21 '17 at 05:29
  • Sorry, no. Looking at [this SO answer](http://stackoverflow.com/a/19737234/107625), he also does _not_ free the memory of the returned `IntPtr`. In my opinion, you cannot blindly call `FreeHGlobal` on an `IntPtr` without knowing the documentation of the called function. – Uwe Keim Mar 21 '17 at 05:30
  • 1
    @UweKeim: I think you are correct. Probably passing IntPtr.Zero makes sense if he doesn't need to know the out value. – CharithJ Mar 21 '17 at 05:37