module main;

import std.c.windows.windows;
import std.c.stdio;
import std.exception;
import printtlsinfo;

version(NoDef)
	enum postfix = "NoDef";
else
	enum postfix = "Def";

version(dynamicDll)
	enum name = "dllUser-Dynamic-" ~ postfix;
else
	enum name = "dllUser-Static-" ~ postfix;

enum val = 900;
int tlsVar = val;

version(dynamicDll)
	extern(C) alias int function() GetType;
else {
	extern(C) int get7_1();
	extern(C) int get7_2();
	extern(C) int get7_3();
}

void main()
{
	puts(name ~ ".D-main:");
	printTlsInfo();

	version(dynamicDll) {
		auto h1 = enforce(LoadLibraryA("DDynamicLib1" ~ postfix));
		auto h2 = enforce(LoadLibraryA("DDynamicLib2" ~ postfix));
		auto h3 = enforce(LoadLibraryA("DDynamicLib3" ~ postfix));

		version(NoDef)
			enum get7_ = "_get7_";
		else
			enum get7_ =  "get7_";

		enforce((cast(GetType) GetProcAddress(h1, get7_ ~ "1"))() == 107);
		enforce((cast(GetType) GetProcAddress(h2, get7_ ~ "2"))() == 207);
		enforce((cast(GetType) GetProcAddress(h3, get7_ ~ "3"))() == 307);
	} else {
		enforce(GetModuleHandleA("DDynamicLib1" ~ postfix));
		enforce(GetModuleHandleA("DDynamicLib2" ~ postfix));
		enforce(GetModuleHandleA("DDynamicLib3" ~ postfix));

		enforce(get7_1() == 107);
		enforce(get7_2() == 207);
		enforce(get7_3() == 307);
	}

	version(dynamicDll) {
		puts("Calling FreeLibrary...");
		FreeLibrary(h3);
		FreeLibrary(h2);
		FreeLibrary(h1);
		puts("Libraries unloaded.");
	}
	puts(name ~ ".D-main done");
	puts("----------------------------------------\n\n");
}
