/*---------------------------------------------------------------------------------------------------------------------------------------------------
Program Listing for:  dllt2.cpp
Project:  printdll
Namespace:  c++
----------------------------------------------------------------------------------------------------------------------------------------------------
*/

#include <windows.h>
#include <winspool.h>
#include <string.h>
#include <stdio.h>
#include <mapi.h>


extern "C" __declspec(dllexport) char *SetDefPrint(char *);
extern "C" __declspec(dllexport) char *GetPrinters(void);
extern "C" __declspec(dllexport) char *GetStringFromMemory( void) ;
extern "C" __declspec(dllexport) int PutStringInMemory(char * str ) ;
extern "C" __declspec(dllexport) int SendEmail( char *name,
                                                                                 char *email, char *subject, char *text, char *from_email,
                                                                                          char *from_name);

char *buffer = NULL;

// for email use SMTP:user@site.com
extern "C" __declspec(dllexport) int SendEmail( char *name,
                                                                                 char *email, char *subject, char *text, char *from_email,
                                                                                          char *from_name)
{

         LPMAPILOGON fn1;
         LPMAPISENDMAIL fn2;
         LPMAPILOGOFF fn3;
         int return_val = 1;

         MapiRecipDesc recipient[2] = {{ 0, MAPI_TO, name, email, 0, NULL },
                           { 0, MAPI_ORIG, from_name, from_email, 0, NULL }          };

         MapiMessage message = { 0, subject, text, NULL, NULL, NULL,
                  0, NULL, 2, &(recipient[0]), 0, NULL };

         LHANDLE lhSession;
         HANDLE hMapiLib;
         hMapiLib = LoadLibrary("MAPI32.DLL");
         if (hMapiLib == NULL) return -1;

         fn1 = (LPMAPILOGON)GetProcAddress(hMapiLib, "MAPILogon");
         fn2 = (LPMAPISENDMAIL)GetProcAddress(hMapiLib, "MAPISendMail");
         fn3 = (LPMAPILOGOFF)GetProcAddress(hMapiLib, "MAPILogoff");

         if ( (*fn1)(0, NULL, NULL, 0, 0, &lhSession) != SUCCESS_SUCCESS ) {
                  return_val = -2;
         }
         else {
                                    
                  if ( (*fn2)(lhSession, 0, &message, 0, 0) != SUCCESS_SUCCESS )
                           return_val = -3;
                  else if ( (*fn3)(lhSession, 0, 0, 0) != SUCCESS_SUCCESS )
                                             return_val = -4;

         }
         FreeLibrary(hMapiLib);
         return (return_val);

}

extern "C" __declspec(dllexport) char *SetDefPrint(char *pcDefault)
{

         PRINTER_INFO_5 printinf[10];

         //DWORD number;
         DWORD x1;
         DWORD x2;
         unsigned int n;
         HANDLE phand;
         PRINTER_DEFAULTS  pd;
         BOOL result;
         PRINTER_INFO_2 pi2[10];
         DWORD nsize;
         DWORD received;
         char *ret;
         ret = "Failed";
         
         EnumPrinters(PRINTER_ENUM_LOCAL, 
                  NULL, 
                  5,
                  (LPBYTE)printinf,
                  200,
                  &x1,
                  &x2 );


         for (n=0;n<x2;n++)
         {
                  //CString out = printinf[n].pPrinterName;
                  //pDC->TextOut(5,5+ (n+1)*40, out, out.GetLength());
                  result = OpenPrinter( printinf[n].pPrinterName, &phand, &pd);
                  if (result)
                  {         

                           nsize = 10* sizeof(PRINTER_INFO_2);
                           GetPrinter( phand, 2, (LPBYTE)( pi2), nsize, &received);

                           if ( ! stricmp( pcDefault, printinf[n].pPrinterName ) )
                           {
                                    pi2[0].Attributes = PRINTER_ATTRIBUTE_DEFAULT ;
                                    SetPrinter( phand, 2, (LPBYTE)pi2 , 0);
                                    ret = "Default changed";
                           }

                           //CString out2;
                           //out2 = pi2[0].pPrinterName;

                           //out2.Format("bytes received: %i", received );
                           //pDC->TextOut(5,5+ (n+1)*40 + 20, out2, out2.GetLength() ); 

                  }
         }
 
         return (ret);
}


extern "C" __declspec(dllexport) char *GetPrinters(void)
{

         PRINTER_INFO_5 printinf[10];

         //DWORD number;
         DWORD x1;
         DWORD x2;
         unsigned int n;

         char far *ret;
         ret = new char[500];
         *ret = (char)0;
         
         EnumPrinters(PRINTER_ENUM_LOCAL, 
                  NULL, 
                  5,
                  (LPBYTE)printinf,
                  200,
                  &x1,
                  &x2 );


         for (n=0;n<x2;n++)
         {
                  if ( strlen(ret) + strlen(printinf[n].pPrinterName) > 500 )
                           break;
                  if (n>0)
                           strcat( ret, ";");
                  strcat( ret, printinf[n].pPrinterName );
         }
 
         return (ret);
}

extern "C" __declspec(dllexport) int PutStringInMemory(char * str ) 
{
         HANDLE hmmf;
         LPSTR lpMsg;
         char buffer2[501];

         hmmf = CreateFileMapping( (HANDLE) 0xFFFFFFFF, NULL,
                                    PAGE_READWRITE, 0, 0x1000, "MMFDEMO");

         if (hmmf == NULL) {
                  return -1;
         }
         lpMsg = (LPSTR)MapViewOfFile( hmmf, FILE_MAP_WRITE, 0, 0, 0);
         if (lpMsg == NULL ) {
                  return -2;
         }
         if (strlen(str) > 500) {
                  UnmapViewOfFile( lpMsg );
                  return -3;
         }
         sprintf( buffer2, "&%s", str );

         strcpy(lpMsg, buffer2 );
         UnmapViewOfFile( lpMsg );
         return 0;

}

extern "C" __declspec(dllexport) char *GetStringFromMemory( void) 
{
         HANDLE hmmf;
         LPSTR lpMsg;
         LPSTR badstring = "no info available";
         if (buffer != NULL) {
                  free(buffer) ;
         }

         buffer = (char *)malloc(500);
         if (! buffer) {
                  return "-3";
         }

         hmmf = CreateFileMapping( (HANDLE) 0xFFFFFFFF, NULL,
                                    PAGE_READWRITE, 0, 0x1000, "MMFDEMO");

         if (hmmf == NULL) {
                  return "-1";
         }
         lpMsg = (LPSTR)MapViewOfFile( hmmf, FILE_MAP_WRITE, 0, 0, 0);
         if (lpMsg == NULL ) {
                  return "-2";
         }
         if ( lpMsg[0] != '&' ) {
                  UnmapViewOfFile( lpMsg );
                  return badstring;
         }
         strncpy( buffer, lpMsg,499);
         buffer[499] = '\0';

         UnmapViewOfFile( lpMsg );

         return buffer;

}