title: c# access to information inside a static method without a return
body: <p>I borrowed the following code I found <a href="https://social.msdn.microsoft.com/Forums/en-US/bfc75b57-df16-48c6-92af-ea0a34f540ae/how-to-get-the-handle-of-a-window-that-i-click?forum=csharplanguage" rel="nofollow noreferrer">online</a> (the comments aren't even mine).  When I click a button on my form, it waits and detects my next mouse click.  Whatever I click on, it gets that information in the winHandle object inside the HookCallback method:</p>
  
  <pre><code>    private static LowLevelMouseProc _proc = HookCallback;
      private static IntPtr _hookID = IntPtr.Zero;
      static IntPtr hHook = IntPtr.Zero;
  
      private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
  
      private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
      {
          if (nCode &gt;= 0 &amp;&amp; MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
          {
              //  The application runs to here when you click on the window whose handle you  want to get                
              POINT cusorPoint;
              bool ret = GetCursorPos(out cusorPoint);
              // cusorPoint contains your cusor’s position when you click on the window
  
  
              // Then use cusorPoint to get the handle of the window you clicked
  
              IntPtr winHandle = WindowFromPoint(cusorPoint);
  
              // winHandle is the Hanle you need
  
  
              // Now you have get the handle, do what you want here
              // …………………………………………………. 
              Debug.WriteLine("Handle: " + winHandle.ToString());
  
  
              // Because the hook may occupy much memory, so remember to uninstall the hook after
              // you finish your work, and that is what the following code does.
              UnhookWindowsHookEx(hHook);
              hHook = IntPtr.Zero;
  
  
              // Here I do not use the GetActiveWindow(). Let's call the window you clicked "DesWindow" and explain my reason.
              // I think the hook intercepts the mouse click message before the mouse click message delivered to the DesWindow's 
              // message queue. The application came to this function before the DesWindow became the active window, so the handle 
              // abtained from calling GetActiveWindow() here is not the DesWindow's handle, I did some tests, and What I got is always 
              // the Form's handle, but not the DesWindow's handle. You can do some test too.
  
              //IntPtr handle = GetActiveWindow();
  
  
          }
          return CallNextHookEx(_hookID, nCode, wParam, lParam);
      }
  
      private const int WH_MOUSE_LL = 14;
  
      private enum MouseMessages
      {
          WM_LBUTTONDOWN = 0x0201,
          WM_LBUTTONUP = 0x0202,
          WM_MOUSEMOVE = 0x0200,
          WM_MOUSEWHEEL = 0x020A,
          WM_RBUTTONDOWN = 0x0204,
          WM_RBUTTONUP = 0x0205
      }
  
      [StructLayout(LayoutKind.Sequential)]
      private struct POINT
      {
          public int x;
          public int y;
      }
  
      [StructLayout(LayoutKind.Sequential)]
      private struct MSLLHOOKSTRUCT
      {
          public POINT pt;
          public uint mouseData;
          public uint flags;
          public uint time;
          public IntPtr dwExtraInfo;
      }
  
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      private static extern IntPtr SetWindowsHookEx(int idHook,
          LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
  
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      [return: MarshalAs(UnmanagedType.Bool)]
      private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
          IntPtr wParam, IntPtr lParam);
  
      [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      private static extern IntPtr GetModuleHandle(string lpModuleName);
  
      [DllImport("user32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      static extern bool GetCursorPos(out POINT lpPoint);
  
      [DllImport("user32.dll")]
      static extern IntPtr WindowFromPoint(POINT Point);
  
      private void GetProcess_Click(object sender, EventArgs e)
      {
          if (IntPtr.Zero == hHook){
              using (Process curProcess = Process.GetCurrentProcess())
              using (ProcessModule curModule = curProcess.MainModule)
              {
                  hHook = SetWindowsHookEx(WH_MOUSE_LL, _proc,
                      GetModuleHandle(curModule.ModuleName), 0);
              }
          }
      }
  </code></pre>
  
  <p>The problem is I need to use the information from that object elsewhere in my code.  But, since it doesn't return that information, I'm not sure how I could get access to it. </p>
  
  <p>I don't think I can return it because it goes instead over to the CallNextHookEx method; I've tried putting it in its own class, but inside the HookCallBack method I don't have access to <strong>this.</strong> or datasets or anything else.  </p>
  
  <p>So what can I do to get that information from outside the static method HookCallback?</p>