/* FileMgr.C -- File Management program - all interface code */

#include "windows.h"		/* all Windows functions		*/
#include "winexp.h"		/* all extra Windows functions	*/
#include "filemgr.h"		/* all file manager defines		*/
#include "string.h"		/* all string functions			*/
#include "stdio.h"		/* all file i/o functions		*/
#include "stdlib.h"		/* misc functions				*/
#include "dos.h"			/* all dos file functions		*/
#include "direct.h"		/* all dos directory functions	*/
#include "ctype.h"		/* all character type functions	*/

extern void EnDecryptFiles(HANDLE hsel);
extern void MakeFilesInv(HANDLE hsel);
extern void MakeFilesVis(HANDLE hsel);
extern void MakeFilesRead(HANDLE hsel);
extern void MakeFilesRW(HANDLE hsel);
extern void MakeDirsInv(HANDLE hsel);
extern void MakeDirsVis(HANDLE hsel);

int		ntitle_len,
		ncharx,
		nchary,
		ntimes,
		ndup,
		nhide;
char		szappname[11+1],
		szkeyname1[11+1],
		szkeyname2[11+1],
		szkey[11+1],
		szerror1[49+1],
		szerror2[49+1],
		szerror3[49+1],
		szerror4[49+1],
		szendecrypt1[49+1],
		szendecrypt2[49+1],
		sztitle[49+1],
		szencrypt[49+1],
		szdecrypt[49+1],
		szinv[49+1],
		szvis[49+1],
		szread[49+1],
		szwrite[49+1],
		szdirinv[49+1],
		szdirvis[49+1],
		szfileext[]="*.*\0";
WORD		uattrib,
		uoldsel;
long		lseltot;
BOOL		nencrypt,
		ndecrypt,
		ninv,
		nvis,
		nread,
		nwrite,
		ndirinv,
		ndirvis,
		nendecrypt;
HANDLE	hinst,
		hnormcursor,
		hwaitcursor;


/* Gets the position in hsel to insert szdir */
WORD GetNextPos(HANDLE hsel, char *szdir)
{
  WORD uindex,ulength;
  static char szname[29+1];

  ulength = (WORD)SendMessage(hsel,LB_GETCOUNT,0,0L);
  for (uindex=0;
       uindex<ulength;
       uindex++)
  {
	SendMessage(hsel,LB_GETTEXT,uindex,(LONG)(LPSTR)szname);
	if ((strcmpi(szdir,szname)<0)||(szname[1]=='-'))
	  return uindex;
  }
  return -1;
}


WORD GetPos(HANDLE hsel, char *szdir)
{
  WORD uindex,ulength;
  static char szname[29+1];

  ulength = (WORD)SendMessage(hsel,LB_GETCOUNT,0,0L);
  for (uindex=0;
       uindex<ulength;
       uindex++)
  {
	SendMessage(hsel,LB_GETTEXT,uindex,(LONG)(LPSTR)szname);
	if (!strcmpi(szdir,szname))
	  return uindex;
  }
  return -1;
}


/* Procedure to delete read-only files in dir
 * listing.  There is a bug in DlgDirList() in
 * this regard
 */
void DelROFiles(HANDLE hsel)
{
  static struct find_t		f_tbuffer;
  static char szsearch[3+1];
  int nresult;

  strset(szsearch,0);
  strcpy(szsearch,"*.*\0");
  /* find all files */
  if (nresult=_dos_findfirst(szsearch,
				_A_HIDDEN,
				&f_tbuffer))
	return;
  while (!nresult)
  {
	/* if we found a hidden subdir */
	if (f_tbuffer.attrib&_A_RDONLY)
	{
	  /* delete from the list */
	  SendMessage(hsel,
	              LB_DELETESTRING,
	              GetPos(hsel,f_tbuffer.name),
	              0L);
	}
	nresult=_dos_findnext(&f_tbuffer);
  }
}


/* Procedure to add invisible directories to dir
 * listing.  There is a bug in DlgDirList() in
 * this regard
 */
