Reputation: 1411
I want to list down the assembly names which are linked to my .exe. As I see in c#, we have an API which does the thing I want i.e. "Assembly.GetReferencedAssemblies"
Description: Gets the AssemblyName objects for all the assemblies referenced by this assembly.
Similar to C#, can we get the list of libraries in c++.
It would be great if anyone could give me an advice.
Upvotes: 0
Views: 125
Reputation: 3239
Here you go. Open VS and create a new Win32 console application
Copy and paste below code. Run it.
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <psapi.h>
#include <vector>
#include <iostream>
#include <string>
int PrintModules(DWORD processID)
{
std::vector<HMODULE> modules;
HANDLE process;
DWORD bytesNeeded;
std::cout << "Process ID:" << processID << std::endl;
process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
if(NULL == process)
return 1;
EnumProcessModulesEx(process, nullptr, 0, &bytesNeeded, LIST_MODULES_ALL);
modules.resize(bytesNeeded / sizeof(HMODULE));
if(EnumProcessModulesEx(process, modules.data(), modules.size() * sizeof(HMODULE), &bytesNeeded, LIST_MODULES_ALL))
{
for(auto handle : modules)
{
std::vector<char> moduleName(1024, 0);
auto newSize = GetModuleFileNameEx(process, handle, moduleName.data(), moduleName.size());
moduleName.resize(newSize);
std::cout << "\t" << moduleName.data() << std::endl;
}
}
CloseHandle(process);
return 0;
}
int main()
{
PrintModules(GetCurrentProcessId());
return 0;
}
You should get something like
Process ID:9348
C:\Path\Visual Studio 2015\Projects\ConsoleApplication5\Debug\ConsoleApplication5.exe
C:\WINDOWS\SYSTEM32\ntdll.dll
C:\WINDOWS\System32\KERNEL32.DLL
C:\WINDOWS\System32\KERNELBASE.dll
C:\WINDOWS\SYSTEM32\MSVCP140D.dll
C:\WINDOWS\SYSTEM32\VCRUNTIME140D.dll
C:\WINDOWS\SYSTEM32\ucrtbased.dll
Now add new Win32 DLL to the solution. Link your console application with it. Call any method from new library in your main
. Something like this.
int main()
{
auto res = fnMyLibrary();
PrintModules(GetCurrentProcessId());
return 0;
}
Rerun you program. You should get something like this
Process ID:9348
C:\Path\Visual Studio 2015\Projects\ConsoleApplication5\Debug\ConsoleApplication5.exe
C:\WINDOWS\SYSTEM32\ntdll.dll
C:\WINDOWS\System32\KERNEL32.DLL
C:\WINDOWS\System32\KERNELBASE.dll
C:\Path\Visual Studio 2015\Projects\ConsoleApplication5\Debug\MyLibrary.dll
C:\WINDOWS\SYSTEM32\MSVCP140D.dll
C:\WINDOWS\SYSTEM32\VCRUNTIME140D.dll
C:\WINDOWS\SYSTEM32\ucrtbased.dll
The whole solution is here
Upvotes: 1