#include <windows.h>
BOOL WINAPI GhostPsToPdf(char *PsFile, char *PdfFile,
char *Resolution, char *PaperSize, char *GsDll )
{
HMODULE hModule = NULL;
char *DllPath;
if (!GsDll)
{
HKEY hReg;
char Path[MAX_PATH] ;
DWORD Size = MAX_PATH;
char RegPath[MAX_PATH] = "SOFTWARE\\AFPL Ghostscript";
if (RegOpenKey(HKEY_LOCAL_MACHINE,RegPath,&hReg) != ERROR_SUCCESS)
{
strcpy(RegPath,"SOFTWARE\\GPL Ghostscript");
if (RegOpenKey(HKEY_LOCAL_MACHINE,RegPath,&hReg) != ERROR_SUCCESS)
return FALSE;
}
// *** Grab the first sub-key
char Version[40]; // just to be safe
DWORD VerSize=9;
if (RegEnumKeyEx(hReg,0,Version,&VerSize,NULL,NULL,NULL,NULL) != ERROR_SUCCESS)
{
RegCloseKey(hReg);
return FALSE;
}
RegCloseKey(hReg);
// *** Add to the main path and open the subkey
strcat(RegPath,"\\");
strcat(RegPath,Version);
if (RegOpenKey(HKEY_LOCAL_MACHINE,RegPath,&hReg) != ERROR_SUCCESS)
return FALSE;
DWORD RegType = REG_SZ;
if (RegQueryValueEx(hReg,"GS_DLL",0,&RegType,(LPBYTE) Path,&Size) != ERROR_SUCCESS)
{
RegCloseKey(hReg);
return FALSE;
}
DllPath = Path;
RegCloseKey(hReg);
}
else
DllPath = GsDll;
hModule = LoadLibrary(DllPath);
if (!hModule)
return FALSE;
if (!Resolution)
{
char cResolution[8] = "150x150";
Resolution = cResolution;
}
char ArgPaperSize[50] = "-sPAPERSIZE=letter";
if (PaperSize)
{
strcpy(ArgPaperSize,"-sPAPERSIZE=");
strcat(ArgPaperSize,PaperSize);
}
// *** Declare all the dynamic typedef function pointers
typedef int (WINAPI *gsapi_new_instance)( char **, int );
gsapi_new_instance lp_gsapi_new_instance ;
lp_gsapi_new_instance = (gsapi_new_instance) GetProcAddress(hModule,"gsapi_new_instance");
typedef int (WINAPI *gsapi_delete_instance) (char *);
gsapi_delete_instance lp_gsapi_delete_instance;
lp_gsapi_delete_instance = (gsapi_delete_instance) GetProcAddress(hModule,"gsapi_delete_instance");
typedef int (WINAPI *gsapi_init_with_args) (char *,int,char *);
gsapi_init_with_args lp_gsapi_init_with_args;
lp_gsapi_init_with_args = (gsapi_init_with_args) GetProcAddress(hModule,"gsapi_init_with_args");
typedef int (WINAPI *gsapi_exit) (char *);
gsapi_exit lp_gsapi_exit;
lp_gsapi_exit = (gsapi_exit) GetProcAddress(hModule,"gsapi_exit");
char ArgResolution[80] = "-r";
strcat(ArgResolution,Resolution);
char ArgOutputFile[MAX_PATH + 16] = "-sOutputFile=";
strcat(ArgOutputFile,PdfFile);
// *** Set up the parameters to the engine
const char * gsargv[11];
int gsargc;
gsargv[0] = "PsToPdf"; /* actual value doesn't matter */
gsargv[1] = "-dNOPAUSE"; // no prompts
gsargv[2] = "-dBATCH"; // exit after processing
gsargv[3] = "-dSAFER"; // Safe mode
gsargv[4] = ArgResolution;
gsargv[5] = ArgPaperSize;
gsargv[6] = "-sDEVICE=pdfwrite";
gsargv[7] = ArgOutputFile;
gsargv[8] = "-c";
gsargv[9] = ".setpdfwrite";
gsargv[10] = "-f";
gsargv[11] = PsFile;
gsargc=12;
// *** Simulate pointer to struct with plain char *
char *Inst = NULL;
int code = 0;
code = (lp_gsapi_new_instance)(&Inst,NULL);
if (code == 0)
{
// Do the conversion
code = (lp_gsapi_init_with_args) (Inst,gsargc,(char *) gsargv );
if (code == 0)
code = (lp_gsapi_exit) (Inst);
// *** Release the handle
(lp_gsapi_delete_instance) (Inst);
}
if (hModule)
FreeLibrary(hModule);
if (code == 0)
return TRUE;
return FALSE;
}