void AddInvDirs(HANDLE hsel)
{
  static struct find_t		f_tbuffer;
  static char szdir[29+1],
              szsearch[1+1];
  int nresult;

  strset(szsearch,0);
  strcpy(szsearch,"*\0");
  strset(szdir,0);
  /* find all subdirectories */
  if (nresult=_dos_findfirst(szsearch,
				_A_HIDDEN|_A_SUBDIR,
				&f_tbuffer))
	return;
  while (!nresult)
  {
	/* if we found a hidden subdir */
	if (f_tbuffer.attrib&_A_HIDDEN)
	{
	  /* get its name */
	  strset(szdir,0);
	  strcpy(szdir,"[\0");
	  strcat(szdir,f_tbuffer.name);
	  strcat(szdir,"]\0");
	  /* add to the list */
	  SendMessage(hsel,
	              LB_INSERTSTRING,
	              GetNextPos(hsel,szdir),
	              (LONG)(LPSTR)szdir);
	}
	nresult=_dos_findnext(&f_tbuffer);
  }
}


/* Procedure to extract directory or drive name from
 * list box string
 */
char *GetDir(char *szlbstring)
{
  static char sztemp[29+1];
  int lbindex,
      tempindex;

  strset(sztemp,0);
  if (szlbstring[1] == '-') /* drive */
  {
	sztemp[0] = szlbstring[2];
	strcat(sztemp,":\0");
  }
  else /* directory */
  {
	tempindex = 0;
	lbindex = 1;
	while (szlbstring[lbindex] != ']')
	{
	  sztemp[tempindex]=szlbstring[lbindex];
	  tempindex++;
	  lbindex++;
	}
	sztemp[tempindex]=0;
  }
  return sztemp;
} /* GetDir */


/* Procedure to get the index of the last
 * selection made in a multiple selection box
 */
WORD GetLastSel(HANDLE hsel)
{
  WORD wtemp,wtemp2,unumsel;
  long ltemp;

  wtemp2=unumsel;
  for(wtemp=unumsel=0,ltemp=0L;
      wtemp<(WORD)SendMessage(hsel,LB_GETCOUNT,0,0L);
      wtemp++)
  {
	if (SendMessage(hsel,LB_GETSEL,wtemp,0L))
	{
	  unumsel++;
	  ltemp += (long)wtemp;
	}
  }
  if ((ltemp != lseltot)||(unumsel!=wtemp2))
  /* selection has changed */
  {
    if ((ltemp!=lseltot)||(unumsel==1))
    {
      if (unumsel!=1)
        uoldsel = LOWORD(labs(ltemp-lseltot));
      else
        uoldsel = LOWORD(ltemp);
    }
    lseltot = ltemp;
    return uoldsel;
  }
  else
  {
	return -1;
  }
} /* GetLastSel */


/* Procedure to control About dialog box */
BOOL FAR PASCAL About(HWND hdlg,
				 unsigned message,
				 WORD wparam,
				 LONG lparam)
{
	switch(message)
	{
	case WM_COMMAND:
		EndDialog(hdlg,0);
		return TRUE;
		break;
	case WM_INITDIALOG:
		return TRUE;
		break;
	default:
		return FALSE;
	}
} /* About */


