Ignore:
Timestamp:
Sep 16, 1999, 1:39:08 AM (26 years ago)
Author:
sandervl
Message:

Rewrite for new win32 image classes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/wprocess.cpp

    r712 r956  
    1 /* $Id: wprocess.cpp,v 1.30 1999-08-27 16:51:01 sandervl Exp $ */
     1/* $Id: wprocess.cpp,v 1.31 1999-09-15 23:38:03 sandervl Exp $ */
    22
    33/*
     
    1515#include <string.h>
    1616
    17 #include "unicode.h"
    18 #include "windll.h"
    19 #include "winexe.h"
     17#include <unicode.h>
     18#include <windllbase.h>
     19#include <winexebase.h>
     20#include <windllpeldr.h>
    2021
    2122#ifdef __IBMCPP__
     
    3233#include "mmap.h"
    3334
    34 BOOL      fExeStarted = FALSE;
    3535BOOL      fFreeLibrary = FALSE;
    3636
     
    199199   return GetFS();
    200200}
    201 /******************************************************************************/
    202 //******************************************************************************
    203 void WIN32API RegisterExe(WIN32EXEENTRY EntryPoint, PIMAGE_TLS_CALLBACK *TlsCallbackAddr,
    204                           LPDWORD TlsIndexAddr, ULONG TlsInitSize,
    205                           ULONG TlsTotalSize, LPVOID TlsAddress,
    206                           LONG Win32TableId, LONG NameTableId, LONG VersionResId,
    207                          LONG Pe2lxVersion, HINSTANCE hinstance, ULONG dwReserved)
    208 {
    209   if(WinExe != NULL) //should never happen
    210         delete(WinExe);
    211 
    212   CheckVersion(Pe2lxVersion, OSLibGetDllName(hinstance));
    213 
    214   if(getenv("WIN32_IOPL2")) {
    215     io_init1();
    216   }
    217 
    218   Win32Exe *winexe;
    219 
    220   winexe = new Win32Exe(hinstance, NameTableId, Win32TableId);
    221 
    222   if(winexe) {
    223         dprintf(("RegisterExe Win32TableId = %x", Win32TableId));
    224         dprintf(("RegisterExe NameTableId  = %x", NameTableId));
    225         dprintf(("RegisterExe VersionResId = %x", VersionResId));
    226         dprintf(("RegisterExe Pe2lxVersion = %x", Pe2lxVersion));
    227 
    228         winexe->setVersionId(VersionResId);
    229         winexe->setEntryPoint((ULONG)EntryPoint);
    230         winexe->setTLSAddress(TlsAddress);
    231         winexe->setTLSInitSize(TlsInitSize);
    232         winexe->setTLSTotalSize(TlsTotalSize);
    233         winexe->setTLSIndexAddr(TlsIndexAddr);
    234         winexe->setTLSCallBackAddr(TlsCallbackAddr);
    235 
    236         winexe->start();
    237   }
    238   else {
    239         eprintf(("Win32Exe creation failed!\n"));
    240         DebugInt3();
    241         return;
    242   }
    243 }
    244 //******************************************************************************
    245 //******************************************************************************
    246 ULONG WIN32API RegisterDll(WIN32DLLENTRY pfnDllEntry, PIMAGE_TLS_CALLBACK *TlsCallbackAddr,
    247                            LPDWORD TlsIndexAddr, ULONG TlsInitSize,
    248                            ULONG TlsTotalSize, LPVOID TlsAddress,
    249                            LONG Win32TableId, LONG NameTableId, LONG VersionResId,
    250                            LONG Pe2lxVersion, HINSTANCE hinstance, ULONG dwAttachType)
    251 {
    252  char *name;
    253 
    254   Win32Dll *winmod = Win32Dll::findModule(hinstance);
    255   if(dwAttachType == 0)
    256   { //Process attach
    257         if(getenv("WIN32_IOPL2")) {
    258                 io_init1();
    259         }
    260         name = OSLibGetDllName(hinstance);
    261         CheckVersion(Pe2lxVersion, name);
    262 
    263         dprintf(("RegisterDll %X %s reason %d\n", hinstance, name, dwAttachType));
    264         dprintf(("RegisterDll Win32TableId = %x", Win32TableId));
    265         dprintf(("RegisterDll NameTableId  = %x", NameTableId));
    266         dprintf(("RegisterDll VersionResId = %x", VersionResId));
    267         dprintf(("RegisterDll Pe2lxVersion = %x", Pe2lxVersion));
    268 
    269         if(winmod != NULL) {
    270                 //dll manually loaded by PE loader (Win32Dll::init)
    271                 winmod->OS2DllInit(hinstance, NameTableId, Win32TableId, pfnDllEntry);
    272         }
    273         else {
    274                 //converted win32 dll loaded by OS/2 loader
    275                 winmod = new Win32Dll(hinstance, NameTableId, Win32TableId, pfnDllEntry);
    276                 if(winmod == NULL) {
    277                         eprintf(("Failed to allocate module object!\n"));
    278                         DebugInt3();
    279                         return 0;                    //fail dll load
    280                 }
    281         }
    282         winmod->setTLSAddress(TlsAddress);
    283         winmod->setTLSInitSize(TlsInitSize);
    284         winmod->setTLSTotalSize(TlsTotalSize);
    285         winmod->setTLSIndexAddr(TlsIndexAddr);
    286         winmod->setTLSCallBackAddr(TlsCallbackAddr);
    287 
    288         /* @@@PH 1998/03/17 console devices initialization */
    289         iConsoleDevicesRegister();
    290 
    291         //SvL: 19-8-'98
    292         winmod->AddRef();
    293         winmod->setVersionId(VersionResId);
    294 
    295         winmod->attachProcess();
    296    }
    297    else {//process detach
    298         if(winmod != NULL && !fFreeLibrary) {
    299                 return 0;   //don't unload (OS/2 dll unload bug)
    300         }
    301         //Runtime environment could already be gone, so don't do this
    302         // dprintf(("KERNEL32: Dll Removed by FreeLibrary or ExitProcess\n"));
    303    }
    304    return 1;   //success
    305 }
    306201//******************************************************************************
    307202//******************************************************************************
     
    346241BOOL WIN32API FreeLibrary(HINSTANCE hinstance)
    347242{
    348  Win32Dll *winmod;
     243 Win32DllBase *winmod;
    349244 BOOL rc;
    350245
    351246  dprintf(("FreeLibrary"));
    352   winmod = Win32Dll::findModule(hinstance);
     247  winmod = Win32DllBase::findModule(hinstance);
    353248  if(winmod) {
    354     winmod->Release();
    355     return(TRUE);
     249        winmod->Release();
     250        return(TRUE);
    356251  }
    357252  dprintf(("KERNEL32: FreeLibrary %s %X\n", OSLibGetDllName(hinstance), hinstance));
     
    366261/******************************************************************************/
    367262/******************************************************************************/
    368 static HINSTANCE iLoadLibraryA(LPCTSTR lpszLibFile)
     263static HINSTANCE iLoadLibraryA(LPCTSTR lpszLibFile, DWORD dwFlags)
    369264{
    370265 char        modname[CCHMAXPATH];
    371266 HINSTANCE   hDll;
    372  Win32Dll  *module;
     267 Win32DllBase *module;
    373268
    374269  hDll = O32_LoadLibrary(lpszLibFile);
     
    388283  }
    389284
    390   if(Win32Image::isPEImage((char *)modname)) {
    391     module = Win32Dll::findModule((char *)modname);
    392     if(module) {//don't load it again
    393         module->AddRef();
    394         return module->getInstanceHandle();
    395     }
    396 
    397     module = new Win32Dll((char *)modname);
    398     if(module == NULL)
    399         return(0);
    400 
    401     module->init(0);
    402     if(module->getError() != NO_ERROR) {
    403         dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
    404         delete(module);
    405         return(0);
    406     }
    407     if(module->attachProcess() == FALSE) {
    408         dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
    409         delete(module);
    410         return(0);
    411     }
    412     module->AddRef();
    413     return module->getInstanceHandle();
    414   }
    415   else
    416     return(0);
    417 }
    418 
    419 
     285  if(Win32ImageBase::isPEImage((char *)modname))
     286  {
     287        module = Win32DllBase::findModule((char *)modname);
     288        if(module) {//don't load it again
     289                module->AddRef();
     290                return module->getInstanceHandle();
     291        }
     292
     293        Win32PeLdrDll *peldrDll = new Win32PeLdrDll((char *)modname);
     294        if(peldrDll == NULL)
     295                return(0);
     296
     297        peldrDll->init(0);
     298        if(peldrDll->getError() != NO_ERROR) {
     299                dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
     300                delete(peldrDll);
     301                return(0);
     302        }
     303        if(dwFlags & DONT_RESOLVE_DLL_REFERENCES) {
     304                peldrDll->setNoEntryCalls();
     305        }
     306
     307        if(peldrDll->attachProcess() == FALSE) {
     308                dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
     309                delete(peldrDll);
     310                return(0);
     311        }
     312        peldrDll->AddRef();
     313        return peldrDll->getInstanceHandle();
     314  }
     315  else  return(0);
     316}
     317//******************************************************************************
     318//******************************************************************************
    420319HINSTANCE WIN32API LoadLibraryA(LPCTSTR lpszLibFile)
    421320{
     
    425324           lpszLibFile));
    426325
    427   hDll = iLoadLibraryA(lpszLibFile);
     326  hDll = iLoadLibraryA(lpszLibFile, 0);
    428327  if (hDll == 0)
    429328  {
     
    438337
    439338      // now try again without fully qualified path
    440       hDll = iLoadLibraryA(pszName);
     339      hDll = iLoadLibraryA(pszName, 0);
    441340    }
    442341  }
     
    448347HINSTANCE WIN32API LoadLibraryExA(LPCTSTR lpszLibFile, HANDLE hFile, DWORD dwFlags)
    449348{
    450  Win32Dll   *module;
    451  HINSTANCE   hDll;
     349 HINSTANCE     hDll;
    452350
    453351  dprintf(("KERNEL32:  LoadLibraryExA %s (%X)\n", lpszLibFile, dwFlags));
    454   hDll = O32_LoadLibrary(lpszLibFile);
    455   if(hDll) {
    456         return hDll;    //converted dll or win32k took care of it
    457   }
    458 
    459   if(Win32Image::isPEImage((char *)lpszLibFile)) {
    460     module = Win32Dll::findModule((char *)lpszLibFile);
    461     if(module) {//don't load it again
    462         module->AddRef();
    463         return module->getInstanceHandle();
     352  hDll = iLoadLibraryA(lpszLibFile, dwFlags);
     353  if (hDll == 0)
     354  {
     355    char * pszName;
     356
     357    // remove path from the image name
     358    pszName = strrchr((char *)lpszLibFile,
     359                      '\\');
     360    if (pszName != NULL)
     361    {
     362      pszName++;                // skip backslash
     363
     364      // now try again without fully qualified path
     365      hDll = iLoadLibraryA(pszName, dwFlags);
    464366    }
    465 
    466     module = new Win32Dll((char *)lpszLibFile);
    467 
    468     if(module == NULL)
    469         return(0);
    470 
    471     module->init(0);
    472     if(module->getError() != NO_ERROR) {
    473         dprintf(("LoadLibary %s failed (::init)\n", lpszLibFile));
    474         delete(module);
    475         return(0);
    476     }
    477     if(dwFlags & DONT_RESOLVE_DLL_REFERENCES) {
    478         module->setNoEntryCalls();
    479     }
    480     if(module->attachProcess() == FALSE) {
    481         dprintf(("LoadLibary %s failed (::attachProcess)\n", lpszLibFile));
    482         delete(module);
    483         return(0);
    484     }
    485     module->AddRef();
    486     return module->getInstanceHandle();
    487   }
    488   return(0);
     367  }
     368
     369  return hDll;
    489370}
    490371//******************************************************************************
     
    562443{
    563444 DWORD rc;
    564  Win32Image *module;
     445 Win32ImageBase *module;
    565446 char *fpath = NULL;
    566447
    567448  dprintf(("GetModuleFileName %X", hinstModule));
    568449  if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getInstanceHandle())) {
    569         module = (Win32Image *)WinExe;
     450        module = (Win32ImageBase *)WinExe;
    570451  }
    571452  else {
    572         module = (Win32Image *)Win32Dll::findModule(hinstModule);
     453        module = (Win32ImageBase *)Win32DllBase::findModule(hinstModule);
    573454  }
    574455
     
    608489{
    609490 HANDLE    hMod;
    610  Win32Dll *windll;
     491 Win32DllBase *windll;
    611492 char      szModule[CCHMAXPATH];
    612493 BOOL      fDllModule = FALSE;
     
    636517        }
    637518        else {
    638                 windll = Win32Dll::findModule(szModule);
     519                windll = Win32DllBase::findModule(szModule);
    639520                if(windll) {
    640521                        hMod = windll->getInstanceHandle();
     
    722603FARPROC WIN32API GetProcAddress(HMODULE hModule, LPCSTR lpszProc)
    723604{
    724  Win32Dll *winmod;
     605 Win32DllBase *winmod;
    725606 FARPROC   proc;
    726607 ULONG     ulAPIOrdinal;
    727608
    728   winmod = Win32Dll::findModule((HINSTANCE)hModule);
     609  winmod = Win32DllBase::findModule((HINSTANCE)hModule);
    729610  if(winmod) {
    730     ulAPIOrdinal = (ULONG)lpszProc;
    731     if (ulAPIOrdinal <= 0x0000FFFF) {
    732         return (FARPROC)winmod->getApi((int)ulAPIOrdinal);
    733     }
    734     else    return (FARPROC)winmod->getApi((char *)lpszProc);
     611        ulAPIOrdinal = (ULONG)lpszProc;
     612        if (ulAPIOrdinal <= 0x0000FFFF) {
     613                proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
     614        }
     615        else    proc = (FARPROC)winmod->getApi((char *)lpszProc);
     616        if(proc == 0) {
     617                SetLastError(ERROR_PROC_NOT_FOUND);
     618        }
     619        return proc;
    735620  }
    736621  proc = O32_GetProcAddress(hModule, lpszProc);
     
    743628BOOL SYSTEM GetVersionStruct(char *modname, char *verstruct, ULONG bufLength)
    744629{
    745  HINSTANCE   hinstance;
    746  Win32Image *winimage;
     630 HINSTANCE       hinstance;
     631 Win32ImageBase *winimage;
    747632
    748633  dprintf(("GetVersionStruct"));
     
    753638  }
    754639  if(WinExe && WinExe->getInstanceHandle() == hinstance) {
    755         winimage = (Win32Image *)WinExe;
     640        winimage = (Win32ImageBase *)WinExe;
    756641  }
    757642  else {
    758         winimage = (Win32Image *)Win32Dll::findModule(hinstance);
     643        winimage = (Win32ImageBase *)Win32DllBase::findModule(hinstance);
    759644        if(winimage == NULL) {
    760645                dprintf(("GetVersionStruct can't find Win32Image for %s\n", modname));
     
    762647        }
    763648  }
    764   if(winimage->getVersionId() == -1) {
    765     dprintf(("GetVersionStruct: %s has no version resource!\n", modname));
    766     return(FALSE);
    767   }
    768   return OSLibGetResource(hinstance, winimage->getVersionId(), verstruct, bufLength);
     649  return winimage->getVersionStruct(verstruct, bufLength);
    769650}
    770651//******************************************************************************
     
    772653ULONG SYSTEM GetVersionSize(char *modname)
    773654{
    774  HINSTANCE   hinstance;
    775  Win32Image *winimage;
     655 HINSTANCE       hinstance;
     656 Win32ImageBase *winimage;
    776657
    777658  dprintf(("GetVersionSize of %s\n", modname));
    778659  hinstance = OSLibQueryModuleHandle(modname);
    779660  if(hinstance == 0) {
    780     dprintf(("GetVersionSize can't find handle for %s\n", modname));
    781     return(FALSE);
     661        dprintf(("GetVersionSize can't find handle for %s\n", modname));
     662        return(FALSE);
    782663  }
    783664
    784665  if(WinExe && WinExe->getInstanceHandle() == hinstance) {
    785         winimage = (Win32Image *)WinExe;
     666        winimage = (Win32ImageBase *)WinExe;
    786667  }
    787668  else {
    788         winimage = (Win32Image *)Win32Dll::findModule(hinstance);
     669        winimage = (Win32ImageBase *)Win32DllBase::findModule(hinstance);
    789670        if(winimage == NULL) {
    790671                dprintf(("GetVersionSize can't find Win32Image for %s\n", modname));
     
    792673        }
    793674  }
    794   if(winimage->getVersionId() == -1) {
    795         dprintf(("GetVersionSize: %s has no version resource!\n", modname));
    796         return(FALSE);
    797   }
    798   ULONG size = OSLibGetResourceSize(hinstance, winimage->getVersionId());
    799 
    800   dprintf(("Version resource size = %d, id %d\n", size, winimage->getVersionId()));
    801   return(size);
     675  return winimage->getVersionSize();
    802676}
    803677//******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.