Skip to content

[Question] Need help obtaining assembly dependencies #114202

Closed
@yegor-pelykh

Description

@yegor-pelykh

There is a DLL written in native C++ using CLR Profiling API.
I need to build a dependency tree of all managed assemblies/modules of the process, starting from its main module, using the same native C++, possibly using the same CLR Profiling API or other arsenal of the CLR.
For each assembly/module, I need to know the full path to it.

Something like this approach in C#, but in unmanaged C++.

Dictionary<string, List<string>> GetDependencies(string assemblyPath)
{
    var dependencies = new Dictionary<string, List<string>>();
    var assembly = Assembly.LoadFrom(assemblyPath);
    var referencedAssemblies = assembly.GetReferencedAssemblies();

    dependencies[assembly.FullName] = new List<string> { assembly.Location };
    foreach (var referencedAssembly in referencedAssemblies)
    {
        string referencedAssemblyPath = Path.Combine(Path.GetDirectoryName(assemblyPath), referencedAssembly.Name + ".dll");
        dependencies[assembly.FullName].Add(referencedAssemblyPath);
        var subDependencies = GetDependencies(referencedAssemblyPath);
        foreach (var subDependency in subDependencies)
        {
            if (!dependencies.ContainsKey(subDependency.Key))
            {
                dependencies[subDependency.Key] = subDependency.Value;
            }
        }
    }

    return dependencies;
}

How can this be implemented? Please give me some hints.

Ideally, I need a solution that would work for all versions of .NET/Core/Framework.

For some reason, the profiler callbacks AssemblyLoadStarted / AssemblyLoadFinished are not called for me (given the correct event mask). Therefore, solutions with these methods are unlikely to help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Diagnostics-coreclrneeds-author-actionAn issue or pull request that requires more info or actions from the author.no-recent-activityquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions