我现在只有一个hinstance指针,有没有办法获取它的详细信息,比如文件名

解决方案 »

  1.   

    use shell function:
    Implemented by many of the Windows shell DLLs to allow applications to obtain DLL-specific version information.HRESULT CALLBACK DllGetVersion(
        DLLVERSIONINFO *pdvi
    );
    //sample code
    BOOL CDLLVersion::GetDLLVersion (LPSTR szDLLFileName, 
                                     DWORD &dwMajor, DWORD &dwMinor, DWORD &dwBuildNumber)
    {
    HINSTANCE   hDllInst;           // Instance of loaded DLL
    char szFileName [_MAX_PATH];    // Temp file nameBOOL bRes = TRUE;               // Result
        lstrcpy (szFileName, szDLLFileName);    // Save a file name copy for the loading
        

        hDllInst = LoadLibrary(TEXT(szFileName));   //load the DLL
        if(hDllInst)   // Could successfully load the DLL
        {
            DLLGETVERSIONPROC pDllGetVersion;
            /*
            You must get this function explicitly because earlier versions of the DLL 
            don't implement this function. That makes the lack of implementation of the 
            function a version er in itself.
            */
            pDllGetVersion = (DLLGETVERSIONPROC) GetProcAddress(hDllInst,
                              TEXT("DllGetVersion"));
       
            if(pDllGetVersion)     // DLL supports version retrieval function
            {
                DLLVERSIONINFO    dvi;            ZeroMemory(&dvi, sizeof(dvi));
                dvi.cbSize = sizeof(dvi);
                HRESULT hr = (*pDllGetVersion)(&dvi);            if(SUCCEEDED(hr))  // Finally, the version is at our hands
                {
                    dwMajor = dvi.dwMajorVersion;
                    dwMinor = dvi.dwMinorVersion;
                    dwBuildNumber = dvi.dwBuildNumber;
                }
                else
                    bRes = FALSE;   // Failure
            } 
            else  // GetProcAddress failed, the DLL cannot tell its version
                bRes = FALSE;       // Failure        FreeLibrary(hDllInst);  // Release DLL
        }
        else  
            bRes = FALSE;   // DLL could not be loaded    if (!bRes) // Cannot read DLL version the nice way
        {
            for(int iDir = WIN_DIR; iDir <= NO_DIR; iDir++) // loop for each possible directory
            {
                lstrcpy (szFileName, szDLLFileName);    // Save a file name copy for the loading
                bRes = CheckFileVersion (szFileName, iDir, 
                                         dwMajor, dwMinor, dwBuildNumber); // Try the ugly way
                if(bRes)
                    break;
            };
    return bRes;
        }
        else
            return TRUE;
    }
      

  2.   

    可能是我没有说清楚哈,我其实是在dll内部要获取dll的文件名,因为文家发布以后可能被使用者改名,所以我需要运行时刻检测文件名……我想上面的代码不是我所希望的(它是或许dll的版本信息的吧)
      

  3.   

    in dll must use this Function:
    GetCommandLine