title: system exception handling: try to get the string message with arguments
body: <p>On windows XP, I try to print the official string message when the CPU is raising an exception (interrupt). Here I have a piece of code which try to access </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  
  LONG WINAPI e(LPEXCEPTION_POINTERS ExceptionInfo) {
      printf("Exception Handled ...\n");
      char buf[8192];
      memset(buf, 0, 8192);
  
      void * pArgs[ExceptionInfo-&gt;ExceptionRecord-&gt;NumberParameters];
      for (int i = 0; i &lt; ExceptionInfo-&gt;ExceptionRecord-&gt;NumberParameters; i++) {
          printf("arg[%d] = %d\n", i, ExceptionInfo-&gt;ExceptionRecord-&gt;ExceptionInformation[i+1]);
          pArgs[i] = (void *) ExceptionInfo-&gt;ExceptionRecord-&gt;ExceptionInformation[i+1];
      }
      HMODULE Hand = LoadLibrary("NTDLL.DLL");
      int res = FormatMessage(
          FORMAT_MESSAGE_FROM_SYSTEM |
          FORMAT_MESSAGE_FROM_HMODULE,
          Hand,
          ExceptionInfo-&gt;ExceptionRecord-&gt;ExceptionCode,
          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
          buf,
          8192,
          (va_list *) pArgs);
      printf("res=%d\n", res);
      FreeLibrary(Hand);
  
      printf("ExceptionCode=0x%08x (%s)\n", ExceptionInfo-&gt;ExceptionRecord-&gt;ExceptionCode, buf);
      printf("ExceptionFlags=%d\n", ExceptionInfo-&gt;ExceptionRecord-&gt;ExceptionFlags);
      printf("ExceptionAddress=0x%08x\n", ExceptionInfo-&gt;ExceptionRecord-&gt;ExceptionAddress);
      printf("NumberParameters=%d\n", ExceptionInfo-&gt;ExceptionRecord-&gt;NumberParameters);
      printf("ExceptionInformation=%s\n", ExceptionInfo-&gt;ExceptionRecord-&gt;ExceptionInformation);
  
  
      return EXCEPTION_EXECUTE_HANDLER;
  }
  
  
  
  int main() {
      LPTOP_LEVEL_EXCEPTION_FILTER p = SetUnhandledExceptionFilter(e);
      for (int i = 10; i &lt; 256; i++) {
          int *p = (int *) i;
          printf("address pointed by p = 0x%08x\n", *p);
      }
  }
  </code></pre>
  
  <p>It produces the following output:</p>
  
  <pre><code>Exception Handled ...
  arg[0] = 10
  arg[1] = 65599
  res=22
  ExceptionCode=0xc0000005 (The instruction at "0x)
  ExceptionFlags=0
  ExceptionAddress=0x004018da
  NumberParameters=2
  ExceptionInformation=
  </code></pre>
  
  <p>As you can see the message is truncated.</p>
  
  <p>On the <code>ntdll.dll</code> there is the string message:</p>
  
  <pre><code>jlouis@didi /c/WINDOWS/system32
  $ strings ntdll.dll | grep instruction
  The instruction at %p referenced memory at %p.
  The instruction at %p tried to %s
  </code></pre>
  
  <p>Any idea of what should be the right way to get the full message ? Thanks.</p>