syncapp.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 5k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. #include "syncapp.h"
  2. #ifndef WIN32
  3. #include <w32sys.h>             // for IsPEFormat definition
  4. #endif
  5. static TCHAR const g_szAppName [] = TEXT("SYNCAPP") ;
  6. static TCHAR const c_szDLL[]      = TEXT("SYNCUI.DLL");
  7. #ifdef UNICODE
  8. #    define BRIEFCASE_CREATE_ENTRY  "Briefcase_CreateW"
  9. #else
  10. #    define BRIEFCASE_CREATE_ENTRY  "Briefcase_Create"
  11. #endif
  12. static CHAR  const c_szFunction[] = BRIEFCASE_CREATE_ENTRY; // Lib entry point (never UNICODE)
  13. static HINSTANCE hInst;
  14. static HICON g_hIcon;
  15. static HINSTANCE g_hModule;
  16. static RUNDLLPROC g_lpfnCommand;
  17. static HWND g_hwndStub;
  18. static TCHAR s_szRunDLL32[] = TEXT("SYNCAPP.EXE ");
  19. static BOOL   ParseCommand(void)
  20. {
  21.         // Load the library and get the procedure address
  22.         // Note that we try to get a module handle first, so we don't need
  23.         // to pass full file names around
  24.         //
  25.         g_hModule = GetModuleHandle(c_szDLL);
  26.         if (g_hModule)
  27.         {
  28.                 TCHAR szName[MAXPATHLEN];
  29.                 GetModuleFileName(g_hModule, szName, ARRAYSIZE(szName));
  30.                 LoadLibrary(szName);
  31.         }
  32.         else
  33.         {
  34.                 g_hModule = LoadLibrary(c_szDLL);
  35.                 if ((UINT_PTR)g_hModule <= 32)
  36.                 {
  37.                         return(FALSE);
  38.                 }
  39.         }
  40.         g_lpfnCommand = (RUNDLLPROC)GetProcAddress(g_hModule, c_szFunction);
  41.         if (!g_lpfnCommand)
  42.         {
  43.                 FreeLibrary(g_hModule);
  44.                 return(FALSE);
  45.         }
  46.         return(TRUE);
  47. }
  48. LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
  49. {
  50.         switch(iMessage)
  51.         {
  52.         case WM_CREATE:
  53.                 g_hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_DEFAULT));
  54.                 break;
  55.         case WM_DESTROY:
  56.                 break;
  57.         default:
  58.                 return DefWindowProc(hWnd, iMessage, wParam, lParam) ;
  59.                 break;
  60.         }
  61.         return 0L;
  62. }
  63. static BOOL   InitStubWindow(HINSTANCE hInst, HINSTANCE hPrevInstance)
  64. {
  65.         WNDCLASS wndclass;
  66.         if (!hPrevInstance)
  67.         {
  68.                 wndclass.style         = 0 ;
  69.                 wndclass.lpfnWndProc   = (WNDPROC)WndProc ;
  70.                 wndclass.cbClsExtra    = 0 ;
  71.                 wndclass.cbWndExtra    = 0 ;
  72.                 wndclass.hInstance     = hInst ;
  73.                 wndclass.hIcon         = LoadIcon(hInst, MAKEINTRESOURCE(IDI_DEFAULT)) ;
  74.                 wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  75.                 wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  76.                 wndclass.lpszMenuName  = NULL ;
  77.                 wndclass.lpszClassName = g_szAppName ;
  78.                 if (!RegisterClass(&wndclass))
  79.                 {
  80.                         return(FALSE);
  81.                 }
  82.         }
  83.         g_hwndStub = CreateWindow(g_szAppName, TEXT(""), 0,
  84.                 0, 0, 0, 0, NULL, NULL, hInst, NULL);
  85.         return(g_hwndStub != NULL);
  86. }
  87. static void   CleanUp(void)
  88. {
  89.         DestroyWindow(g_hwndStub);
  90.         FreeLibrary(g_hModule);
  91. }
  92. int  WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszWinMainCmdLine, int nCmdShow)
  93. {
  94.         LPTSTR lpszCmdLine;
  95.         hInst = hInstance;
  96.         //
  97.         // The command line passed to WinMain is always ANSI, so for UNICODE
  98.         // builds we need to ask for the command line in UNICODE
  99.         //
  100. #ifdef UNICODE
  101.         //
  102.         // Since the command line returned from GetCommandLine includes
  103.         // argv[0], but the one passed to Winmain does not, we have
  104.         // to strip argv[0] in order to be equivalent
  105.         //
  106.         lpszCmdLine = GetCommandLine();
  107.         
  108.         //
  109.         // Skip past program name (first token in command line).
  110.         // Check for and handle quoted program name.
  111.         //
  112.         
  113.         if ( *lpszCmdLine == '"' ) 
  114.         {
  115.     
  116.             //
  117.             // Scan, and skip over, subsequent characters until
  118.             // another double-quote or a null is encountered.
  119.             //
  120.     
  121.             while ( *++lpszCmdLine && (*lpszCmdLine
  122.                  != '"') );
  123.             //
  124.             // If we stopped on a double-quote (usual case), skip
  125.             // over it.
  126.             //
  127.     
  128.             if ( *lpszCmdLine == '"' )
  129.                 lpszCmdLine++;
  130.         }
  131.         else 
  132.         {
  133.             while (*lpszCmdLine > ' ')
  134.                 lpszCmdLine++;
  135.         }
  136.         //
  137.         // Skip past any white space preceeding the second token.
  138.         //
  139.     
  140.         while (*lpszCmdLine && (*lpszCmdLine <= ' ')) 
  141.         {
  142.             lpszCmdLine++;
  143.         }
  144. #else
  145.         lpszCmdLine = lpszWinMainCmdLine;
  146. #endif
  147.         // turn off critical error stuff
  148.         SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  149.         if (!ParseCommand())
  150.         {
  151.                 goto Error0;
  152.         }
  153.         if (!InitStubWindow(hInstance, hPrevInstance))
  154.         {
  155.                 goto Error1;
  156.         }
  157.         (*g_lpfnCommand)(g_hwndStub, hInstance, lpszCmdLine, nCmdShow);
  158. Error1:
  159.         CleanUp();
  160. Error0:
  161.         return(FALSE);
  162. }