/* Procedure to control File dialog box */
BOOL FAR PASCAL File(HWND hdlg,
				 unsigned message,
				 WORD wparam,
				 LONG lparam)
{
  static char szlbentry[29+1];

	switch(message)
	{
	case WM_COMMAND:
	  switch(wparam)
	  {
		case IDCANCEL:
		  EndDialog(hdlg,0);
		  return TRUE;
		  break;
		case IDOK:
		  nendecrypt = TRUE;
		  if ((nencrypt) || (ndecrypt))
		  {
			EnDecryptFiles(GetDlgItem(hdlg,102));
		  }
		  if (ninv)
		  {
		  	SetCursor(hwaitcursor);
			MakeFilesInv(GetDlgItem(hdlg,102));
			SetCursor(hnormcursor);
		  }
		  if (nvis)
		  {
		  	SetCursor(hwaitcursor);
			MakeFilesVis(GetDlgItem(hdlg,102));
			SetCursor(hnormcursor);
		  }
		  if (nread)
		  {
		  	SetCursor(hwaitcursor);
			MakeFilesRead(GetDlgItem(hdlg,102));
			SetCursor(hnormcursor);
		  }
		  if (nwrite)
		  {
		  	SetCursor(hwaitcursor);
			MakeFilesRW(GetDlgItem(hdlg,102));
			SetCursor(hnormcursor);
		  }
		  if (ndirinv)
		  {
		  	SetCursor(hwaitcursor);
			MakeDirsInv(GetDlgItem(hdlg,102));
			SetCursor(hnormcursor);
		  }
		  if (ndirvis)
		  {
		  	SetCursor(hwaitcursor);
			MakeDirsVis(GetDlgItem(hdlg,102));
			SetCursor(hnormcursor);
		  }
		  if (nendecrypt)
			EndDialog(hdlg,0);
		  return TRUE;
		  break;
		/* something happened in the listbox */
		case 102:
		  switch (HIWORD(lparam))
		  {
			case LBN_SELCHANGE:
			  /* update lseltot */
			  GetLastSel(LOWORD(lparam));
			  break;
			/* double-click */
			case LBN_DBLCLK:
			  /* if a directory go to it else
			   * start process
			   */
			  strset(szlbentry,0);
			  SendMessage(LOWORD(lparam),
							LB_GETTEXT,
							uoldsel,
							(long)(LPSTR)szlbentry);
			  if (szlbentry[0]=='[')
			  {
				lseltot = 0;
				strcpy(szlbentry,GetDir(szlbentry));
				strcat(szlbentry,"\\\0");
				strcat(szlbentry,szfileext);
				DlgDirList(hdlg, 
						(LPSTR)szlbentry,
				 		102,
				 		104,
				 		uattrib);
				if (ndirvis)
				  AddInvDirs(GetDlgItem(hdlg,102));
				if ((nencrypt)||(ndecrypt)||(nread))
				  DelROFiles(GetDlgItem(hdlg,102));
			  }
			  else /* start process */
				PostMessage(hdlg,
						WM_COMMAND,
						IDOK,
						0L);
			  return TRUE;
			  break;
			default:
			  return FALSE;
			  break;
	  	  }
	 	  break;
		default:
		  return FALSE;
		  break;
	  }
	  break;
	case WM_INITDIALOG:
		lseltot = 0L;
		if (nencrypt)
		{
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szencrypt);
		  uattrib = FA_HIDDEN|FA_SUBDIRS|FA_DRIVES;
		  DlgDirList(hdlg, 
				(LPSTR)szfileext,
				 102,
				 104,
				 uattrib);
		  DelROFiles(GetDlgItem(hdlg,102));
		}
		if (ndecrypt)
		{
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szdecrypt);
		  uattrib = FA_HIDDEN|FA_SUBDIRS|FA_DRIVES;
		  DlgDirList(hdlg, 
				(LPSTR)szfileext,
				 102,
				 104,
				 uattrib);
		  DelROFiles(GetDlgItem(hdlg,102));
		}
		if (ninv)
		{
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szinv);
		  uattrib = FA_READONLY|FA_SUBDIRS|FA_DRIVES;
		  DlgDirList(hdlg, 
				(LPSTR)szfileext,
				 102,
				 104,
				 uattrib);
		}
		if (nvis)
		{
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szvis);
		  uattrib = FA_HIDDEN|FA_SUBDIRS|FA_DRIVES|FA_EXCLUSIVE;
		  DlgDirList(hdlg, 
				(LPSTR)szfileext,
				 102,
				 104,
				 uattrib);
		}
		if (nread)
		{
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szread);
		  uattrib = FA_HIDDEN|FA_SUBDIRS|FA_DRIVES;
		  DlgDirList(hdlg, 
				(LPSTR)szfileext,
				 102,
				 104,
				 uattrib);
		  DelROFiles(GetDlgItem(hdlg,102));
		}
		if (nwrite)
		{
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szwrite);
		  uattrib = FA_READONLY|FA_HIDDEN|FA_SUBDIRS|FA_DRIVES|FA_EXCLUSIVE;
		  DlgDirList(hdlg, 
				(LPSTR)szfileext,
				 102,
				 104,
				 uattrib);
		}
		if (ndirinv)
		{
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szdirinv);
		  uattrib = FA_SUBDIRS|FA_DRIVES|FA_EXCLUSIVE;
		  DlgDirList(hdlg, 
				(LPSTR)szfileext,
				 102,
				 104,
				 uattrib);
		}
		if (ndirvis)
		{
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szdirvis);
		  uattrib = FA_SUBDIRS|FA_DRIVES|FA_EXCLUSIVE;
		  DlgDirList(hdlg, 
				(LPSTR)szfileext,
				 102,
				 104,
				 uattrib);
		  AddInvDirs(GetDlgItem(hdlg,102));
		}
		return TRUE;
		break;
	default:
		return FALSE;
	}
} /* File */


