title: Undefined reference to `NtQuerySystemInformation@16'
body: <p>I am trying to use NtQuerySystemInformation to list all the loaded modules. I am using the source code from <a href="http://www.rohitab.com/discuss/topic/40696-list-loaded-drivers-with-ntquerysysteminformation/" rel="nofollow noreferrer">http://www.rohitab.com/discuss/topic/40696-list-loaded-drivers-with-ntquerysysteminformation/</a> and it looks like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;winternl.h&gt;
  
  #pragma comment(lib,"ntdll.lib")
  
  // These structures are copied from Process Hacker source code (ntldr.h)
  
  typedef struct _RTL_PROCESS_MODULE_INFORMATION
  {
      HANDLE Section;
      PVOID MappedBase;
      PVOID ImageBase;
      ULONG ImageSize;
      ULONG Flags;
      USHORT LoadOrderIndex;
      USHORT InitOrderIndex;
      USHORT LoadCount;
      USHORT OffsetToFileName;
      UCHAR FullPathName[256];
  } RTL_PROCESS_MODULE_INFORMATION, *PRTL_PROCESS_MODULE_INFORMATION;
  
  typedef struct _RTL_PROCESS_MODULES
  {
      ULONG NumberOfModules;
      RTL_PROCESS_MODULE_INFORMATION Modules[1];
  } RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES;
  
  int main()
  {
      NTSTATUS status;
      ULONG i;
  
      PRTL_PROCESS_MODULES ModuleInfo;
  
      ModuleInfo=(PRTL_PROCESS_MODULES)VirtualAlloc(NULL,1024*1024,MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE); // Allocate memory for the module list
  
      if(!ModuleInfo)
      {
          printf("\nUnable to allocate memory for module list (%d)\n",GetLastError());
          return -1;
      }
  
      if(!NT_SUCCESS(status=NtQuerySystemInformation((SYSTEM_INFORMATION_CLASS)11,ModuleInfo,1024*1024,NULL))) // 11 = SystemModuleInformation
      {
          printf("\nError: Unable to query module list (%#x)\n",status);
  
          VirtualFree(ModuleInfo,0,MEM_RELEASE);
          return -1;
      }
  
      for(i=0;i&lt;ModuleInfo-&gt;NumberOfModules;i++)
      {
          printf("\n*****************************************************\n");
          printf("\nImage base: %#x\n",ModuleInfo-&gt;Modules[i].ImageBase);
          printf("\nImage name: %s\n",ModuleInfo-&gt;Modules[i].FullPathName+ModuleInfo-&gt;Modules[i].OffsetToFileName);
          printf("\nImage full path: %s\n",ModuleInfo-&gt;Modules[i].FullPathName);
          printf("\nImage size: %d\n",ModuleInfo-&gt;Modules[i].ImageSize);
          printf("\n*****************************************************\n");
      }
  
      VirtualFree(ModuleInfo,0,MEM_RELEASE);
      return 0;
  }
  </code></pre>
  
  <p>However, when I try to compile it with Dev C++, it gives me Undefined reference to `NtQuerySystemInformation@16'. When I ctrl+click the "#include winternl.h, it brings me to the winternl.h file which shows that NtQuerySystemInformation has already been defined as follows:</p>
  
  <pre><code> NTSTATUS NTAPI NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass,PVOID SystemInformation,ULONG SystemInformationLength,PULONG ReturnLength);
  </code></pre>
  
  <p>I tried adding the flags -L and -lm to the linker options in Dev C++ but to no avail. Am i missing any steps?</p>