home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / internet / webpost / wbpost / wbpost.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-12  |  6.3 KB  |  299 lines

  1. //----------------------------------------------------------------------
  2. //       Copyright (C) 1993-1997 Microsoft Corporation.
  3. //       All rights reserved.
  4. //----------------------------------------------------------------------
  5.  
  6. //----------------------------------------------------------------------
  7. //    WebPost API usage example
  8. //
  9. //  This example allows the user to post files to a web site.
  10. //
  11. //----------------------------------------------------------------------
  12.  
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <tchar.h>
  18.  
  19. #include <wpapi.h>
  20. #include <wpspi.h>
  21.  
  22. #include "wbpost.h"
  23.  
  24. //global buffer for holding the error message
  25. TCHAR    g_rgchErr[MAX_PATH];
  26.  
  27. //you can always call WpGetErrorString to get the full error string text.
  28. VOID
  29. GetErrorString(LONG lErr)
  30. {
  31.     DWORD    dwErr;
  32.     LONG    lRet;
  33.  
  34.     dwErr=MAX_PATH;
  35.  
  36.     lRet=WpGetErrorString(
  37.         (UINT)lErr,            //uErrCode
  38.         (LPTSTR)g_rgchErr,    //sOutputBuf
  39.         &dwErr);            //pdwBufLen
  40.  
  41.     if(FAILED(lRet))
  42.         g_rgchErr[0]=NULL;
  43.  
  44.     return;
  45. }
  46.  
  47. LONG
  48. SimpleWebPost( LPTSTR lpszFileName )
  49. {
  50.     LONG     lRet;
  51.  
  52.     _tprintf("Calling WpPost(...,%s,...)\n", lpszFileName);
  53.  
  54.     lRet = WpPost(    NULL,        // hWin
  55.             1,        // count of files
  56.             &lpszFileName,    // file list
  57.             NULL,        // lpcbSiteName
  58.             NULL,        // lpszSiteName
  59.             NULL,        // lpcbURL
  60.             NULL,        // lpszURL
  61.             0 );        // flags
  62.     
  63.     GetErrorString(lRet);
  64.  
  65.     _tprintf("WpPost returned 0x%x which means %s\n", lRet, g_rgchErr);
  66.     
  67.     return lRet;
  68. }
  69.  
  70. LONG
  71. EnumProviders()
  72. {
  73.     static TCHAR    buffer[8192];
  74.     LPWPPROVINFO    lpbProv;
  75.     DWORD            cProv, cbProv, lRet, i;
  76.  
  77.     cbProv=sizeof(buffer);
  78.     cProv=0;
  79.     lpbProv=(LPWPPROVINFO)buffer;
  80.  
  81.     printf("Calling WpEnumProviders()\n");
  82.  
  83.     lRet=WpEnumProviders(&cbProv,    //pdwProvidersBufLen
  84.         lpbProv,                    //pProvidersBuffer
  85.         &cProv);                    //pdwNumProviders
  86.  
  87.     GetErrorString(lRet);
  88.  
  89.     _tprintf("WpEnumProviders() returned 0x%x which means %s\n", lRet, g_rgchErr);
  90.  
  91.     if(lRet)
  92.         return lRet;
  93.  
  94.     printf("Total Providers: %d\n", cProv);
  95.  
  96.     for(i=0; i<cProv; i++, lpbProv++)
  97.     {
  98.         _tprintf("%d, Name =%s\n", i, lpbProv->lpszProviderName);
  99.         _tprintf("   CLSID=%s\n", lpbProv->lpszProviderCLSID);
  100.         _tprintf("   Path =%s\n", lpbProv->lpszDllPath);
  101.     }
  102.  
  103.     return lRet;
  104. }
  105.  
  106.  
  107. LONG
  108. ListSites()
  109. {
  110.     static TCHAR    buffer[8192];
  111.     LPWPSITEINFO    lpbSites;
  112.     DWORD        cSites, cbSites, lRet, i;
  113.  
  114.     cbSites = sizeof( buffer );
  115.     cSites = 0;
  116.     lpbSites = (LPWPSITEINFO) buffer;
  117.  
  118.     printf("Calling WpListSites()\n");
  119.  
  120.     lRet = WpListSites( &cbSites, lpbSites, &cSites );
  121.  
  122.     GetErrorString(lRet);
  123.  
  124.     _tprintf("WpListSites() returned 0x%x which means %s\n", lRet, g_rgchErr);
  125.  
  126.     if (lRet)
  127.         return lRet;
  128.     
  129.     printf("Total Sites: %d\n", cSites);
  130.  
  131.     for (i = 0; i < cSites; i++, lpbSites++)
  132.         _tprintf("%d. SiteName=%s\n", i, lpbSites->lpszSiteName);
  133.     
  134.     return lRet;
  135. }
  136.  
  137. //
  138. // The next function illustrates getting a pointer to the web post
  139. // provider's interface and calling the functions in that interface.
  140. //
  141.  
  142. LONG
  143. AdvancedWebPost( LPTSTR lpszSiteName, LPTSTR lpszFileName )
  144. {
  145.     LONG    cbURL = 0;
  146.     LONG    lRet;
  147.     LONG    lErrType;
  148.     LONG    lErrBufLen;
  149.  
  150.     WCHAR    rgwchFileName[MAX_PATH];
  151.     WCHAR    rgwchError[MAX_PATH];
  152.     LPWSTR    lpwFileName;
  153.  
  154.     IWPProvider    *lpSite = NULL;
  155.  
  156.     _tprintf("Calling WpBindToSite(..., %s, ...)\n", lpszSiteName);
  157.  
  158.     lRet = WpBindToSite(NULL,        // hWin
  159.                 lpszSiteName,
  160.                 NULL,        // lpszURL
  161.                 0,        // fdwFlags
  162.                 0,        // dwReserved
  163.                 (PVOID *)&lpSite );    // Interface pointer
  164.  
  165.     GetErrorString(lRet);
  166.  
  167.     _tprintf("WpBindToSite returned 0x%x which means %s\n", lRet, g_rgchErr);
  168.  
  169.     if (lRet)
  170.         return lRet;
  171.  
  172.     //note that both the old IWPSite interface and the IWPProvider interface
  173.     //are unicode only.
  174.  
  175.     lRet = lpSite->NetworkConnect(NULL, NULL);
  176.     printf("NetworkConnect returned 0x%x\n", HRESULT_CODE(lRet));
  177.     if (lRet)
  178.         goto cleanup;
  179.  
  180.     lRet = lpSite->ServerLogin(NULL, NULL);
  181.     printf("ServerLogin returned 0x%x\n", HRESULT_CODE(lRet));
  182.     if (lRet)
  183.         goto cleanup;
  184.     
  185.     _tprintf("Calling PostFiles(...,%s,..)\n", lpszFileName);
  186.  
  187. #ifdef _UNICODE
  188.     lpwFileName=lpszFileName;
  189. #else
  190.     MultiByteToWideChar(CP_ACP, 0, lpszFileName, lstrlenA(lpszFileName), rgwchFileName, MAX_PATH);
  191.     lpwFileName=rgwchFileName;
  192. #endif
  193.  
  194.     lRet = lpSite->PostFiles(    1,    // cLocalPaths
  195.                     &lpwFileName,    //this has to be unicode
  196.                     &cbURL,    // lpcbURL
  197.                     NULL,    // lpszURL
  198.                     0 );    // fdwFlags
  199.     
  200.     lErrType=0;
  201.     lErrBufLen=MAX_PATH;
  202.     lpSite->GetError(
  203.         &lErrType,        //pdwErrorType
  204.         &lRet,            //pdwErrorCode
  205.         &lErrBufLen,    //pdwErrorBufLen
  206.         rgwchError);    //wsError
  207.  
  208.     wprintf(L"PostFiles returned 0x%x which means %s\n", HRESULT_CODE(lRet), rgwchError);
  209.  
  210.     lRet = lpSite->ServerLogout();
  211.     printf("ServerLogout returned 0x%x\n", HRESULT_CODE(lRet));
  212.     if (lRet)
  213.         goto cleanup;
  214.  
  215.     lRet = lpSite->NetworkDisconnect();
  216.     printf("NetworkDisconnect returned 0x%x\n", HRESULT_CODE(lRet));
  217.  
  218. cleanup:
  219.     lRet = lpSite->Release();
  220.     printf("Release returned 0x%x\n", HRESULT_CODE(lRet));
  221.  
  222.     return lRet;
  223. }
  224.  
  225. void
  226. usage(void)
  227. {
  228.     printf("Usage: wbpost [-s sitename] [filename]\n");
  229.     printf("       wbpost -l\t: lists the web sites\n");
  230.     printf("       wbpost -e\t: numerates the available providers\n");
  231.     exit(1);
  232. }
  233.  
  234. int __cdecl
  235. main(int argc, char *argv[])
  236. {
  237.     LPTSTR    lpszSiteName = NULL, lpszFileName = NULL;
  238.     int    fList = 0;
  239.     int    fEnum = 0;
  240.     LONG    lRet;
  241.     char    c;
  242.  
  243. #ifdef _UNICODE
  244.     WCHAR    rgwch[MAX_PATH];
  245. #endif
  246.  
  247.     // parse the arguments
  248.  
  249.     while (--argc > 0 && (*++argv)[0] == '-')
  250.         if (c = *++argv[0])
  251.             switch (c) {
  252.             case 'l':
  253.                 fList = 1;
  254.                 break;
  255.             case 'e':
  256.                 fEnum = 1;
  257.                 break;
  258.             case 's':
  259.                 if (--argc) {
  260. #ifdef _UNICODE
  261.                     MultiByteToWideChar(CP_ACP, 0, *++argv, lstrlenA(*argv), rgwch, MAX_PATH);
  262.                     lpszSiteName=rgwch;
  263. #else
  264.                     lpszSiteName = *++argv;
  265. #endif
  266.                 }
  267.                 break;
  268.             default:
  269.                 printf("Error: illegal option %c\n", c);
  270.                 usage();
  271.                 break;
  272.             }
  273.     if (argc == 1) {
  274. #ifdef _UNICODE
  275.         MultiByteToWideChar(CP_ACP, 0, *argv, lstrlenA(*argv), rgwch, MAX_PATH);
  276.         lpszFileName=rgwch;
  277. #else
  278.         lpszFileName = *argv;
  279. #endif
  280.         argc--;
  281.     }
  282.     
  283.     if (argc)
  284.         usage();
  285.     
  286.     // call the apis
  287.  
  288.     if (fList)
  289.         lRet = ListSites();
  290.     else if (fEnum)
  291.         lRet = EnumProviders();
  292.     else if (lpszSiteName)
  293.         lRet = AdvancedWebPost(lpszSiteName, lpszFileName);
  294.     else
  295.         lRet = SimpleWebPost(lpszFileName);
  296.     
  297.     return lRet;
  298. }
  299.