/* Procedure to control Key dialog box */
BOOL FAR PASCAL Key(HWND hdlg,
				 unsigned message,
				 WORD wparam,
				 LONG lparam)
{
  static char sztemp[11+1];
  FARPROC lpproc;

	switch(message)
	{
	case WM_COMMAND:
	  switch(wparam)
	  {
		case IDCANCEL:
		  nendecrypt = FALSE;
		  ntimes--;
		  EndDialog(hdlg,0);
		  return TRUE;
		  break;
		case IDOK:
		  GetDlgItemText(hdlg,101,(LPSTR)szkey,11);
		  if (szkey[0]==0)
		  {
			MessageBox(hdlg,
					(LPSTR)szerror3,
					(LPSTR)NULL,
					MB_ICONEXCLAMATION | MB_OK);
			nendecrypt = FALSE;
			return TRUE;
			break;
		  }
		  if (ntimes==1) /* first time thru */
		  {
			strset(sztemp,0);
			strcpy(sztemp,szkey);
			lpproc = MakeProcInstance(Key,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(KEYBOX),
					hdlg,
					lpproc);
			FreeProcInstance(lpproc);
			if ((nendecrypt)&&(strcmp(sztemp,szkey)))
			{
			  MessageBox(hdlg,
						(LPSTR)szerror4,
						(LPSTR)NULL,
						MB_ICONEXCLAMATION | MB_OK);
			  nendecrypt = FALSE;
			}
		  }
		  ntimes--;
		  EndDialog(hdlg,0);
		  return TRUE;
		  break;
		default:
		  return FALSE;
		  break;
	  }
	  break;
	case WM_INITDIALOG:
		strset(szkey,0);
		if (ntimes==0)
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szendecrypt1);
		else
		  SetDlgItemText(hdlg, 
					100, 
					(LPSTR)szendecrypt2);
		ntimes++;
		return TRUE;
		break;
	default:
		return FALSE;
	}
} /* Key */


