title: Debug an imported dll with VS 2010
body: <p>I have a problem while I trying to call WinAPI functions from C# code. I have lots of imports, many of them works fine, but some of them not and leading to unexpected break main program, without any message, exception type, nothing, just fell down all its windows and exit.</p>
  
  <p>I have two ways in code: via my developed library, where is more of winapi calls and I'm lazy to code specific structures, pointers, etc, and direct import from user32.dll, like this:</p>
  
  <pre><code>[DllImport(@"tradeInterop.dll")]
      public static extern void ChooseInstrumentByMouse(UInt32 hwnd, int baseX, int baseY, int idx, int _isDown);
      [DllImport(@"tradeInterop.dll")]
      public static extern void PutNumber(int num);
      [DllImport(@"tradeInterop.dll")]
      public static extern void PutRefresh();
      [DllImport(@"user32.dll")]
      public static extern UInt32 FindWindow(string sClass, string sWindow);
      [DllImport(@"user32.dll")]
      public static extern int GetWindowRect(uint hwnd, out RECT lpRect);
      [DllImport(@"user32.dll")]
      public static extern int SetWindowPos(uint hwnd, uint nouse, int x, int y, int cx, int cy, uint flags);
      [DllImport(@"user32.dll")]
      public static extern int LockSetForegroundWindow(uint uLockCode);
      [DllImport(@"user32.dll")]
      public static extern int SetForegroundWindow(uint hwnd);
      [DllImport(@"user32.dll")]
      public static extern int ShowWindow(uint hwnd, int cmdShow);
      [DllImport(@"tradeInterop.dll")]
      public static extern ulong PixelColor(uint hwnd, int winX, int winY); //tried (signed) long and both ints as return type, same result (WINAPI says DWORD as unsigned long, what about 64-bit assembly where compiled both lib and program?
      public struct RECT
      {
          public int Left;        
          public int Top; ...
  </code></pre>
  
  <p>As I said, many of this calls works perfectly, but have problem of last two of them: ShowWindow() and PixelColor() with following code:</p>
  
  <pre><code>extern "C" __declspec(dllexport) COLORREF __stdcall PixelColor(unsigned hwnd, int winX, int winY)
  {
      LPPOINT point;
      point-&gt;x = winX;
      point-&gt;y = winY;
      ClientToScreen((HWND) hwnd, point);
      HDC dc = GetDC(NULL);
      COLORREF colorPx = GetPixel(dc, point-&gt;x, point-&gt;y);
      ReleaseDC(NULL, dc);
      return colorPx;
  }
  </code></pre>
  
  <p><strong>So, while I try to call directly imported ShowWindow() function, or library which calls api function(s), I got a program crash</strong></p>
  
  <p>Is there any way how to debug external libraries and its results?</p>
  
  <p>What I¨m doing wrong?</p>
  
  <p>Thanks a lot</p>