home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / APPSR / APPDLL / APPLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-10  |  16.2 KB  |  480 lines

  1. /*
  2. Applib.dll, dynamic link library for AppBar.exe, AppSetup.exe and the AppTools.
  3.  
  4.  by
  5.  GMP van kempen
  6.  NEVERnever Software 1991-1993
  7.  
  8. history:
  9.     4.00.1  Initial version
  10.     4.00.2  Added the IniFile parameter to the SaveButtonIni and
  11.         ReadButtonIni function so that AppMore and Morsetup can use them
  12.         too.
  13. */
  14.  
  15. #define STRICT
  16. #include <windows.h>
  17. #include <windowsx.h>
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <stdlib.h>
  21. #include "applib.h"
  22.  
  23. BOOL fWMCloseSeen = TRUE;
  24. FARPROC lpfnOrgCloseWndProc;
  25.  
  26. int FAR PASCAL LibMain(HINSTANCE hInstance, UINT wDataSeg, UINT wHeapSize,
  27.                LPSTR lpszCmdline)
  28.     {
  29.     if(wHeapSize > 0)
  30.     UnlockData(0);
  31.  
  32.     return 1;
  33.     }
  34.  
  35. /*--------------------------------------------------------------------------*/
  36. TEXTMETRIC WINAPI RetrieveTextMetrics(HWND hWnd)
  37.     {
  38.     int iOldMapMode;
  39.     HDC hDC;
  40.     TEXTMETRIC tm;
  41.  
  42.     hDC = GetDC(hWnd);
  43.     iOldMapMode = GetMapMode(hDC);
  44.     SetMapMode(hDC, MM_TEXT);
  45.     SelectFont(hDC, GetStockFont(SYSTEM_FONT));
  46.     GetTextMetrics(hDC, &tm);
  47.     SetMapMode(hDC, iOldMapMode);
  48.     DeleteDC(hDC);
  49.     return tm;
  50.     }
  51.  
  52. /*-------------------------------------------------------------------------*/
  53. void WINAPI DrawBitmap(HDC hdc, short x, short y, HBITMAP hBitmap)
  54.     {
  55.     BITMAP bm;
  56.     HDC    hMemDC;
  57.     POINT  pt;
  58.  
  59.     hMemDC = CreateCompatibleDC(hdc);
  60.     SelectObject(hMemDC, hBitmap);
  61.     GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &bm);
  62.     pt.x = bm.bmWidth;
  63.     pt.y = bm.bmHeight;
  64.  
  65.     BitBlt(hdc, x, y, pt.x, pt.y, hMemDC, 0, 0, SRCCOPY);
  66.  
  67.     DeleteDC(hMemDC);
  68.     }
  69.  
  70. /*----------------------------------------------------------------------------*/
  71. /* FUNCTION: ReadSystemIni(void)                              */
  72. /*                                                                            */
  73. /* PURPOSE:  Read [System] settings in the appbar.ini file.                  */
  74. /*----------------------------------------------------------------------------*/
  75. APPSYSTEM WINAPI ReadSystemIni(VOID)
  76.     {
  77.     APPSYSTEM AppSystem;
  78.     int itemp;
  79.  
  80.     // get ini-settings for AppSystem struct.
  81.     itemp = GetPrivateProfileInt(INI_SYSTEM, LEFT, LEFT_DEFAULT, INI_FILE);
  82.     AppSystem.Left = WITHIN(itemp, -1, MAXRES);
  83.  
  84.     itemp = GetPrivateProfileInt(INI_SYSTEM, TOP, TOP_DEFAULT, INI_FILE);
  85.     AppSystem.Top = WITHIN(itemp, -1, MAXRES);
  86.  
  87.     itemp = GetPrivateProfileInt(INI_SYSTEM, CLOSEALL, CLOSEALL_DEFAULT, INI_FILE);
  88.     AppSystem.CloseAll = ONEORZERO(itemp);
  89.  
  90.     itemp = GetPrivateProfileInt(INI_SYSTEM, STAYINFRONT, STAYINFRONT_DEFAULT, INI_FILE);
  91.     AppSystem.StayInFront = ONEORZERO(itemp);
  92.  
  93.     itemp = GetPrivateProfileInt(INI_SYSTEM, DOUBLECLICK, DOUBLECLICK_DEFAULT, INI_FILE);
  94.     AppSystem.DoubleClick = ONEORZERO(itemp);
  95.  
  96.     itemp = GetPrivateProfileInt(INI_SYSTEM, BIGBUTTONS, BIGBUTTONS_DEFAULT, INI_FILE);
  97.     AppSystem.BigButtons = ONEORZERO(itemp);
  98.  
  99.     itemp = GetPrivateProfileInt(INI_SYSTEM, INITIALIZED, INITIALIZED_DEFAULT, INI_FILE);
  100.     AppSystem.Initialized = WITHIN(itemp, 0, APPBARVERSION);
  101.  
  102.     itemp = GetPrivateProfileInt(INI_SYSTEM, BUTTONS, BUTTONS_DEFAULT, INI_FILE);
  103.     AppSystem.Buttons = WITHIN(itemp, 1, MAXAPPS);
  104.  
  105.     itemp = GetPrivateProfileInt(INI_SYSTEM, COLUMNS, COLUMNS_DEFAULT, INI_FILE);
  106.     AppSystem.Columns = WITHIN(itemp, 1, AppSystem.Buttons+1);
  107.  
  108.     itemp = GetPrivateProfileInt(INI_SYSTEM, BORDER, BORDER_DEFAULT, INI_FILE);
  109.     AppSystem.Border = WITHIN(itemp, 0, MAXBORDER);
  110.  
  111.     GetPrivateProfileString(INI_SYSTEM, DEFAULTICONDIR, AppSystem.DefaultIconDir,
  112.                AppSystem.DefaultIconDir, MAXFILECHARS-1, INI_FILE);
  113.  
  114.     GetPrivateProfileString(INI_SHELL, SHELLGROUP, SHELLGROUP_DEFAULT,
  115.                AppSystem.ShellGroup, 4*MAXFILECHARS-1, INI_FILE);
  116.  
  117.     return AppSystem;
  118.     }
  119.  
  120. /*----------------------------------------------------------------------------*/
  121. /* FUNCTION: ReadSoundIni(void)                              */
  122. /*                                                                            */
  123. /* PURPOSE:  Read [Sound] settings in the appbar.ini file.                  */
  124. /*----------------------------------------------------------------------------*/
  125. APPSOUND WINAPI ReadSoundIni(VOID)
  126.     {
  127.     int itemp;
  128.     APPSOUND AppSound;
  129.  
  130.     itemp = GetPrivateProfileInt(INI_SOUND, SOUNDENABLE, SOUNDENABLE_DEFAULT, INI_FILE);
  131.     AppSound.EnableSound = ONEORZERO(itemp);
  132.  
  133.     GetPrivateProfileString(INI_SOUND, APPSTART, APPSTART_DEFAULT,
  134.             AppSound.AppBarStart, MAXFILECHARS-1, INI_FILE);
  135.  
  136.     GetPrivateProfileString(INI_SOUND, APPEXIT, APPEXIT_DEFAULT,
  137.             AppSound.AppBarExit, MAXFILECHARS-1, INI_FILE);
  138.  
  139.     GetPrivateProfileString(INI_SOUND, WINEXIT, WINEXIT_DEFAULT,
  140.             AppSound.WindowsExit, MAXFILECHARS-1, INI_FILE);
  141.  
  142.     GetPrivateProfileString(INI_SOUND, PROGSTART, PROGSTART_DEFAULT,
  143.             AppSound.ProgramStart, MAXFILECHARS-1, INI_FILE);
  144.  
  145.     GetPrivateProfileString(INI_SOUND, PROGCLOSE, PROGCLOSE_DEFAULT,
  146.             AppSound.ProgramClose, MAXFILECHARS-1, INI_FILE);
  147.  
  148.     GetPrivateProfileString(INI_SOUND, DROPFILE, DROPFILE_DEFAULT,
  149.             AppSound.DropFile, MAXFILECHARS-1, INI_FILE);
  150.  
  151.     GetPrivateProfileString(INI_SOUND, ERRORMESSAGE, ERRORMESSAGE_DEFAULT,
  152.             AppSound.ErrorMessage, MAXFILECHARS-1, INI_FILE);
  153.  
  154.     GetPrivateProfileString(INI_SOUND, SOUNDDIR, SOUNDDIR_DEFAULT,
  155.             AppSound.SoundDirectory, MAXFILECHARS-1, INI_FILE);
  156.  
  157.     return AppSound;
  158.     }
  159.  
  160. /*----------------------------------------------------------------------------*/
  161. /* FUNCTION: ReadButtonIni(void)                              */
  162. /*                                                                            */
  163. /* PURPOSE:  Read settings of one Button from the appbar.ini file.                  */
  164. /*----------------------------------------------------------------------------*/
  165. APPBARBUTTONS WINAPI ReadButtonIni(int i, LPSTR SectionName, LPSTR IniFile)
  166.     {
  167.     char IniString[20];
  168.     APPBARBUTTONS AppButton;
  169.     int itemp, j;
  170.  
  171.     j = i+1;
  172.     wvsprintf(IniString,"Icon%d",&j);
  173.     GetPrivateProfileString(SectionName, IniString, AppButton.IcoName,
  174.                 AppButton.IcoName, MAXFILECHARS-1, IniFile);
  175.  
  176.     wvsprintf(IniString,"Prog%d",&j);
  177.     GetPrivateProfileString(SectionName, IniString, AppButton.ProgName,
  178.                 AppButton.ProgName, MAXFILECHARS-1, IniFile);
  179.  
  180.     wvsprintf(IniString,"Dir%d",&j);
  181.     GetPrivateProfileString(SectionName, IniString, AppButton.StartDir,
  182.                 AppButton.StartDir, MAXFILECHARS-1, IniFile);
  183.  
  184.     wvsprintf(IniString,"Params%d",&j);
  185.     GetPrivateProfileString(SectionName, IniString, AppButton.Params,
  186.                 AppButton.Params, MAXFILECHARS-1, IniFile);
  187.  
  188.     wvsprintf(IniString,"Close%d",&j);
  189.     itemp = GetPrivateProfileInt(SectionName, IniString, CLOSEPROG_DEFAULT, IniFile);
  190.     AppButton.Close = ONEORZERO(itemp);
  191.  
  192.     wvsprintf(IniString,"ShowMode%d",&j);
  193.     itemp = GetPrivateProfileInt(SectionName, IniString, SHOWMODE_DEFAULT, IniFile);
  194.     AppButton.ShowMode = WITHIN(itemp, 0, 3);
  195.  
  196.     wvsprintf(IniString,"IconNumber%d",&j);
  197.     itemp = GetPrivateProfileInt(SectionName, IniString, ICONNUMBER_DEFAULT, IniFile);
  198.     AppButton.IconNumber = itemp;
  199.     AppButton.IconNumber = max(AppButton.IconNumber, 0);
  200.  
  201.     wvsprintf(IniString,"ButtonLook%d",&j);
  202.     itemp = GetPrivateProfileInt(SectionName, IniString, BUTTONLOOK_DEFAULT, IniFile);
  203.     AppButton.ButtonLook = ONEORZERO(itemp);
  204.  
  205.     wvsprintf(IniString,"NoSound%d",&j);
  206.     itemp = GetPrivateProfileInt(SectionName, IniString, NOSOUND_DEFAULT, IniFile);
  207.     AppButton.NoSound = ONEORZERO(itemp);
  208.  
  209.     return AppButton;
  210.     }
  211.  
  212. /*--------------------------------------------------------------------------*/
  213. VOID WINAPI SaveSystemIni(APPSYSTEM AppSystem)
  214.     {
  215.     char SaveString[MAXDIGITS];
  216.  
  217.     wvsprintf(SaveString,"%d",&AppSystem.Left);
  218.     WritePrivateProfileString(INI_SYSTEM, LEFT, SaveString, INI_FILE);
  219.  
  220.     wvsprintf(SaveString,"%d",&AppSystem.Top);
  221.     WritePrivateProfileString(INI_SYSTEM, TOP, SaveString, INI_FILE);
  222.  
  223.     wvsprintf(SaveString,"%d",&AppSystem.Columns);
  224.     WritePrivateProfileString(INI_SYSTEM, COLUMNS, SaveString, INI_FILE);
  225.  
  226.     wvsprintf(SaveString,"%d",&AppSystem.Border);
  227.     WritePrivateProfileString(INI_SYSTEM, BORDER, SaveString, INI_FILE);
  228.  
  229.     wvsprintf(SaveString,"%d",&AppSystem.Buttons);
  230.     WritePrivateProfileString(INI_SYSTEM, BUTTONS, SaveString, INI_FILE);
  231.  
  232.     wvsprintf(SaveString,"%d",&AppSystem.CloseAll);
  233.     WritePrivateProfileString(INI_SYSTEM, CLOSEALL, SaveString, INI_FILE);
  234.  
  235.     wvsprintf(SaveString,"%d",&AppSystem.StayInFront);
  236.     WritePrivateProfileString(INI_SYSTEM, STAYINFRONT, SaveString, INI_FILE);
  237.  
  238.     wvsprintf(SaveString,"%d",&AppSystem.DoubleClick);
  239.     WritePrivateProfileString(INI_SYSTEM, DOUBLECLICK, SaveString, INI_FILE);
  240.  
  241.     wvsprintf(SaveString,"%d",&AppSystem.BigButtons);
  242.     WritePrivateProfileString(INI_SYSTEM, BIGBUTTONS, SaveString, INI_FILE);
  243.  
  244.     WritePrivateProfileString(INI_SHELL, SHELLGROUP, AppSystem.ShellGroup, INI_FILE);
  245.     }
  246.  
  247. /*--------------------------------------------------------------------------*/
  248. VOID WINAPI SaveSoundIni(APPSOUND AppSound)
  249.     {
  250.     char SaveString[MAXDIGITS];
  251.  
  252.     wvsprintf(SaveString,"%d",&AppSound.EnableSound);
  253.     WritePrivateProfileString(INI_SOUND, SOUNDENABLE, SaveString, INI_FILE);
  254.     WritePrivateProfileString(INI_SOUND, APPSTART, AppSound.AppBarStart, INI_FILE);
  255.     WritePrivateProfileString(INI_SOUND, APPEXIT, AppSound.AppBarExit, INI_FILE);
  256.     WritePrivateProfileString(INI_SOUND, WINEXIT, AppSound.WindowsExit, INI_FILE);
  257.     WritePrivateProfileString(INI_SOUND, PROGSTART, AppSound.ProgramStart, INI_FILE);
  258.     WritePrivateProfileString(INI_SOUND, PROGCLOSE, AppSound.ProgramClose, INI_FILE);
  259.     WritePrivateProfileString(INI_SOUND, DROPFILE, AppSound.DropFile, INI_FILE);
  260.     WritePrivateProfileString(INI_SOUND, ERRORMESSAGE, AppSound.ErrorMessage, INI_FILE);
  261.     WritePrivateProfileString(INI_SOUND, SOUNDDIR, AppSound.SoundDirectory, INI_FILE);
  262.     }
  263.  
  264. /*--------------------------------------------------------------------------*/
  265. VOID WINAPI SaveButtonIni(APPBARBUTTONS AppButton, int i, LPSTR SectionName, LPSTR IniFile)
  266.     {
  267.     char SaveString[MAXDIGITS], IniString[20];
  268.     int j;
  269.  
  270.     j= i+1;
  271.     wvsprintf(IniString,"Icon%d",&j);
  272.     WritePrivateProfileString(SectionName, IniString, AppButton.IcoName, IniFile);
  273.  
  274.     wvsprintf(IniString,"Prog%d",&j);
  275.     WritePrivateProfileString(SectionName, IniString, AppButton.ProgName, IniFile);
  276.  
  277.     wvsprintf(IniString,"Dir%d",&j);
  278.     WritePrivateProfileString(SectionName, IniString, AppButton.StartDir, IniFile);
  279.  
  280.     wvsprintf(IniString,"Params%d",&j);
  281.     WritePrivateProfileString(SectionName, IniString, AppButton.Params, IniFile);
  282.  
  283.     wvsprintf(IniString,"Close%d",&j);
  284.     wvsprintf(SaveString,"%d",&AppButton.Close);
  285.     WritePrivateProfileString(SectionName, IniString, SaveString, IniFile);
  286.  
  287.     wvsprintf(IniString,"ShowMode%d",&j);
  288.     wvsprintf(SaveString,"%d",&AppButton.ShowMode);
  289.     WritePrivateProfileString(SectionName, IniString, SaveString, IniFile);
  290.  
  291.     wvsprintf(IniString,"IconNumber%d",&j);
  292.     wvsprintf(SaveString,"%d",&AppButton.IconNumber);
  293.     WritePrivateProfileString(SectionName, IniString, SaveString, IniFile);
  294.  
  295.     wvsprintf(IniString,"ButtonLook%d",&j);
  296.     wvsprintf(SaveString,"%d",&AppButton.ButtonLook);
  297.     WritePrivateProfileString(SectionName, IniString, SaveString, IniFile);
  298.  
  299.     wvsprintf(IniString,"NoSound%d",&j);
  300.     wvsprintf(SaveString,"%d",&AppButton.NoSound);
  301.     WritePrivateProfileString(SectionName, IniString, SaveString, IniFile);
  302.     }
  303.  
  304. /*--------------------------------------------------------------------------*/
  305. APPBARBUTTONS WINAPI InitButton(VOID)
  306.     {
  307.     APPBARBUTTONS AppButtonDefault;
  308.  
  309.     lstrcpy(AppButtonDefault.IcoName, "");
  310.     lstrcpy(AppButtonDefault.ProgName, "");
  311.     lstrcpy(AppButtonDefault.Params, "");
  312.     lstrcpy(AppButtonDefault.StartDir, "");
  313.     AppButtonDefault.IconNumber = 0;
  314.     AppButtonDefault.ButtonLook = 0;
  315.     AppButtonDefault.Close = 1;
  316.     AppButtonDefault.ShowMode = 0;
  317.     AppButtonDefault.hWndApp = NULL;
  318.     AppButtonDefault.NoSound = 0;
  319.     AppButtonDefault.ProgStatus = 0;
  320.  
  321.     return AppButtonDefault;
  322.     }
  323.  
  324. /*--------------------------------------------------------------------------*/
  325. VOID WINAPI ExecuteErrorHandle(int iError, char *szError)
  326.     {
  327.     char szBuffer[256];
  328.  
  329.     switch(iError)
  330.     {
  331.     case 0:
  332.         lstrcpy(szBuffer,"Out of memory.");
  333.         break;
  334.  
  335.     case 2:
  336.         wvsprintf(szBuffer,"File %s not found.", szError);
  337.         break;
  338.  
  339.     case 3:
  340.         wvsprintf(szBuffer,"Path %s not found.", szError);
  341.         break;
  342.  
  343.     case 5:
  344.         lstrcpy(szBuffer,"Attempt to dynamically link to a task.");
  345.         break;
  346.  
  347.     case 6:
  348.         lstrcpy(szBuffer,"Library requires separate data segments\nfor each task.");
  349.         break;
  350.  
  351.     case 8:
  352.         wvsprintf(szBuffer,"Not enough memory to start %s.", szError);
  353.         break;
  354.  
  355.     case 10:
  356.         lstrcpy(szBuffer,"Incorrect Windows version.");
  357.         break;
  358.  
  359.     case 11:
  360.         lstrcpy(szBuffer,"Invalid .EXE file.");
  361.         break;
  362.  
  363.     case 12:
  364.         wvsprintf(szBuffer,"%s is an OS/2 application.", szError);
  365.         break;
  366.  
  367.     case 13:
  368.         wvsprintf(szBuffer,"%s is a DOS 4.0 application.", szError);
  369.         break;
  370.  
  371.     case 14:
  372.         wvsprintf(szBuffer,"%s has an unknown .EXE type.", szError);
  373.         break;
  374.  
  375.     case 15:
  376.         lstrcpy(szBuffer,"Attempt to load a Windows 2.x .EXE in protected mode.");
  377.         break;
  378.  
  379.     case 16:
  380.         lstrcpy(szBuffer,"Attempt to load a 2nd instance of\nan .EXE with writeable data.");
  381.         break;
  382.  
  383.     case 17:
  384.         lstrcpy(szBuffer,"Attempt to link a DLL in large-frame EMS mode.");
  385.         break;
  386.  
  387.     case 19:
  388.         lstrcpy(szBuffer,"Attempt to load compressed .EXE file.");
  389.         break;
  390.  
  391.     case 20:
  392.         lstrcpy(szBuffer,"Invalid DLL file.");
  393.         break;
  394.  
  395.     default:
  396.         wvsprintf(szBuffer,"Unknown File Execution Error (%d)",&iError);
  397.         break;
  398.     }
  399.     MessageBeep(0);
  400.     MessageBox(NULL, szBuffer, "AppBar - File Execution", MB_OK);
  401.     }
  402.  
  403. /*--------------------------------------------------------------------------*/
  404. VOID WINAPI ReadRunLoadIni(LPSTR szRun, LPSTR szLoad)
  405.     {
  406.     char szBuffer[256];
  407.  
  408.     GetProfileString("windows", "load", "  ", szBuffer, 255);
  409.     lstrcpy(szLoad, szBuffer);
  410.     GetProfileString("windows", "run", "  ", szBuffer, 255);
  411.     lstrcpy(szRun, szBuffer);
  412.     }
  413.  
  414. /*--------------------------------------------------------------------------*/
  415. VOID WINAPI SaveRunLoadIni(LPSTR szRun, LPSTR szLoad)
  416.     {
  417.     WriteProfileString("windows","load",szLoad);
  418.     WriteProfileString("windows","run",szRun);
  419.     }
  420.  
  421.  
  422. /*--------------------------------------------------------------------------*/
  423. LONG WINAPI WaitForWMCloseWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  424.     {
  425.     LRESULT  lRet;
  426.  
  427.     lRet = CallWindowProc((WNDPROC)lpfnOrgCloseWndProc, hWnd, message, wParam, lParam);
  428.     if(message == WM_CLOSE)
  429.     {
  430.     if(IsWindow (hWnd))
  431.         SetWindowLong(hWnd, GWL_WNDPROC, (WPARAM) lpfnOrgCloseWndProc);
  432.         fWMCloseSeen = TRUE;
  433.     }
  434.     return lRet;
  435.     }
  436.  
  437. /*------------------------------------------------------------------------*/
  438. VOID WINAPI WaitForWMClose(HWND hWndClose, HINSTANCE hInst)
  439.     {
  440.     static FARPROC  lpfnMy = NULL;
  441.     MSG msg;
  442.  
  443.     if(lpfnMy == NULL)
  444.     {
  445.     lpfnMy = MakeProcInstance((FARPROC) WaitForWMCloseWndProc, hInst);
  446.         if (lpfnMy == NULL)
  447.             return;
  448.     }
  449.     lpfnOrgCloseWndProc = (FARPROC) GetWindowLong(hWndClose, GWL_WNDPROC);
  450.     SetWindowLong(hWndClose, GWL_WNDPROC, (WPARAM) lpfnMy);
  451.     fWMCloseSeen = FALSE;
  452.  
  453.     //  This is a MUST here. We need to keep messages flowing around while
  454.     //  we're waiting a program to make up his mind with this close thing.
  455.     do    {
  456.     if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  457.         {
  458.         TranslateMessage(&msg);
  459.         DispatchMessage(&msg);
  460.         }
  461.     }
  462.     while(!fWMCloseSeen);
  463.  
  464.     // We've just seen WM_DESTROY let the app to clean up
  465.     // himself completely before we go on.
  466.     Yield();
  467.     FreeProcInstance((FARPROC)lpfnMy);
  468.     }
  469.  
  470. /*-------------------------------------------------------------------------*/
  471. BOOL WINAPI CloseApp(HWND hWndClose, HINSTANCE hInst)
  472.     {
  473.     PostMessage(hWndClose, WM_CLOSE, 0, 0L);
  474.     WaitForWMClose(hWndClose, hInst);
  475.     //if windows is still there, application refused to die.
  476.     if(IsWindow(hWndClose))
  477.     return FALSE;
  478.     return TRUE;
  479.     }
  480.