/* Procedure to control Lock window */
long FAR PASCAL WndProc(HWND hwnd, 
				 unsigned message, 
				 WORD wparam, 
				 LONG lparam)
{
	FARPROC		lpproc;
	static char		sztemp[11+1];

	switch(message)
	{
	case WM_CLOSE:
		PostQuitMessage(0);
		return TRUE;
		break;
	case WM_INITMENUPOPUP:
		return TRUE;
		break;
	case WM_COMMAND:
		switch(wparam)
		{
		  case IDCUT:
			  PostMessage(GetFocus(),
						WM_CUT,
						0,
						0L);
			  break;
		  case IDCOPY:
			  PostMessage(GetFocus(),
						WM_COPY,
						0,
						0L);
			  break;
		  case IDPASTE:
			  PostMessage(GetFocus(),
						WM_PASTE,
						0,
						0L);
			  break;
		  case IDABOUT:
			lpproc = MakeProcInstance(About,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(ABOUTBOX),
					hwnd,
					lpproc);
			FreeProcInstance(lpproc);
			break;
		  case IDEXIT:
			PostMessage(hwnd,WM_CLOSE,0,0L);
			break;
		  case IDENCRYPT:
			nencrypt = TRUE;
			ndecrypt = FALSE;
			ninv = FALSE;
			nvis = FALSE;
			nread = FALSE;
			nwrite = FALSE;
			ndirinv = FALSE;
			ndirvis = FALSE;
			lpproc = MakeProcInstance(File,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(FILEBOX),
					hwnd,
					lpproc);
			FreeProcInstance(lpproc);
			break;
		  case IDDECRYPT:
			nencrypt = FALSE;
			ndecrypt = TRUE;
			ninv = FALSE;
			nvis = FALSE;
			nread = FALSE;
			nwrite = FALSE;
			ndirinv = FALSE;
			ndirvis = FALSE;
			lpproc = MakeProcInstance(File,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(FILEBOX),
					hwnd,
					lpproc);
			FreeProcInstance(lpproc);
			break;
		  case IDINVISIBLE:
			nencrypt = FALSE;
			ndecrypt = FALSE;
			ninv = TRUE;
			nvis = FALSE;
			nread = FALSE;
			nwrite = FALSE;
			ndirinv = FALSE;
			ndirvis = FALSE;
			lpproc = MakeProcInstance(File,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(FILEBOX),
					hwnd,
					lpproc);
			FreeProcInstance(lpproc);
			break;
		  case IDVISIBLE:
			nencrypt = FALSE;
			ndecrypt = FALSE;
			ninv = FALSE;
			nvis = TRUE;
			nread = FALSE;
			nwrite = FALSE;
			ndirinv = FALSE;
			ndirvis = FALSE;
			lpproc = MakeProcInstance(File,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(FILEBOX),
					hwnd,
					lpproc);
			FreeProcInstance(lpproc);
			break;
		  case IDREAD:
			nencrypt = FALSE;
			ndecrypt = FALSE;
			ninv = FALSE;
			nvis = FALSE;
			nread = TRUE;
			nwrite = FALSE;
			ndirinv = FALSE;
			ndirvis = FALSE;
			lpproc = MakeProcInstance(File,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(FILEBOX),
					hwnd,
					lpproc);
			FreeProcInstance(lpproc);
			break;
		  case IDWRITE:
			nencrypt = FALSE;
			ndecrypt = FALSE;
			ninv = FALSE;
			nvis = FALSE;
			nread = FALSE;
			nwrite = TRUE;
			ndirinv = FALSE;
			ndirvis = FALSE;
			lpproc = MakeProcInstance(File,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(FILEBOX),
					hwnd,
					lpproc);
			FreeProcInstance(lpproc);
			break;
		  case IDDIRINVIS:
			nencrypt = FALSE;
			ndecrypt = FALSE;
			ninv = FALSE;
			nvis = FALSE;
			nread = FALSE;
			nwrite = FALSE;
			ndirinv = TRUE;
			ndirvis = FALSE;
			lpproc = MakeProcInstance(File,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(FILEBOX),
					hwnd,
					lpproc);
			FreeProcInstance(lpproc);
			break;
		  case IDDIRVIS:
			nencrypt = FALSE;
			ndecrypt = FALSE;
			ninv = FALSE;
			nvis = FALSE;
			nread = FALSE;
			nwrite = FALSE;
			ndirinv = FALSE;
			ndirvis = TRUE;
			lpproc = MakeProcInstance(File,hinst);
			DialogBox(hinst,
					MAKEINTRESOURCE(FILEBOX),
					hwnd,
					lpproc);
			FreeProcInstance(lpproc);
			break;
		  case IDDUP:
			if (CheckMenuItem(GetMenu(hwnd),
			                  IDDUP,
						  MF_CHECKED)
			    ==MF_UNCHECKED)
			  ndup=MF_CHECKED;
			else
			{
			  ndup=MF_UNCHECKED;
			  CheckMenuItem(GetMenu(hwnd),
			                IDDUP,
						MF_UNCHECKED);
			}
			break;
		  case IDHIDE:
			if (CheckMenuItem(GetMenu(hwnd),
			                  IDHIDE,
						  MF_CHECKED)
			    ==MF_UNCHECKED)
			  nhide=MF_CHECKED;
			else
			{
			  nhide=MF_UNCHECKED;
			  CheckMenuItem(GetMenu(hwnd),
			                IDHIDE,
						MF_UNCHECKED);
			}
			break;
		  case IDSAVE:
			  WriteProfileString((LPSTR)szappname,
		                          (LPSTR)szkeyname1,
		                          (LPSTR)itoa(ndup,
			                                 sztemp,
			                                 10));
			  WriteProfileString((LPSTR)szappname,
		                          (LPSTR)szkeyname2,
		                          (LPSTR)itoa(nhide,
			                                 sztemp,
			                                 10));
			break;
		}
		return TRUE;
		break;
	default:
		return DefWindowProc(hwnd,message,
						wparam,lparam);
	}
} /* WndProc */


