0

In C#, I am using an external dll, using loadLibrary, like this:

public class Utilities
    [DllImport("kernel32", CharSet= CharSet.Auto, SetLastError=true)]
    private static extern IntPtr LoadLibrary(string librayName);
    [DllImport("kernel32", CharSet= CharSet.Auto, SetLastError=true)]
    private static extern IntPtr GetProcAddress(intPtr hwnd, string procedureName);

    public static LoadAssembliesAndMethods() {
        string mainPath = AppDomain.CurrentDomain.BaseDirectory;
        string path = Path.Combine(mainPath, "MyAssembly.dll");

        IntPtr ptr = LoadLibrary(path);

        // What to do next in order to get all the list of functions/methods/class in the library and use them?

    }

The dll has no signature of Assembly (it's a 3rd party), so I cannot do

Assebly.LoadFile(path);

I need to get all of the functions/methods/class of the dll, and use some of them, using C#.

How can I do that.

Eitan
  • 1,286
  • 2
  • 16
  • 55
  • what kind of dll library do you want to load? – Bacon Feb 17 '20 at 08:32
  • `LoadLibrary` then `GetProcAddress(hLibrary, functionName)` returns address (`IntPtr`) of the desired function – Dmitry Bychenko Feb 17 '20 at 08:33
  • I don't know if the desire function is inside a static class or just a function. I don't have the source of the 3rd party library. I need to check the list of the classes, and list of methods, and use then use them. – Eitan Feb 17 '20 at 08:49

2 Answers2

1

There is no common way to get the export list of a unmanaged library programatically.

See Is there a way to find all the functions exposed by a dll for more info

The Snowman
  • 84
  • 1
  • 2
1

If you want to list all methods inside a unmanaged dll this link help you:

C# get the list of unmanaged C dll exports

Update:

IntPtr funcaddr = GetProcAddress(Handle,functionName);
YourFunctionDelegate function = 
Marshal.GetDelegateForFunctionPointer(funcaddr,typeof(YourFunctionDelegate )) 
as YourFunctionDelegate ;
function.Invoke(pass here your parameters);

you need to create delegates at coding or using DynamicModules for creating delegates at runtime

mostafa
  • 261
  • 1
  • 2
  • 10
  • Thank you. That's help. I see function "AFunc" - How can I call that function? Thanks. – Eitan Feb 17 '20 at 10:03
  • @Eitan check update, use Delegate.CreateDelegate for creating delegate at runtime – mostafa Feb 17 '20 at 14:08
  • after doing: IntPtr ptr = LoadLibrary(path) (as my code); & IntPtr funcaddr = GetProcAddress(Handle,functionName); I get funcaddr=0x00000000, and not a valid address (Even I see that the function name is in the list (as the link you gave: https://stackoverflow.com/questions/18249566/c-sharp-get-the-list-of-unmanaged-c-dll-exports) – Eitan Feb 19 '20 at 06:08
  • OK. found the mistake: as i see on https://stackoverflow.com/questions/3754264/c-sharp-getprocaddress-returns-zero, I see I need to change in the decorator of GetProcAddress: CharSet = CharSet.Ansi – Eitan Feb 19 '20 at 06:59
  • Also, can you give me, please an example for: GetDelegateForFunctionPointer(IntPtr) – Eitan Feb 19 '20 at 07:05
  • @Eitan you need to create delegate yourself or create it at Runtime based on parameters that you read, the hardest part of your work is to create delegate and pass to GetDelegateForFunctionPointer – mostafa Feb 22 '20 at 05:33