Back to index

lightning-sunbird  0.9+nobinonly
xpapi.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 //
00038 //  XPAPI.CPP
00039 //  API implementation file for mapi16.dll and mapi32.dll
00040 //  Written by: Rich Pizzarro (rhp@netscape.com)
00041 //  November 1997
00042 //
00043 #include <windows.h>
00044 #include <stdio.h>
00045 #include <stdarg.h>
00046 #include "xpapi.h"
00047 #include "mapiutl.h"
00048 
00049 WORD LOAD_DS XP_CallProcess(LPCSTR pPath, LPCSTR pCmdLine)
00050 {            
00051        WORD wReturn = 0;
00052 #ifndef WIN16
00053        STARTUPINFO startupInfo;
00054        PROCESS_INFORMATION processInfo;
00055 
00056        memset(&startupInfo, 0, sizeof(startupInfo));
00057        startupInfo.cb = sizeof(startupInfo);
00058        if (wReturn = CreateProcess(pPath, (LPTSTR)pCmdLine, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_PROCESS_GROUP, NULL, NULL, &startupInfo, &processInfo))
00059        {
00060               WaitForInputIdle(processInfo.hProcess, 120000);
00061        }
00062 #else       
00063        // char szMsg[80];   
00064        char szExecute[512];
00065        lstrcpy(szExecute, pPath);
00066        lstrcat(szExecute, " ");
00067        lstrcat(szExecute, pCmdLine);
00068        wReturn = WinExec(szExecute,SW_SHOW);
00069        
00070        if (wReturn < 32) 
00071        {
00072            wReturn = 0;
00073        }
00074 #endif
00075        return wReturn;
00076 }
00077 
00078 HKEY LOAD_DS RegOpenParent(LPCSTR pSection, HKEY hRootKey, REGSAM access)
00079 {
00080        HKEY hKey;
00081 #ifndef WIN16
00082        if (RegOpenKeyEx(hRootKey, pSection, 0, access, &hKey) != ERROR_SUCCESS)
00083        {
00084               return(NULL);
00085        }        
00086 #else
00087     if (RegOpenKey(hRootKey, pSection, &hKey) != ERROR_SUCCESS)
00088     {
00089        return(NULL);
00090     }
00091 #endif
00092        return(hKey);
00093 }
00094 
00095 HKEY LOAD_DS RegCreateParent(LPCSTR pSection, HKEY hMasterKey)
00096 {
00097        HKEY hKey;
00098        if (RegCreateKey(hMasterKey, pSection, &hKey) != ERROR_SUCCESS)
00099        {
00100               return(NULL);
00101        }        
00102        return(hKey);
00103 }
00104 
00105 BOOL LOAD_DS GetConfigInfoStr(LPCSTR pSection, LPCSTR pKey, LPSTR pBuf, int lenBuf, HKEY hMasterKey)
00106 {
00107        HKEY hKey;
00108        hKey = RegOpenParent(pSection, hMasterKey, KEY_QUERY_VALUE);
00109        if (!hKey)
00110        {
00111               return(FALSE);
00112        }
00113 
00114        DWORD len = (DWORD)lenBuf;
00115 #ifndef WIN16
00116        BOOL retVal = (RegQueryValueEx(hKey, pKey, NULL, NULL, (LPBYTE)pBuf, &len) == ERROR_SUCCESS);
00117 #else
00118        BOOL retVal = (RegQueryValue(hKey, pKey, pBuf, (long far*)&len) == ERROR_SUCCESS);
00119 #endif
00120        RegCloseKey(hKey);
00121        return(retVal);
00122 }
00123 
00124 BOOL LOAD_DS GetConfigInfoNum(LPCSTR pSection, LPCSTR pKey, DWORD* pVal, HKEY hMasterKey)
00125 {
00126        HKEY hKey;
00127        hKey = RegOpenParent(pSection, hMasterKey, KEY_QUERY_VALUE);
00128        if (!hKey)
00129        {
00130               return(FALSE);
00131        }
00132 
00133        DWORD len = sizeof(DWORD);
00134 #ifndef WIN16
00135        BOOL retVal = (RegQueryValueEx(hKey, pKey, NULL, NULL, (LPBYTE)pVal, &len) == ERROR_SUCCESS);
00136 #else
00137        BOOL retVal = (RegQueryValue(hKey, pKey, (char far*)pVal, (long far*)&len) == ERROR_SUCCESS);
00138 #endif
00139        RegCloseKey(hKey);
00140        return(retVal);
00141 }
00142 
00143 BOOL LOAD_DS SetConfigInfoStr(LPCSTR pSection, LPCSTR pKey, LPSTR pStr, HKEY hMasterKey)
00144 {
00145        HKEY hKey;
00146        hKey = RegCreateParent(pSection, hMasterKey);
00147        if (!hKey)
00148        {
00149               return(FALSE);
00150        }
00151 #ifndef WIN16
00152        BOOL retVal = (RegSetValueEx(hKey, pKey, 0, REG_SZ, (LPBYTE)pStr, lstrlen(pStr) + 1) == ERROR_SUCCESS);
00153 #else
00154        BOOL retVal = (RegSetValue(hKey, pKey, REG_SZ, pStr, lstrlen(pStr) + 1) == ERROR_SUCCESS);
00155 #endif
00156        RegCloseKey(hKey);
00157        return(retVal);
00158 }
00159 
00160 
00161 BOOL LOAD_DS XP_GetInstallDirectory(LPCSTR pcurVersionSection, LPCSTR pInstallDirKey, LPSTR path, UINT nSize, HKEY hKey)
00162 {   
00163 #ifdef WIN32   
00164        if (!GetConfigInfoStr(pcurVersionSection, pInstallDirKey, path, nSize, hKey))
00165        {  
00166               return FALSE;
00167        }
00168        else
00169        {
00170               return TRUE;
00171        }       
00172 #else
00173        if ( 0 < GetPrivateProfileString(pcurVersionSection, pInstallDirKey,"ERROR", path, nSize, szNetscapeINI))
00174        {
00175               return TRUE;
00176        }
00177        else
00178        {
00179               return FALSE;
00180        }
00181 #endif
00182 }
00183 
00184 
00185 BOOL LOAD_DS XP_GetVersionInfoString(LPCSTR pNavigatorSection, LPCSTR pCurrentVersionKey, LPSTR pcurVersionStr, UINT nSize, HKEY hKey)
00186 { 
00187 #ifdef WIN32
00188        if (!GetConfigInfoStr(pNavigatorSection, pCurrentVersionKey, pcurVersionStr, nSize, HKEY_LOCAL_MACHINE))
00189        {
00190               return FALSE;
00191        }               
00192        else
00193        {
00194               return TRUE;
00195        }
00196 #else 
00197        if ( 0 < GetPrivateProfileString(pNavigatorSection, pCurrentVersionKey,"ERROR", pcurVersionStr, nSize, szNetscapeINI))
00198        {
00199               return TRUE;
00200        }
00201        else
00202        {
00203               return FALSE;
00204        }
00205 #endif
00206 
00207 }   
00208 
00209 DWORD LOAD_DS XP_GetInstallLocation(LPSTR pPath, UINT nSize)
00210 {           
00211 char curVersionStr[256]; 
00212 char curVersionSection[256];
00213 
00214   if (!pPath) 
00215     return  MAPI_E_LOGON_FAILURE;
00216   
00217 #ifdef WIN32
00218   if (!XP_GetVersionInfoString(szNavigatorSection, szCurrentVersionKey, curVersionStr, 
00219     sizeof(curVersionStr), HKEY_LOCAL_MACHINE))
00220   {
00221     return (MAPI_E_LOGON_FAILURE);
00222   }
00223   
00224   wsprintf(curVersionSection, szNavigatorCurVersionSection, curVersionStr);
00225               
00226   if (!XP_GetInstallDirectory(curVersionSection, szInstallDirKey, pPath, 
00227     nSize, HKEY_LOCAL_MACHINE))
00228   {
00229     return (MAPI_E_ACCESS_DENIED);
00230     
00231   }
00232   
00233   lstrcat(pPath, "\\");
00234   lstrcat(pPath, "Program\\netscape.exe");
00235   
00236   return SUCCESS_SUCCESS;
00237 #else
00238   if (32 == Is_16_OR_32_BIT_CommunitorRunning())
00239   {   
00240     if (!GetConfigInfoStr("snews\\shell\\open", "command", curVersionStr, sizeof(curVersionStr), HKEY_CLASSES_ROOT))
00241     {
00242       return (MAPI_E_ACCESS_DENIED);
00243     }
00244     else
00245     {  
00246       char *pFind = strstr(curVersionStr,"-h");
00247       
00248       if (pFind)
00249       {
00250         *pFind=0;
00251         lstrcpy(pPath,curVersionStr);
00252       }
00253       else
00254       {
00255         return (MAPI_E_ACCESS_DENIED);
00256       }
00257     }
00258     
00259     return SUCCESS_SUCCESS;
00260   }
00261   else //setup up to start navstart since we are a sixteen bit DLL.
00262   {
00263     if (!XP_GetVersionInfoString(szNavigatorSection, szCurrentVersionKey, curVersionStr, sizeof(curVersionStr), HKEY_LOCAL_MACHINE))
00264     {
00265       return (MAPI_E_LOGON_FAILURE);
00266     }
00267     
00268     wsprintf(curVersionSection, szNavigatorCurVersionSection, curVersionStr);
00269     
00270     if (!XP_GetInstallDirectory(curVersionSection, szInstallDirKey, pPath,nSize, HKEY_LOCAL_MACHINE))
00271     {
00272       return (MAPI_E_ACCESS_DENIED);             
00273     }
00274     
00275     lstrcat(pPath, "\\");
00276     lstrcat(pPath, "NAVSTART.EXE");
00277     return SUCCESS_SUCCESS; 
00278   }
00279 #endif
00280 }
00281 
00282 int LOAD_DS Is_16_OR_32_BIT_CommunitorRunning()
00283 {
00284   if (FindWindow("AfxFrameOrView", NULL) && !FindWindow("aHiddenFrameClass", NULL))
00285               return(16);
00286        else if (FindWindow("aHiddenFrameClass", NULL))
00287               return(32);
00288        else 
00289               return 0;
00290 }
00291 
00292 // size of buffer to use for copying files.
00293 #define COPYBUFSIZE 1024
00294 
00295 #ifdef WIN16
00296      
00297 BOOL Win16CopyFile(LPCSTR a_Src, LPCSTR a_Dest, BOOL a_bOverwrite)
00298 {
00299        OFSTRUCT ofSrc, ofDest;
00300        HFILE hSrc, hDest;
00301        BOOL bResult;
00302        
00303        ofDest.cBytes = ofSrc.cBytes = sizeof(OFSTRUCT);
00304        hDest = OpenFile(a_Dest, &ofDest, OF_EXIST);
00305        if (hDest != HFILE_ERROR && !a_bOverwrite)
00306               bResult = FALSE;     // file exists but caller doesn't want file overwritten
00307        else { // file either doesn't exist, or caller wants it overwritten.
00308               hSrc = OpenFile(a_Src, &ofSrc, OF_READ);
00309               hDest = OpenFile(a_Dest, &ofDest, OF_WRITE | OF_CREATE);
00310               if (hSrc != HFILE_ERROR && hDest != HFILE_ERROR) {
00311                      unsigned char buf[COPYBUFSIZE];
00312                      UINT bufsize =  COPYBUFSIZE;
00313                      UINT bytesread;
00314                      
00315                      bResult = TRUE;                    
00316                      while (0 != (bytesread = _lread(hSrc, (LPVOID)buf, bufsize))) {
00317                             if ((bytesread == HFILE_ERROR) ||  // check for read error...
00318                                                                              // and write error
00319                                    (bytesread != _lwrite(hDest, (LPVOID)buf, bytesread))) {
00320                                    bResult = FALSE; // could be out of diskspace
00321                                    break;
00322                             }
00323                      }
00324               }
00325               else
00326                      bResult = FALSE;
00327 
00328               if (hSrc != HFILE_ERROR)
00329                      _lclose(hSrc);
00330               if (hDest != HFILE_ERROR)
00331                      _lclose(hDest);
00332        }
00333                      
00334        return bResult;
00335 }
00336 
00337 #endif  // WIN16
00338 
00339 BOOL LOAD_DS 
00340 XP_CopyFile(LPCSTR lpExistingFile, LPCSTR lpNewFile, BOOL bFailifExist)
00341 {                                                  
00342 #ifdef WIN32
00343   return      CopyFile(lpExistingFile, lpNewFile, bFailifExist);
00344 #else                                                             
00345   return    Win16CopyFile(lpExistingFile, lpNewFile, TRUE);
00346 #endif
00347 }