/* Procedure called when the application is loaded for the
 * first time 
 */
BOOL WndInit(HANDLE hinstance)
{
	HANDLE			hwnd,hdc;
	PWNDCLASS   		pwndclass;
	static TEXTMETRIC		tm;

	/* Load strings from resource */
	LoadString(hinstance, 
			IDSNAME, 
			(LPSTR)szappname, 
			10);
	ntitle_len=LoadString(hinstance, 
					IDSTITLE, 
					(LPSTR)sztitle,
					45);
	LoadString(hinstance, 
			IDSKEY1, 
			(LPSTR)szkeyname1, 
			10);
	LoadString(hinstance, 
			IDSKEY2, 
			(LPSTR)szkeyname2, 
			10);
	LoadString(hinstance, 
			IDSENCRYPT, 
			(LPSTR)szencrypt, 
			45);
	LoadString(hinstance, 
			IDSDECRYPT, 
			(LPSTR)szdecrypt, 
			45);
	LoadString(hinstance, 
			IDSINV, 
			(LPSTR)szinv, 
			45);
	LoadString(hinstance, 
			IDSVIS, 
			(LPSTR)szvis, 
			45);
	LoadString(hinstance, 
			IDSREAD, 
			(LPSTR)szread, 
			45);
	LoadString(hinstance, 
			IDSWRITE, 
			(LPSTR)szwrite, 
			45);
	LoadString(hinstance, 
			IDSDIRINV, 
			(LPSTR)szdirinv, 
			45);
	LoadString(hinstance, 
			IDSDIRVIS, 
			(LPSTR)szdirvis, 
			45);
	LoadString(hinstance, 
			IDSERROR1, 
			(LPSTR)szerror1, 
			45);
	LoadString(hinstance, 
			IDSERROR2, 
			(LPSTR)szerror2, 
			45);
	LoadString(hinstance, 
			IDSERROR3, 
			(LPSTR)szerror3, 
			45);
	LoadString(hinstance, 
			IDSERROR4, 
			(LPSTR)szerror4, 
			45);
	LoadString(hinstance, 
			IDSENDECRYPT1, 
			(LPSTR)szendecrypt1, 
			45);
	LoadString(hinstance, 
			IDSENDECRYPT2, 
			(LPSTR)szendecrypt2, 
			45);

	hnormcursor = LoadCursor(NULL,IDC_ARROW);
	hwaitcursor = LoadCursor(NULL,IDC_WAIT);

	pwndclass = (PWNDCLASS)LocalAlloc(LPTR,
								sizeof(WNDCLASS));

	pwndclass->hCursor      = hnormcursor;
	pwndclass->hIcon        = LoadIcon(hinstance,
								(LPSTR)szappname);
	pwndclass->lpszMenuName = (LPSTR)szappname;
	pwndclass->lpszClassName= (LPSTR)szappname;
	pwndclass->hbrBackground= COLOR_WINDOW+1;
	pwndclass->hInstance    = hinstance;
	pwndclass->style        = CS_HREDRAW | CS_VREDRAW;
	pwndclass->lpfnWndProc  = WndProc;

	if (!RegisterClass( (LPWNDCLASS)pwndclass))
	/* Initialization failed.
		* Windows will automatically deallocate all
		* allocated memory.
		*/
		return FALSE;

	hwnd = CreateWindow((LPSTR)szappname,
					(LPSTR)sztitle,
					WS_OVERLAPPEDWINDOW,
					0,
					0,
					0,
					0,
					(HWND)NULL,
					(HMENU)NULL,
					hinstance,
					(LPSTR)NULL);
     hdc = GetDC(hwnd);
     GetTextMetrics(hdc,(LPTEXTMETRIC)&tm);
     nchary = tm.tmHeight + 2;
     ncharx = tm.tmMaxCharWidth;
     ReleaseDC(hwnd,hdc);
     DestroyWindow(hwnd);

	LocalFree( (HANDLE)pwndclass);
	return( TRUE );        /* Initialization succeeded */
} /* WndInit */


