Ignore:
Timestamp:
Sep 13, 2000, 12:47:58 AM (25 years ago)
Author:
bird
Message:

Documented and enhanced GetModuleFileNameA/W.
LoadLibraryExA uses matchModName to compare the library filename with
the executable (WinExe) module name.

File:
1 edited

Legend:

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

    r4237 r4250  
    1 /* $Id: wprocess.cpp,v 1.92 2000-09-12 04:32:36 bird Exp $ */
     1/* $Id: wprocess.cpp,v 1.93 2000-09-12 22:47:58 bird Exp $ */
    22
    33/*
     
    7474//TODO: This should not be here: (need to rearrange NTDLL; kernel32 can't depend on ntdll)
    7575BOOLEAN (* WINAPI RtlAllocateAndInitializeSid) ( PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
    76                                                  BYTE nSubAuthorityCount,
    77                                                  DWORD nSubAuthority0,
    78                                                  DWORD nSubAuthority1,
    79                                                  DWORD nSubAuthority2,
    80                                                  DWORD nSubAuthority3,
    81                                                  DWORD nSubAuthority4,
    82                                                  DWORD nSubAuthority5,
    83                                                  DWORD nSubAuthority6,
    84                                                  DWORD nSubAuthority7,
    85                                                  PSID *pSid);
     76                                                 BYTE nSubAuthorityCount,
     77                                                 DWORD nSubAuthority0,
     78                                                 DWORD nSubAuthority1,
     79                                                 DWORD nSubAuthority2,
     80                                                 DWORD nSubAuthority3,
     81                                                 DWORD nSubAuthority4,
     82                                                 DWORD nSubAuthority5,
     83                                                 DWORD nSubAuthority6,
     84                                                 DWORD nSubAuthority7,
     85                                                 PSID *pSid);
    8686static HINSTANCE hInstNTDll = 0;
    8787
     
    9191{
    9292    if(TIBFlatPtr == NULL) {
    93         DebugInt3();
     93        DebugInt3();
    9494        return 0;
    9595    }
     
    231231    //Initialize thread security objects (TODO: Not complete)
    232232    if(hInstNTDll == 0) {
    233         hInstNTDll = LoadLibraryA("NTDLL.DLL");
    234         *(ULONG *)&RtlAllocateAndInitializeSid = (ULONG)GetProcAddress(hInstNTDll, "RtlAllocateAndInitializeSid");
    235         if(RtlAllocateAndInitializeSid == NULL) {
    236                 DebugInt3();
    237         }
     233        hInstNTDll = LoadLibraryA("NTDLL.DLL");
     234        *(ULONG *)&RtlAllocateAndInitializeSid = (ULONG)GetProcAddress(hInstNTDll, "RtlAllocateAndInitializeSid");
     235        if(RtlAllocateAndInitializeSid == NULL) {
     236                DebugInt3();
     237        }
    238238    }
    239239    SID_IDENTIFIER_AUTHORITY sidIdAuth = {0};
     
    445445    //SvL: Ignore FreeLibary for executable
    446446    if(WinExe && hinstance == WinExe->getInstanceHandle()) {
    447         return TRUE;
     447        return TRUE;
    448448    }
    449449
     
    700700                 lpszLibFile, hFile, dwFlags));
    701701        SetLastError(ERROR_INVALID_PARAMETER);
    702         return NULL;
     702        return NULL;
    703703    }
    704704
    705705
    706706    /** @sketch
    707      *  First we'll see if the module is allready loaded.
     707     *  First we'll see if the module is allready loaded - either as the EXE or as DLL.
     708     *  IF Executable present AND libfile matches the modname of the executable THEN
     709     *      RETURN instance handle of executable.
     710     *  Endif
    708711     *  IF allready loaded THEN
    709712     *      IF it's a LX dll which isn't loaded and we're using the PeLoader THEN
     
    715718     *  Endif
    716719     */
    717     if(WinExe) {
    718       char  szDllName[CCHMAXPATH];
    719       char *dot;
    720 
    721         strcpy(szDllName, OSLibStripPath((char *)lpszLibFile));
    722         strupr(szDllName);
    723         dot = strstr(szDllName, ".");
    724         if(dot)
    725             *dot = 0;
    726 
    727         if(!strcmp(szDllName, WinExe->getModuleName())) {
    728                 return WinExe->getInstanceHandle();
    729         }
    730     }
     720    if (WinExe != NULL && WinExe->matchModName(lpszLibFile))
     721        return WinExe->getInstanceHandle();
    731722    pModule = Win32DllBase::findModule((LPSTR)lpszLibFile);
    732723    if (pModule)
     
    790781        if (hDll)
    791782        {
    792             /* OS/2 dll, system dll, converted dll or win32k took care of it.*/
     783            /* OS/2 dll, system dll, converted dll or win32k took care of it. */
    793784            pModule = (Win32DllBase *)Win32LxDll::findModuleByOS2Handle(hDll);
    794785            if(pModule)
     
    797788                if(pModule->isLxDll())
    798789                {
    799                         ((Win32LxDll *)pModule)->setDllHandleOS2(hDll);
    800                         if(fPeLoader) {
    801                                 pModule->AddRef();
    802                         }
     790                    ((Win32LxDll *)pModule)->setDllHandleOS2(hDll);
     791                    if (fPeLoader)
     792                        pModule->AddRef();
    803793                }
    804794                pModule->incDynamicLib();
    805795            }
    806             else {
     796            else
    807797                return hDll; //happens when LoadLibrary is called in kernel32's initterm (nor harmful)
    808             }
    809798            dprintf(("KERNEL32: LoadLibraryExA(%s, 0x%x, 0x%x): returns 0x%x. Loaded %s using O32_LoadLibrary.",
    810799                     lpszLibFile, hFile, dwFlags, hDll, szModname));
     
    895884        {
    896885            peldrDll->AddRef();
    897             if (peldrDll->attachProcess()) {
     886            if (peldrDll->attachProcess())
     887            {
    898888                hDll = peldrDll->getInstanceHandle();
    899                 //Must be called *after* attachprocess, since attachprocess may also
     889                //Must be called *after* attachprocess, since attachprocess may also
    900890                //trigger LoadLibrary calls
    901891                //Those dlls must not be put in front of this dll in the dynamic
     
    10581048
    10591049/**
    1060  * Internal function which gets the commandline.
     1050 * Internal function which gets the commandline (string) used to start the current process.
    10611051 * @returns     OS/2 / Windows return code
    10621052 *              On successful return (NO_ERROR) the global variables
     
    13751365
    13761366
    1377 DWORD WIN32API GetModuleFileNameA(HMODULE hinstModule, LPTSTR lpszPath, DWORD cchPath)
    1378 {
    1379  DWORD rc;
    1380  Win32ImageBase *module;
    1381  char *fpath = NULL;
    1382 
    1383   dprintf(("GetModuleFileName %X", hinstModule));
    1384   if(hinstModule == 0 || hinstModule == -1 || (WinExe && hinstModule == WinExe->getInstanceHandle())) {
    1385         module = (Win32ImageBase *)WinExe;
    1386   }
    1387   else {
    1388         module = (Win32ImageBase *)Win32DllBase::findModule(hinstModule);
    1389   }
    1390 
    1391   if(module) {
    1392         fpath = module->getFullPath();
    1393   }
    1394   if(fpath) {
    1395         //SvL: 13-9-98: +1
    1396         rc = min(strlen(fpath)+1, cchPath);
    1397         strncpy(lpszPath, fpath, rc);
    1398         lpszPath[rc-1] = 0;
    1399   }
    1400   //only needed for call inside kernel32's initterm (profile init)
    1401   else  rc = O32_GetModuleFileName(hinstModule, lpszPath, cchPath);
    1402 
    1403   if(rc) {
    1404         dprintf(("KERNEL32: GetModuleFileName %s %d\n", lpszPath, hinstModule));
    1405   }
    1406   else  dprintf(("KERNEL32: WARNING: GetModuleFileName %x not found!", hinstModule));
    1407   return(rc);
    1408 }
    1409 //******************************************************************************
    1410 //******************************************************************************
    1411 DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpFileName, DWORD nSize)
    1412 {
    1413  char *asciifilename = (char *)malloc(nSize+1);
    1414  DWORD rc;
    1415 
    1416     dprintf(("KERNEL32:  OSLibGetModuleFileNameW\n"));
    1417     rc = GetModuleFileNameA(hModule, asciifilename, nSize);
    1418     if(rc)      AsciiToUnicode(asciifilename, lpFileName);
    1419     free(asciifilename);
    1420     return(rc);
    1421 }
     1367/**
     1368 * GetModuleFileName gets the full path and file name for the specified module.
     1369 * @returns     Bytes written to the buffer (lpszPath). This count includes the
     1370 *              terminating '\0'.
     1371 *              On error 0 is returned. Last error is set.
     1372 * @param       hModule     Handle to the module you like to get the file name to.
     1373 * @param       lpszPath    Output buffer for full path and file name.
     1374 * @param       cchPath     Size of the lpszPath buffer.
     1375 * @sketch      Validate lpszPath.
     1376 *              Find the module object using handle.
     1377 *              If found Then
     1378 *                  Get full path from module object.
     1379 *                  If found path Then
     1380 *                      Copy path to buffer and set the number of bytes written.
     1381 *                  Else
     1382 *                      IPE!
     1383 *              Else
     1384 *                  Call Open32 GetModuleFileName. (kernel32 initterm needs/needed this)
     1385 *              Log result.
     1386 *              Return number of bytes written to the buffer.
     1387 *
     1388 * @status      Completely implemented, Open32.
     1389 * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no)
     1390 *              Sander van Leeuwen (sandervl@xs4all.nl)
     1391 * @remark      - Do we still have to call Open32?
     1392 *              - Do we set ERROR_BUFFER_OVERFLOW when cch > cchPath?
     1393 *              - Does NT really set the last error?
     1394 */
     1395DWORD WIN32API GetModuleFileNameA(HMODULE hModule, LPTSTR lpszPath, DWORD cchPath)
     1396{
     1397    Win32ImageBase *    pMod;           /* Pointer to the module object. */
     1398    DWORD               cch;            /* Length of the  */
     1399
     1400    if (!VALID_PSZ(lpszPath))
     1401    {
     1402        dprintf(("KERNEL32:  GetModuleFileNameA(0x%x, 0x%x, 0x%x): invalid pointer lpszLibFile = 0x%x\n",
     1403                 hModule, lpszPath, cchPath, lpszPath));
     1404        SetLastError(ERROR_INVALID_PARAMETER); //or maybe ERROR_ACCESS_DENIED is more appropriate?
     1405        return 0;
     1406    }
     1407
     1408    pMod = Win32ImageBase::findModule(hModule);
     1409    if (pMod != NULL)
     1410    {
     1411        const char *pszFn = pMod->getFullPath();
     1412        if (pszFn)
     1413        {
     1414            cch = strlen(pszFn) + 1;
     1415            if (cch > cchPath)
     1416                cch = cchPath;
     1417            memcpy(lpszPath, pszFn, cch);
     1418            lpszPath[cch - 1] = '\0';
     1419        }
     1420        else
     1421        {
     1422            dprintf(("KERNEL32:  GetModuleFileNameA(%x,...): IPE - getFullPath returned NULL or empty string\n"));
     1423            DebugInt3();
     1424            SetLastError(ERROR_INVALID_HANDLE);
     1425        }
     1426    }
     1427    else
     1428    {
     1429        SetLastError(ERROR_INVALID_HANDLE);
     1430        //only needed for call inside kernel32's initterm (profile init)
     1431        //(console init only it seems...)
     1432        cch = O32_GetModuleFileName(hModule, lpszPath, cchPath);
     1433        if (cch > 0)    cch++;          /* Open32 doesn't count the terminator. */
     1434    }
     1435
     1436    if (cch > 0)
     1437        dprintf(("KERNEL32:  GetModuleFileNameA(%x,...): %s %d\n", lpszPath, hModule, cch));
     1438    else
     1439        dprintf(("KERNEL32:  WARNING: GetModuleFileNameA(%x,...) - not found!", hModule));
     1440
     1441    return cch;
     1442}
     1443
     1444
     1445/**
     1446 * GetModuleFileName gets the full path and file name for the specified module.
     1447 * @returns     Bytes written to the buffer (lpszPath). This count includes the
     1448 *              terminating '\0'.
     1449 *              On error 0 is returned. Last error is set.
     1450 * @param       hModule     Handle to the module you like to get the file name to.
     1451 * @param       lpszPath    Output buffer for full path and file name.
     1452 * @param       cchPath     Size of the lpszPath buffer.
     1453 * @sketch      Find the module object using handle.
     1454 *              If found Then
     1455 *                  get full path from module object.
     1456 *                  If found path Then
     1457 *                      Determin path length.
     1458 *                      Translate the path to into the buffer.
     1459 *                  Else
     1460 *                      IPE.
     1461 *              else
     1462 *                  SetLastError to invalid handle.
     1463 *              Log result.
     1464 *              return number of bytes written to the buffer.
     1465 *
     1466 * @status      Completely implemented.
     1467 * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no)
     1468 * @remark      - We do _NOT_ call Open32.
     1469 *              - Do we set ERROR_BUFFER_OVERFLOW when cch > cchPath?
     1470 *              - Does NT really set the last error?
     1471 */
     1472DWORD WIN32API GetModuleFileNameW(HMODULE hModule, LPWSTR lpszPath, DWORD cchPath)
     1473{
     1474    Win32ImageBase *    pMod;
     1475    DWORD               cch = 0;
     1476
     1477    if (!VALID_PSZ(lpszPath))
     1478    {
     1479        dprintf(("KERNEL32:  GetModuleFileNameW(0x%x, 0x%x, 0x%x): invalid pointer lpszLibFile = 0x%x\n",
     1480                 hModule, lpszPath, cchPath, lpszPath));
     1481        SetLastError(ERROR_INVALID_PARAMETER); //or maybe ERROR_ACCESS_DENIED is more appropriate?
     1482        return 0;
     1483    }
     1484
     1485    pMod = Win32ImageBase::findModule(hModule);
     1486    if (pMod != NULL)
     1487    {
     1488        const char *pszFn = pMod->getFullPath();
     1489        if (pszFn || *pszFn != '\0')
     1490        {
     1491            cch = strlen(pszFn) + 1;
     1492            if (cch > cchPath)
     1493                cch = cchPath;
     1494            AsciiToUnicodeN(pszFn, lpszPath, cch);
     1495        }
     1496        else
     1497        {
     1498            dprintf(("KERNEL32:  GetModuleFileNameW(%x,...): IPE - getFullPath returned NULL or empty string\n"));
     1499            DebugInt3();
     1500            SetLastError(ERROR_INVALID_HANDLE);
     1501        }
     1502    }
     1503    else
     1504        SetLastError(ERROR_INVALID_HANDLE);
     1505
     1506    if (cch > 0)
     1507        dprintf(("KERNEL32:  GetModuleFileNameW(%x,...): %s %d\n", lpszPath, hModule, cch));
     1508    else
     1509        dprintf(("KERNEL32:  WARNING: GetModuleFileNameW(%x,...) - not found!", hModule));
     1510
     1511    return cch;
     1512}
     1513
     1514
    14221515//******************************************************************************
    14231516//NOTE: GetModuleHandleA does NOT support files with multiple dots (i.e.
     
    16481741        ulAPIOrdinal = (ULONG)lpszProc;
    16491742        if (ulAPIOrdinal <= 0x0000FFFF) {
    1650                 proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
     1743                proc = (FARPROC)winmod->getApi((int)ulAPIOrdinal);
    16511744        }
    16521745        else    proc = (FARPROC)winmod->getApi((char *)lpszProc);
    1653         if(proc == 0) {
     1746        if(proc == 0) {
    16541747#ifdef DEBUG
    1655                 if(ulAPIOrdinal <= 0x0000FFFF) {
    1656                         dprintf(("GetProcAddress %x %x not found!", hModule, ulAPIOrdinal));
    1657                 }
    1658                 else    dprintf(("GetProcAddress %x %s not found!", hModule, lpszProc));
     1748                if(ulAPIOrdinal <= 0x0000FFFF) {
     1749                        dprintf(("GetProcAddress %x %x not found!", hModule, ulAPIOrdinal));
     1750                }
     1751                else    dprintf(("GetProcAddress %x %s not found!", hModule, lpszProc));
    16591752#endif
    1660                 SetLastError(ERROR_PROC_NOT_FOUND);
    1661         }
    1662         if(HIWORD(lpszProc))
    1663                 dprintf(("KERNEL32:  GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
    1664         else    dprintf(("KERNEL32:  GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
    1665         return proc;
     1753                SetLastError(ERROR_PROC_NOT_FOUND);
     1754        }
     1755        if(HIWORD(lpszProc))
     1756                dprintf(("KERNEL32:  GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
     1757        else    dprintf(("KERNEL32:  GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
     1758        return proc;
    16661759  }
    16671760  proc = O32_GetProcAddress(hModule, lpszProc);
    16681761  if(HIWORD(lpszProc))
    1669         dprintf(("KERNEL32:  GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
     1762        dprintf(("KERNEL32:  GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
    16701763  else  dprintf(("KERNEL32:  GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
    16711764  return(proc);
Note: See TracChangeset for help on using the changeset viewer.