none
Consume Relative DLL Method RRS feed

  • Question

  • Hi,

    Created a Common dll with class library project with below code in MessageDisplay.cs Class

    public class MessageDisplay
        {
            public static void DisplayMessage(string message)
            {
                Console.WriteLine("Input Message is : " + message);
            }
        }

    This Project DLL is copied into D Drive.

    Created Another Console Application Project with below code

    public class Program
        {
            public const string dllName = @"D:\Common.dll";
            static void Main(string[] args)
            {
                Console.WriteLine("Please provide input Message : ");
                var message = Console.ReadLine();

                DisplayMessage(message);

                Console.ReadKey();
            }

            [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
            public static extern void DisplayMessage(string message);

       }

    DisplayMessage is unable to find entry point for this method in Imported DLL.

    Could you please help me with this issue?

    Else provide sample implementation of Importing Relative DLL and Invokation of methods in that DLL.

    Friday, March 29, 2019 1:56 PM

Answers

  • Hi

    You could refer to the following code to load the method based on loading assembly.

     static void Main(string[] args)
            {
                Assembly assembly = Assembly.LoadFile("D:\\ClassLibrary.dll");
                Type type = assembly.GetType("ClassLibrary.DisplayMessage");
                object obj = Activator.CreateInstance(type);
                MethodInfo method = type.GetMethod("DisplayMessage1");
                Object[] o = { "Hi" };
                method.Invoke(obj, o);
                Console.ReadKey();
            }

    result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Nayan Bunny Monday, April 1, 2019 6:51 AM
    Monday, April 1, 2019 6:41 AM
    Moderator

All replies

  • DllImport is for loading native DLLs into managed code, not for loading managed code into another managed app. To allow a managed app to access a managed library add a reference to the assembly in your project (or use NuGet).

    If for some reason you cannot add a reference then your only alternative is to use reflection but this should be a last resort. To load the assembly via reflection you'll need to use Assembly.Load but note that you need to read the documentation carefully because it needs to be loaded properly otherwise things won't work and the path is critical for this. Unless you have a shared assembly that contains a base type or interface that both assemblies use then you're going to be stuck with using reflection to do everything including calling that method. This is pretty ugly so creating a shared assembly that both rely on is generally recommended.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 29, 2019 2:10 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 1, 2019 1:50 AM
    Moderator
  • Hi Jack,

    Appreciate your reply.

    Could you please share reference code with me how can Invoke method of a class after Loading Assembly.

    Monday, April 1, 2019 6:04 AM
  • Hi

    Thanks for the feedback.

    You could refer to the following steps to invoke a method of a class.

    Code:

     static void Main(string[] args)
            {
                Console.WriteLine("Please provide input Message : ");
                var message = Console.ReadLine();
    
                DisplayMessage.DisplayMessage1(message);
    
                Console.ReadKey();
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 1, 2019 6:15 AM
    Moderator
  • Hi Jack, Thanks you very much for this process which I already known. If possible, please provide answer based on loading assembly as I am try to keep common library DLLs in some folder for deployment of multiple services consumption.
    Monday, April 1, 2019 6:22 AM
  • Hi

    You could refer to the following code to load the method based on loading assembly.

     static void Main(string[] args)
            {
                Assembly assembly = Assembly.LoadFile("D:\\ClassLibrary.dll");
                Type type = assembly.GetType("ClassLibrary.DisplayMessage");
                object obj = Activator.CreateInstance(type);
                MethodInfo method = type.GetMethod("DisplayMessage1");
                Object[] o = { "Hi" };
                method.Invoke(obj, o);
                Console.ReadKey();
            }

    result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Nayan Bunny Monday, April 1, 2019 6:51 AM
    Monday, April 1, 2019 6:41 AM
    Moderator
  • Thanks Jack.

    This Implementation is working.

    Monday, April 1, 2019 6:51 AM