/* WinMain - Main loop for all Windows applications */
int PASCAL WinMain (HANDLE hinstance,
                    		HANDLE hprevinstance,
                    		LPSTR  lpszcmdline, 
                    		int    ncmdshow)
{
	HWND		hwnd,hacc,hdc;
	static MSG	msg;
	static char sztemp[11+1];

	if (!hprevinstance)
	/* Call initialization procedure if this is the
	 * first instance 
	 */
	{
	  if (!WndInit(hinstance))
		return( FALSE );
	}
	else
	{
	  GetInstanceData(hprevinstance,szappname,10);
	  GetInstanceData(hprevinstance,sztitle,45);
	  GetInstanceData(hprevinstance,(PSTR)&ntitle_len,2);
	  GetInstanceData(hprevinstance,szkeyname1,10);
	  GetInstanceData(hprevinstance,szkeyname2,10);
	  GetInstanceData(hprevinstance,szencrypt,45);
	  GetInstanceData(hprevinstance,szdecrypt,45);
	  GetInstanceData(hprevinstance,szinv,45);
	  GetInstanceData(hprevinstance,szvis,45);
	  GetInstanceData(hprevinstance,szread,45);
	  GetInstanceData(hprevinstance,szwrite,45);
	  GetInstanceData(hprevinstance,szdirinv,45);
	  GetInstanceData(hprevinstance,szdirvis,45);
	  GetInstanceData(hprevinstance,szerror1,45);
	  GetInstanceData(hprevinstance,szerror2,45);
	  GetInstanceData(hprevinstance,szerror3,45);
	  GetInstanceData(hprevinstance,szerror4,45);
	  GetInstanceData(hprevinstance,szendecrypt1,45);
	  GetInstanceData(hprevinstance,szendecrypt2,45);
	  GetInstanceData(hprevinstance,(PSTR)&hnormcursor,2);
	  GetInstanceData(hprevinstance,(PSTR)&hwaitcursor,2);
	  GetInstanceData(hprevinstance,(PSTR)&nchary,2);
	  GetInstanceData(hprevinstance,(PSTR)&ncharx,2);
	}

	/* Save hInstance for dlg box calls */
	hinst = hinstance;

	hacc = LoadAccelerators(hinstance,(LPSTR)szappname); 

	/* create window */
	hwnd = CreateWindow((LPSTR)szappname,
					(LPSTR)sztitle,
					WS_OVERLAPPEDWINDOW,
					ncharx*10,
					nchary*5,
					ncharx*(ntitle_len+25),
					nchary*20,
					(HWND)NULL,
					(HMENU)NULL,
					hinstance,
					(LPSTR)NULL);

	/* show window */
	ShowWindow(hwnd, ncmdshow);
	UpdateWindow(hwnd);

	/* Get or Init default menu options */
	if ((ndup=GetProfileInt((LPSTR)szappname,
	                         (LPSTR)szkeyname1,
	                         -1))==-1)
	{
		ndup=(int)MF_UNCHECKED;
		nhide=(int)MF_UNCHECKED;
		WriteProfileString((LPSTR)szappname,
		                   (LPSTR)szkeyname1,
		                   (LPSTR)itoa(MF_UNCHECKED,
			                          sztemp,
			                          10));
		WriteProfileString((LPSTR)szappname,
		                   (LPSTR)szkeyname2,
		                   (LPSTR)itoa(MF_UNCHECKED,
			                          sztemp,
			                          10));
	}
	else
	{
	  nhide=GetProfileInt((LPSTR)szappname,
	                       (LPSTR)szkeyname2,
	                       MF_UNCHECKED);
	}
	CheckMenuItem(GetMenu(hwnd),IDDUP,(WORD)ndup);
	CheckMenuItem(GetMenu(hwnd),IDHIDE,(WORD)nhide);

	while (GetMessage((LPMSG)&msg,(HWND)NULL,0,0))
	  {
		if (!TranslateAccelerator(hwnd,hacc,(LPMSG)&msg))
		{
		  TranslateMessage((LPMSG)&msg);
		  DispatchMessage((LPMSG)&msg);
		}
	  }


	/* exit back to Windows */
	exit(msg.wParam);

} /* WinMain */

