-2

How can i force my linker to link against the Windows.h statically, so all the functions of the Windows library gets inside my executable ?

I want API calls like VirtualAllocEx, OpenProcess, and other API calls like Writing to another process's memory and such and CreateRemoteThread (basically all the communication API calls and memory allocation ones) to be inside my executable, so i don't have to use Windows.h for it, so basically my IAT would not contain these functions, what is the best way to achieve this?

19
  • Thanks for the link but how can i force it to link windows.h statically? @πάντα ῥεῖ
    – Mery Ted
    Aug 18, 2019 at 14:28
  • 1
    "how can i force it to link windows.h statically?" - That makes no sense. "Windows.h" is a header file, not a library. It may expose things from a library (or many different libraries), but it is not itself a library. Aug 18, 2019 at 14:32
  • 1
    @MeryTed Is there something wrong with your google? I've added another link. Aug 18, 2019 at 14:33
  • 1
    @dedecos Even in that case we could explain that the crt libraries are compiler dependent because contains the basic functions necessary to run compiler library functions (fopen, get, printf, etc.). Some compilers, as MSVC, allow to choose how to link them, statically or dynamically, with a specific line switch, so he must refer to compiler documentation. More difficult is to explain that, due to technical reasons, the use of the 2 make difference. I.e. choosing static link don't allow to share file pointers between DLL's in running in the same process space.
    – Frankie_C
    Aug 18, 2019 at 14:52
  • 1
    @πάνταῥεῖ Again you can't statically link system libraries because each DLL use shared sections to keep track of interprocess arbitration and allow the kernel to interact with user code without interference between processes. That just in plain words, real things are much more complicate.
    – Frankie_C
    Aug 18, 2019 at 14:57

3 Answers 3

1

You can't.

These things are part of the operating system.

The operating system has permission to do things your own code does not.

In addition, the implementation with your operating system is correct for that version of the operating system.

This is a good thing. You do not want to statically link the OS's API implementation.

The header file Windows.h only provides declarations that allow you to call them.

1

A I said in comments under MS Windows OS the dynamic library have some characteristics that don't pertain to static library code. Let try to explain in very plain way:

First of all a DLL is loaded independently from current executable, and directly from the OS module loading function, as a separate object which code memory is mapped in the current process memory, but retained to be mapped in a different process.

During the loading the loader creates different memory areas that can be unique for a single process or shared between processes or private to DLL internal functions.

The shared areas permits to create mutex's,semaphores and any data necessary to the kernel to arbitrate the multitasking environment and resources sharing.

On the reverse a static library is loaded only in the current process, the code segment lays together with the user program code segment, and the data segment, in same way, is added to the current executable data space.

For these reasons, and much more, you can't statically link any system library function to your executable.


A simple experiment on static and dynamic linking using the CRT libraries.

Create a main program in where you open a file:

#include <stdio.h>
extern void DllReadRoutine(FILE *);
int main(int argc, char *argv[])
{
    FILE *fp = fopen("Myfile.txt", "r");
    //Diagnostic omitted to keep it simple
    DllReadRoutine(fp);    //Pass the file pointer to the external DLL function
    fclose(fp);
    return 0;
}

Now create DLL (we omit DLL entry):

#include <stdio.h>
void DllReadRoutine(FILE *fp);
int main(int argc, char *argv[])
{
    int c;
    while ((c=fgetc(pFile)) != EOF)
    {
        putchar(c);
    }
}

Then compile the two linking the CRT statically the first time (use /MT on MSVC) and dynamically the second (use /MD on MSVC).

In the first case the DLL function will fail because the static linking to CRT creates local open files table that is incompatible with the table local to the main executable. The result is a crash.

In the second case the internal CRT data, as the open files table, is created in a shared area where both, executable and DLL, access. In this case the code will work smoothly.

2
  • But when i used the runtime library option of /MT instead of /MD, it did kinda work because the size of my binary went from 9kb to 77, and the text section got a lot bigger so i assume these are the API calls from windows that were added to my .text section correct? but i don't know why the IAT table still contains the function that i used (MessageBox from user32.dll) and it contains this only, thought it would get removed from IAT since i linked it statically
    – Mery Ted
    Aug 18, 2019 at 15:21
  • @MeryTed No. Using /MT or /MD you instruct the linker to respectively link statically or dynamically only the the compiler CRT (Compiler Run Time libraries). Not the system libraries. Check the sample code I provided above.
    – Frankie_C
    Aug 18, 2019 at 15:38
0

I think you may be misunderstanding the process of static linking. The Windows.h header file contains declarations for various types of activities, for example function calls. Note these are declarations not binary implementation of the functions themselves. Take a look at the ShellExecuteA documentation. Scroll to the end of the document and you will see a Requirements section that looks like this:

Requirements

Minimum supported client Windows XP [desktop apps only] 
Minimum supported server Windows 2000 Server [desktop apps only] 
Target Platform Windows Header shellapi.h 
Library Shell32.lib 
DLL Shell32.dll (version 3.51 or later)

Windows.h includes shellapi.h (Target Platform Windows Header). Shellapi.h contains the declaration for ShellExecuteA (function we looked up in the docs). This section also tells you the library containing a binary implementation of the function, Shell32.lib in this case. If you know the function declaration and the location of it's binary implementation you can link it. The linker just matches the (function) name to the (implementation) name available at link time. On Windows you should be able to statically link using the lib file, or dynamically link using the dll file. If you statically link you include the binary implementation from the lib file into your executable. If MS fixes a but in the function you use, you don't get that fix unless you recompile. If you dynamically link (link to a dll) your executable will be smaller and more inclusive of future MS changes.

Not the answer you're looking for? Browse other questions tagged or ask your own question.