Esempio n. 1
0
HRESULT CAsmLink::AddImport(mdAssembly AssemblyID, mdToken ImportToken, DWORD dwFlags, mdFile * pFileToken)
{
    // If we have already emitted the manifest, and then we import
    // a file with a CA that maps to an assembly option, we're in trouble!
    ASSERT(m_bInited && !m_bPreClosed && !m_bManifestEmitted);

    HRESULT hr;
    CFile *file = NULL;
    if (TypeFromToken(ImportToken) == mdtModule) {
        ASSERT(RidFromToken(ImportToken) < m_pModules->CountFiles());
        hr = m_pModules->GetFile( ImportToken, &file);
    } else {
        ASSERT(TypeFromToken(ImportToken) == mdtAssemblyRef && RidFromToken(ImportToken) < m_pImports->CountFiles());
        hr = m_pImports->GetFile( ImportToken, &file);
    }
    if (FAILED(hr))
        return hr;
    ASSERT(file != NULL);
    if (FAILED(hr = m_pAssem->AddFile(file, dwFlags, pFileToken)))
        return hr;
    else if (AssemblyID == AssemblyIsUBM) {
        if (pFileToken)
            *pFileToken = ImportToken;
        return S_FALSE;
    }
    ASSERT(AssemblyID == TokenFromRid(mdtAssembly, 1));
    if (FAILED(hr = m_pModules->RemoveFile( ImportToken)))
        return hr;
    if (FAILED(hr = file->ImportFile( NULL, m_pAssem)))
        return hr;
    return file->ImportResources(m_pAssem);
}
Esempio n. 2
0
HRESULT CAsmLink::ImportFile2(LPCWSTR pszFilename, LPCWSTR pszTargetName, IMetaDataAssemblyImport *pAssemblyScopeIn, 
                      BOOL fSmartImport, mdToken *pFileToken, IMetaDataAssemblyImport **ppAssemblyScope, 
                      DWORD *pdwCountOfScopes)
{
    ASSERT(m_bInited);
    LPWSTR newTarget = NULL;
    bool bNewTarget = false;

    IMetaDataAssemblyImport *pAImport = NULL;
    IMetaDataImport *pImport = NULL;
    mdAssembly tkAssembly;
    if (pszFilename && wcslen(pszFilename) > MAX_PATH)
        return FileNameTooLong(pszFilename);
    if (pszTargetName && wcslen(pszTargetName) > MAX_PATH)
        return FileNameTooLong(pszTargetName); // File name too long

    if (pszTargetName == NULL)
        newTarget = VSAllocStr(pszFilename);
    else
        newTarget = VSAllocStr(pszTargetName);
    
    if (_wcsicmp(newTarget, pszFilename) == 0) {
    } else {
        bNewTarget = true;
    }

    if ((pAImport = pAssemblyScopeIn) != NULL)
    {
        pAImport->AddRef();
    }
    else
    {
        HRESULT hr = m_pDisp->OpenScope(pszFilename, ofRead | ofNoTypeLib, IID_IMetaDataAssemblyImport, (IUnknown**)&pAImport);
        if (FAILED(hr)) {
            *pFileToken = 0;
            if (ppAssemblyScope) *ppAssemblyScope = NULL;
            if (pdwCountOfScopes) *pdwCountOfScopes = 0;
            VSFree(newTarget);
            return hr;
        }
    }

    HRESULT hr;
    VERIFY(SUCCEEDED(hr = pAImport->QueryInterface( IID_IMetaDataImport, (void**)&pImport)));
    hr = pAImport->GetAssemblyFromScope( &tkAssembly);

    if (FAILED(hr)) {
        hr = S_FALSE;
        // This is NOT an assembly
        if (ppAssemblyScope) *ppAssemblyScope = NULL;
        if (pdwCountOfScopes) *pdwCountOfScopes = 1;
        *pFileToken = mdTokenNil;
        CFile* file = new CFile(newTarget, pImport, m_pError, this);
        if (bNewTarget) {
            if (SUCCEEDED(hr = file->SetSource(pszFilename)))
                hr = file->UpdateModuleName();
        }
        pAImport->Release();
        pImport->Release();
        if (SUCCEEDED(hr) && SUCCEEDED(hr = m_pModules->AddFile( file, 0, pFileToken))) {
            if (fSmartImport)
                hr = file->ImportFile( NULL, NULL);
            *pFileToken = TokenFromRid(RidFromToken(*pFileToken), mdtModule);
        }
        if (ppAssemblyScope) *ppAssemblyScope = NULL;
        if (pdwCountOfScopes) *pdwCountOfScopes = 1;
        VSFree(newTarget);
        return hr == S_OK ? S_FALSE : hr;
    } else {
        // It is an Assembly
        CAssemblyFile* assembly = new CAssemblyFile( pszFilename, pAImport, pImport, m_pError, this);
        if (ppAssemblyScope)
            *ppAssemblyScope = pAImport;
        else
            pAImport->Release();
        pImport->Release();
        if (m_bDontDoHashes)
            assembly->DontDoHash();
        hr = assembly->ImportAssembly(pdwCountOfScopes, fSmartImport, m_pDisp);

        if (SUCCEEDED(hr) && bNewTarget)
            hr = assembly->ReportError( ERR_CantRenameAssembly, mdTokenNil, NULL, pszFilename);

        if (FAILED(hr)) {
            delete assembly;
            VSFree(newTarget);
            return hr;
        }

        VSFree(newTarget);
        if (FAILED(hr = m_pImports->AddFile( assembly, 0, pFileToken)))
            delete assembly;
        return SUCCEEDED(hr) ? S_OK : hr;
    }
}