home *** CD-ROM | disk | FTP | other *** search
- //*---------------------------------------------------------------------------------
- //| ODBC Sample Translation DLL and Tool
- //|
- //| This code is furnished on an as-is basis as part of the ODBC SDK and is
- //| intended for example purposes only.
- //|
- //| Title: TRANSPEC.C
- //|
- //| Purpose:
- //| This program puts up a modal dialog which displays the current
- //| libraries which are loaded on the system. For TRANCHK.DLL,
- //| the user is allowed to update the current options for the
- //| loaded version.
- //|
- //| Note that the function used for displaying DLLs are located in
- //| TOOLHELP.DLL which is not available under Windows NT. The 32-bit
- //| version of this app therefore is not as interesting, but will still
- //| allow access to the TRNSLCHK .ini options.
- //|
- //*-------------------------------------------------------------------------
- #define _DCL_OPT_STRS
-
- #include <windows.h>
- #include <string.h>
- #include <stdlib.h>
- #include "options.h"
- #include "transpec.h"
-
- #ifndef WIN32
- #include "W16MACRO.H"
- #include "toolhelp.h"
- #else
- #include <windowsx.h>
- #endif
-
- VSZFile;
- #define NumItems(s) (sizeof(s) / sizeof(s[0]))
-
-
-
- //------------------------------------------------------------------------
- // Globals
- //------------------------------------------------------------------------
- OPTIONS optval;
- OPTIONS FAR * opt=&optval;
- HINSTANCE ghInst;
- HICON hIcon;
- int iHelpCnt=0;
-
-
- dCSEG(char) szDLL[] = ".DLL";
- dCSEG(char) szVBX[] = ".VBX";
- dCSEG(char) szud[] = "%u";
- dCSEG(char) szUnloadLibrary[] = "Unload library %s?";
- dCSEG(char) szHelpFile[] = "..\\HELP\\ODBCSMPL.HLP";
- dCSEG(char) szUNKNOWN[] = "unknown";
-
-
- //
- // Use following table on 32-bit to find processor type
- //
- #ifdef WIN32
- struct {
- DWORD dwType; // Processor flag
- LPSTR szPlatform; // Text name
- } Platforms[] = {
- PROCESSOR_INTEL_386, "80386",
- PROCESSOR_INTEL_486, "80486",
- PROCESSOR_INTEL_PENTIUM, "Pentium",
- PROCESSOR_INTEL_860, "860",
- PROCESSOR_MIPS_R2000, "R2000",
- PROCESSOR_MIPS_R3000, "R3000",
- PROCESSOR_MIPS_R4000, "R4000",
- PROCESSOR_ALPHA_21064, "Alpha",
- };
- #endif
-
-
-
- //------------------------------------------------------------------------
- // Local function prototypes
- //------------------------------------------------------------------------
- int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
- BOOL WINAPI Init(HINSTANCE, HINSTANCE, LPSTR, int);
- int WINAPI DoMain(HINSTANCE);
- BOOL EXTFUN SpecWndProc(HWND, UINT, WPARAM, LPARAM);
- VOID WINAPI SetVersionInfo(HWND hDlg);
- VOID WINAPI DoRefreshStats(HWND hDlg);
- int WINAPI RefreshLibraries(HWND hDlg);
- int WINAPI GetHandleToLib(HWND hDlg, HINSTANCE FAR * hi);
- BOOL WINAPI IsValidDLL(LPSTR instr);
- BOOL WINAPI fUnloadWarning(HWND hwnd);
-
-
- //*------------------------------------------------------------------------
- //| WinMain:
- //| Parameters:
- //| hInstance - Handle to current Data Segment
- //| hPrevInstance - Handle to previous Data Segment (NULL if none)
- //| lpszCmdLine - Long pointer to command line info
- //| nCmdShow - Integer value specifying how to start app.,
- //| (Iconic [7] or Normal [1,5])
- //*------------------------------------------------------------------------
- int WINAPI WinMain (HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpszCmdLine,
- int nCmdShow)
- {
- int nReturn;
-
- if (Init(hInstance, hPrevInstance,lpszCmdLine,nCmdShow))
- nReturn = DoMain(hInstance);
- return nReturn;
- }
-
-
-
-
- //*------------------------------------------------------------------------
- //| Init
- //| Initialization for the program is done here:
- //|
- //*------------------------------------------------------------------------
- BOOL WINAPI Init(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpszCmdLine, int nCmdShow)
- {
- HWND hwnd;
-
- // If it's already open, just maximize it or bring it to the top.
- if(hPrevInstance) {
- hwnd = FindWindow(NULL, szWinTitle);
- if(!hwnd)
- MessageBox(NULL, "Window not found by FindWindow", "FindWindow", MB_OK);
- if(IsIconic(hwnd))
- OpenIcon(hwnd);
- else
- BringWindowToTop(hwnd);
- return FALSE;
- }
-
- ghInst = hInstance;
- hIcon = LoadIcon(hInstance, "BASEICON");
-
- return TRUE;
- }
-
-
-
- //*------------------------------------------------------------------------
- //| DoMain:
- //| This is the main loop for the application:
- //*------------------------------------------------------------------------
- int WINAPI DoMain(HINSTANCE hInstance)
- {
- DLGPROC dlgproc;
-
- dlgproc = MakeProcInstance(SpecWndProc, hInstance);
- if(-1 == DialogBox(hInstance, "TRANSPEC", NULL, dlgproc))
- MessageBox(NULL, "Could not open dialog box.",
- "Transpec", MB_ICONEXCLAMATION);
- FreeProcInstance((FARPROC) dlgproc);
- return TRUE;
- }
-
-
-
-
-
- //*------------------------------------------------------------------------
- //| SpecDlgWndProc:
- //| Handle the messages for the dialog proc.
- //*------------------------------------------------------------------------
- BOOL EXTFUN SpecWndProc(HWND hDlg,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
- {
- switch(msg) {
- // Initialize the default values for the dialog box, as well as
- // add valid values.
- case WM_INITDIALOG:
- {
- opt = &optval;
- GetIniInfo(opt, TRUE);
- CenterDialog(hDlg);
- SetVersionInfo(hDlg);
- SendMessage(hDlg, USER_REINITDIALOG, 0, 0L);
- }
- return TRUE;
-
-
- case WM_ACTIVATEAPP:
- case USER_REINITDIALOG:
- DoRefreshStats(hDlg);
- return TRUE;
-
-
-
- #ifndef WIN32
- case USER_REFRESHLIBCOUNT:
- {
- HINSTANCE hInst;
- int cnt;
- char tmpbuff[_SMALLBUFF];
-
- cnt = GetHandleToLib(hDlg, &hInst);
- wsprintf(tmpbuff, szpd, cnt);
- SetWindowText(GetDlgItem(hDlg, IDT_COUNT), tmpbuff);
- wsprintf(tmpbuff, szud, hInst);
- SetWindowText(GetDlgItem(hDlg, IDT_HMOD), tmpbuff);
- }
- return TRUE;
- #endif
-
-
- case WM_COMMAND:
- switch(GET_WM_COMMAND_ID(wParam, lParam)) {
- case IDOK:
- SendMessage(hDlg, USER_REFRESHLIBCOUNT, 0, 0L);
- return TRUE;
-
- case IDB_CLOSE:
- WriteIniInfo(opt);
- SendMessage(hDlg, WM_CLOSE, 0, 0L);
- return TRUE;
-
- #ifndef WIN32
- //----------------------------------------------------------
- // Get the usage count of the current selection and
- // call FreeLibrary until its gone.
- //----------------------------------------------------------
- case IDB_UNLOAD:
- {
- HINSTANCE hInst;
- int cnt, cursel;
-
- if(fUnloadWarning(hDlg)) { // Make sure they know what they're doing
- cnt = GetHandleToLib(hDlg, &hInst);
- while(cnt--)
- FreeLibrary(hInst);
- cursel = (int) SendDlgItemMessage(hDlg, IDL_LIBS, LB_GETCURSEL, 0, 0L);
- SendDlgItemMessage(hDlg, IDL_LIBS, LB_DELETESTRING,
- (WPARAM)cursel--, 0L);
- SendDlgItemMessage(hDlg, IDL_LIBS, LB_SETCURSEL,
- (cursel >= 0) ? cursel : 0, 0L);
- SendMessage(hDlg, USER_REFRESHLIBCOUNT, 0, 0L);
- }
- }
- return TRUE;
- #endif
-
- case IDB_OPTIONS:
- HandleOptions(hDlg, ghInst, opt);
- return TRUE;
-
- case IDB_HELP:
- iHelpCnt += WinHelp(hDlg,szHelpFile,HELP_CONTEXT,HLP_TRNSLCHK);
- return TRUE;
-
- default:
- break;
- }
-
- #ifndef WIN32
- //----------------------------------------------------------------
- // If it wasn't a button, see if we are getting a notification
- // message that the user has changed selection.
- //----------------------------------------------------------------
- switch(GET_WM_COMMAND_CMD(wParam,lParam)) {
- case LBN_SELCHANGE:
- SendMessage(hDlg, USER_REFRESHLIBCOUNT, 0, 0L);
- return TRUE;
-
- default:
- return FALSE;
- }
- #endif
-
- return TRUE;
-
-
- case WM_CLOSE:
- if(iHelpCnt)
- WinHelp(hDlg, szHelpFile, HELP_QUIT, 0L);
- EndDialog(hDlg, FALSE);
- return TRUE;
-
-
- default:
- return FALSE;
- }
- }
-
-
-
-
- //*------------------------------------------------------------------------
- //| SetVersionInfo:
- //| Retrieve info on this system and fill out dialog accordingly.
- //*------------------------------------------------------------------------
- VOID WINAPI SetVersionInfo(HWND hDlg)
- {
- LPSTR szCPU=(LPSTR)szUNKNOWN;
- DWORD dwVers;
- WORD wWinVer;
- WORD wPlatform;
- char szVersion[25];
-
- #ifndef WIN32
- DWORD dwFlags;
- #else
- SYSTEM_INFO si;
- int dex;
- #endif
-
-
-
- //
- // Display the version of Windows we're on
- //
- dwVers = GetVersion();
- wWinVer = LOWORD(dwVers);
- wPlatform = HIWORD(dwVers);
-
- wsprintf(szVersion, "Windows %d.%d", LOBYTE(wWinVer),
- HIBYTE(wWinVer));
- #ifdef WIN32
- if(LOBYTE(wWinVer) <= 3) {
- if(wPlatform & 0x8000)
- lstrcat(szVersion, " (32s)");
- else
- lstrcat(szVersion, " (NT)");
- }
- #endif
- SetWindowText(GetDlgItem(hDlg, IDT_WINVER), szVersion);
-
-
- //
- // Now get the processor information
- //
- #ifndef WIN32
- dwFlags = GetWinFlags();
- szCPU = (dwFlags & WF_CPU286) ? (LPSTR)"80286" :
- (dwFlags & WF_CPU386) ? (LPSTR)"80386" :
- (dwFlags & WF_CPU486) ? (LPSTR)"80486" :
- (LPSTR)szUNKNOWN;
- #else
- GetSystemInfo(&si);
- for(dex=0; dex<NumItems(Platforms); dex++)
- if(si.dwProcessorType == Platforms[dex].dwType) {
- szCPU = Platforms[dex].szPlatform;
- break;
- }
- #endif
- SetWindowText(GetDlgItem(hDlg, IDT_CPU), szCPU);
- }
-
-
-
-
- //*------------------------------------------------------------------------
- //| DoRefreshStats:
- //| Refresh the amount of available memory.
- //*------------------------------------------------------------------------
- VOID WINAPI DoRefreshStats(HWND hDlg)
- {
- char szMem[_SMALLBUFF];
- DWORD dwFree;
- #ifdef WIN32
- MEMORYSTATUS mst;
- #endif
-
- // Calculate free memory and display
- #ifndef WIN32
- dwFree = GetFreeSpace(0) / 1024;
- #else
- mst.dwLength = sizeof(mst);
- GlobalMemoryStatus(&mst);
- dwFree = mst.dwAvailVirtual / 1024;
- #endif
- wsprintf((LPSTR)&szMem, "%lu", dwFree);
- SetDlgItemText(hDlg, IDT_FREEMEM, (LPSTR)szMem);
-
-
- #ifndef WIN32
- dwFree = GetFreeSystemResources(GFSR_SYSTEMRESOURCES);
- _ultoa(dwFree, szMem, 10);
- lstrcat(szMem, "%");
- SetWindowText(GetDlgItem(hDlg, IDT_FREESYS), szMem);
-
- RefreshLibraries(hDlg);
- #endif
- }
-
-
- #ifndef WIN32
- //*------------------------------------------------------------------------
- //| RefreshLibraries:
- //| This function enumerates the currently loaded libraries using the
- //| ToolHelp functions. Each is displayed in a list box.
- //*------------------------------------------------------------------------
- int WINAPI RefreshLibraries(HWND hDlg)
- {
- MODULEENTRY md;
- BOOL flag;
- int count=0;
-
- _fmemset(&md, 0, sizeof(MODULEENTRY));
- md.dwSize = sizeof(MODULEENTRY);
-
- // Kill all old entries. Use ModuleFirst and ModuleNext to loop
- // through all of the loaded libraries. For each one, add its
- // name to the listbox.
- SendDlgItemMessage(hDlg, IDL_LIBS, LB_RESETCONTENT, 0, 0L);
- flag = ModuleFirst(&md);
- while(flag) {
- if(IsValidDLL(md.szExePath)) {
- ++count;
- SendDlgItemMessage(hDlg, IDL_LIBS, LB_ADDSTRING,
- 0, (LPARAM)(LPSTR)md.szExePath);
- }
- flag = ModuleNext(&md);
- }
- SendDlgItemMessage(hDlg, IDL_LIBS, LB_SETCURSEL, 0, 0L);
- return count;
- }
-
-
-
-
-
- //*------------------------------------------------------------------------
- //| GetHandleToLib:
- //| This function gets the handle to the currently select module name
- //| in the libraries list box. It returns the load count of this
- //| library.
- //*------------------------------------------------------------------------
- int WINAPI GetHandleToLib(HWND hDlg, HINSTANCE FAR * hi)
- {
- MODULEENTRY md;
- int count=0;
- char szModName[_MAX_PATH];
- char szFile[35];
-
- *szFile = '\0';
- _fmemset(&md, 0, sizeof(MODULEENTRY));
- md.dwSize = sizeof(MODULEENTRY);
-
- SendDlgItemMessage(hDlg, IDL_LIBS, LB_GETTEXT,
- (WPARAM)SendDlgItemMessage(hDlg, IDL_LIBS, LB_GETCURSEL, 0, 0L),
- (LPARAM)(LPSTR)szModName);
- if(!*szModName)
- return 0;
-
- // Get just the file name (no extension)
- _splitpath(szModName, NULL, NULL, szFile, NULL);
- if(!*szFile)
- return 0;
-
- *hi = ModuleFindName(&md, szFile);
-
- return (*hi) ? md.wcUsage : 0;
- }
-
-
-
-
- //*------------------------------------------------------------------------
- //| IsValidDLL:
- //| This function verifies that the the module name returned is a
- //| .DLL file, since any module can be returned.
- //*------------------------------------------------------------------------
- BOOL WINAPI IsValidDLL(LPSTR instr)
- {
- char szext[5];
-
- _splitpath(instr, NULL, NULL, NULL, szext);
- if(!lstrcmp(szext, szDLL) ||
- !lstrcmp(szext, szVBX))
- return TRUE;
- else
- return FALSE;
- }
-
-
-
- //*------------------------------------------------------------------------
- //| WarnWndProc:
- //| Handle the messages for the dialog proc.
- //*------------------------------------------------------------------------
- BOOL EXTFUN WarnWndProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg) {
- case WM_INITDIALOG:
- CenterDialog(hDlg);
- CheckDlgButton(hDlg, IDX_NEVERAGAIN, TRUE);
- return TRUE;
-
-
- case WM_COMMAND:
- switch(GET_WM_COMMAND_ID(wParam, lParam)) {
- case IDOK:
- opt->fWarn = !IsDlgButtonChecked(hDlg, IDX_NEVERAGAIN);
- EndDialog(hDlg, IDOK);
- return TRUE;
-
- case IDCANCEL:
- opt->fWarn = !IsDlgButtonChecked(hDlg, IDX_NEVERAGAIN);
- EndDialog(hDlg, IDCANCEL);
- return TRUE;
- }
- break;
- }
-
- return FALSE;
- }
-
-
- //*------------------------------------------------------------------------
- //| fUnloadWarning:
- //| Ask user if they really want to unload the DLL. We'll store a
- //| constant in the .ini file so they can ignore this message.
- //| Return TRUE if unload may continue
- //*------------------------------------------------------------------------
- BOOL WINAPI fUnloadWarning(HWND hwnd)
- {
- DLGPROC dlgproc;
- BOOL fRtn=TRUE;
- int iRtn;
-
- if(!opt->fWarn) // No warning desired
- return TRUE;
-
- dlgproc = MakeProcInstance(WarnWndProc, ghInst);
- iRtn = DialogBox(ghInst,
- MAKEINTRESOURCE(IDD_WARNING), hwnd, dlgproc);
-
- if(iRtn == -1) {
- MessageBox(NULL, "Could not open dialog box.",
- "IDD_WARNING", MB_ICONEXCLAMATION);
- fRtn = FALSE;
- }
- else if(iRtn == IDCANCEL)
- fRtn = FALSE;
-
- FreeProcInstance((FARPROC) dlgproc);
-
- return fRtn;
- }
-
- #endif
-
-