/*******************************************************************************
//
//	ftprn_api.c
//
//	Printer for Remote Desktop
//
//	Implementaion of loading and unloading ftprintapi.dll for dynamic linking
//
//	Copyright (c) FabulaTech
//
*******************************************************************************/

#include "ftprn_api.h"
#include "ftprn_api_config.h"

static HMODULE g_hPrinterApi = NULL;

FTPRNVIRTAPIINIT FtPrnVirtApiInit;
FTPRNVIRTAPICLEANUP FtPrnVirtApiCleanUp;
FTPRNSELECTPRINTER FtPrnSelectPrinter;
FTPRNGETDEFAULTPRINTER FtPrnGetDefaultPrinter;
FTPRNAPPLYKEYA FtPrnApplyKeyA;
FTPRNAPPLYKEYW FtPrnApplyKeyW;
FTPRNGETKEYDATA FtPrnGetKeyData;
FTPRNGETKEYDATAINTA FtPrnGetKeyDataIntA;
FTPRNGETKEYDATAINTW FtPrnGetKeyDataIntW;
FTPRNGETKEYDATASTRA FtPrnGetKeyDataStrA;
FTPRNGETKEYDATASTRW FtPrnGetKeyDataStrW;
FTPRNUSELICENSESERVER FtPrnUseLicenseServer;
FTPRNGETUSINGLICENSESERVER FtPrnGetUsingLicenseServer;
FTPRNSETLASTUSEDPRINTER FtPrnSetLastUsedPrinter;


FT_EXTERN BOOL __cdecl FtPrnVirtApiLoad()
{
	DWORD dwErr;

	if (NULL != g_hPrinterApi)
		return TRUE;

	//Load library.
	g_hPrinterApi = LoadLibraryW(cPrintApiDllName);
	if (NULL == g_hPrinterApi)
	{
		return FALSE;
	}

	do
	{
		//Load api functions.
		FtPrnVirtApiInit = (FTPRNVIRTAPIINIT)GetProcAddress(
			g_hPrinterApi, "FtPrnVirtApiInit");

		if (NULL == FtPrnVirtApiInit)
			break;

		FtPrnVirtApiCleanUp = (FTPRNVIRTAPICLEANUP)GetProcAddress(
			g_hPrinterApi, "FtPrnVirtApiCleanUp");

		if (NULL == FtPrnVirtApiCleanUp)
			break;

		FtPrnSelectPrinter = (FTPRNSELECTPRINTER)GetProcAddress(
			g_hPrinterApi, "FtPrnSelectPrinter");

		if (NULL == FtPrnSelectPrinter)
			break;

		FtPrnGetDefaultPrinter = (FTPRNGETDEFAULTPRINTER)GetProcAddress(
			g_hPrinterApi, "FtPrnGetDefaultPrinter");

		if (NULL == FtPrnGetDefaultPrinter)
			break;

		FtPrnApplyKeyA = (FTPRNAPPLYKEYA)GetProcAddress(
			g_hPrinterApi, "FtPrnApplyKeyA");

		if (NULL == FtPrnApplyKeyA)
			break;

		FtPrnApplyKeyW = (FTPRNAPPLYKEYW)GetProcAddress(
			g_hPrinterApi, "FtPrnApplyKeyW");

		if (NULL == FtPrnApplyKeyW)
			break;

		FtPrnGetKeyData = (FTPRNGETKEYDATA)GetProcAddress(
			g_hPrinterApi, "FtPrnGetKeyData");

		if (NULL == FtPrnGetKeyData)
			break;

		FtPrnGetKeyDataIntA = (FTPRNGETKEYDATAINTA)GetProcAddress(
			g_hPrinterApi, "FtPrnGetKeyDataIntA");

		if (NULL == FtPrnGetKeyDataIntA)
			break;

		FtPrnGetKeyDataIntW = (FTPRNGETKEYDATAINTW)GetProcAddress(
			g_hPrinterApi, "FtPrnGetKeyDataIntW");

		if (NULL == FtPrnGetKeyDataIntW)
			break;

		FtPrnGetKeyDataStrA = (FTPRNGETKEYDATASTRA)GetProcAddress(
			g_hPrinterApi, "FtPrnGetKeyDataStrA");

		if (NULL == FtPrnGetKeyDataStrA)
			break;

		FtPrnGetKeyDataStrW = (FTPRNGETKEYDATASTRW)GetProcAddress(
			g_hPrinterApi, "FtPrnGetKeyDataStrW");

		if (NULL == FtPrnGetKeyDataStrW)
			break;

		FtPrnUseLicenseServer = (FTPRNUSELICENSESERVER)GetProcAddress(
			g_hPrinterApi, "FtPrnUseLicenseServer");

		if (NULL == FtPrnUseLicenseServer)
			break;

		FtPrnGetUsingLicenseServer = (FTPRNGETUSINGLICENSESERVER)GetProcAddress(
			g_hPrinterApi, "FtPrnGetUsingLicenseServer");

		if (NULL == FtPrnGetUsingLicenseServer)
			break;

		FtPrnSetLastUsedPrinter = (FTPRNSETLASTUSEDPRINTER)GetProcAddress(
			g_hPrinterApi, "FtPrnSetLastUsedPrinter");

		if (NULL == FtPrnSetLastUsedPrinter)
			break;

		return TRUE;

	} while (FALSE);

	//
	dwErr = GetLastError();

	SetLastError(dwErr);

	return FALSE;
}

FT_EXTERN void __cdecl FtPrnVirtApiFree()
{
	if (FtPrnVirtApiCleanUp)
		FtPrnVirtApiCleanUp();

	FtPrnVirtApiInit = NULL;
	FtPrnVirtApiCleanUp = NULL;
	FtPrnSelectPrinter = NULL;
	FtPrnGetDefaultPrinter = NULL;
	FtPrnApplyKeyA = NULL;
	FtPrnApplyKeyW = NULL;
	FtPrnGetKeyData = NULL;
	FtPrnGetKeyDataIntA = NULL;
	FtPrnGetKeyDataIntW = NULL;
	FtPrnGetKeyDataStrA = NULL;
	FtPrnGetKeyDataStrW = NULL;
	FtPrnUseLicenseServer = NULL;
	FtPrnGetUsingLicenseServer = NULL;
	FtPrnSetLastUsedPrinter = NULL;

	if (NULL != g_hPrinterApi)
		FreeLibrary(g_hPrinterApi);

	g_hPrinterApi = NULL;
}

