Is it possible to call code in application from DLL?

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
Morning ( ;)) all,

I'm busy with an application and while coding I can see that it can be useful to convert part (one class) of that application at a later stage to a DLL (no experience with creating DLLs, but that is not part of the question).

Below code is part of the class that I consider to move to a DLL
namespace dllNamespace
{
    class classInDLL
    {
        predefinedNamespace.predefinedClass myclass = new predefinedNamespace.predefinedClass();

        string executeresult=null;

        public bool doSomework(string strInput)
        {
            string result1 = "S2";
            string result2 = "b0001";
            executeresult = someMethod(result1, result2);
        }

        public string getResult()
        {
            return executeresult;
        }

        private string someMethod(string strA, string strB)
        {
            return myclass.execute_Enter(strA, null, strB);
        }
    }
}


This is the code that will stay in the application or needs to be coded in future applications that use the DLL
namespace predefinedNamespace
{
    class predefinedClass
    {
        public string execute_Enter(string strBase, string strInput, string strOutput)
        {
            string toExecute = strBase + "_Enter";
            System.Reflection.MethodInfo mi = this.GetType().GetMethod(toExecute);

            if (mi == null)
            {
                return genericMethod(toExecute);
            }
            else
            {
                return (string)mi.Invoke(this, new object[] { strOutput });
            }
        }

        public string S2_Enter(string strValue)
        {
            uint result;
            if (LibraryWim.Utils.String2UInt(strValue, out result))
            {
                return "Result of entering S2: output value b" + Convert.ToString(result, 2).PadLeft(32, '0');
            }
            else
            {
                return "S2: Invalid output defined";
            }
        }

        public string genericMethod(string strBase)
        {
            return String.Format("This is the result of a generic method as no method matching '{0}' is coded by the programmer", strBase);
        }
    }
}


The idea is that from the DLL user defined methods in a specific class in the application that uses the DLL (that are not known when the DLL is compiled) can be called. So the programmer that uses the DLL will implement the methods that are required by the application. The DLL may assume that the class predefinedNamespace.predefinedClass will be available at run time and if it's not throw an exception.

Is this possible? If so, what do I need to read up on?

The idea is that the user enters some data in an application, a method in the DLL is called and based on the 'result' of that method another method is called that is outside the DLL.
namespace applicationNamespace
{
    private void Go()
    {
        dllNamespace.classInDLL theClass = new dllNamespace.classInDLL();
        theClass.doSomework("SomeUserInput");
Console.WriteLine(theClass.getResult());
    }
}


I hope this is a little bit clear.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,216
Location
Norway
Programming Experience
10+
Raise an event in DLL, its consumer can act upon it. A variation of this is to pass a delegate to DLL that it can invoke.
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
Follow up question

I have implemented something and it works. There is however one question that I could not find the answer to.

The below code (in the DLL) triggers one of two events and next returns true or false.
        public bool gotoNext()
        {
            currentstate++;
            // simulate statechange result
            if (currentstate < numstates)
            {
                // state has changed
                StateChangedEventArgs args = new StateChangedEventArgs();
                args.State = currentstate;
                OnStateChanged(args);
                return true;
            }
            else
            {
                // state has not changed
                StateChangedEventArgs args = new StateChangedEventArgs();
                args.State = currentstate;
                OnStateNotChanged(args);
                return false;
            }
        }


The question is about the sequence of events. Will the triggering of the events always cause the event handler in the remaining code to be called before the shown code returns? So if it's the same as 'calling the event handler directly' like calling a method? Debugging shows that it is the case, but I need to know if I need to cater for situations where this might not be the case.

Thanks in advance.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,122
Location
Sydney, Australia
Programming Experience
10+
The question is about the sequence of events. Will the triggering of the events always cause the event handler in the remaining code to be called before the shown code returns? So if it's the same as 'calling the event handler directly' like calling a method? Debugging shows that it is the case, but I need to know if I need to cater for situations where this might not be the case.

Yes, that will be the case in all situations. In your OnStateChanged method you should have a RaiseEvent statement. That statement basically loops through the handlers for the event and executes each one in sequence. That statement does not complete, and therefore your method does not return, until all event handlers have executed.

EDIT: What I said about the OnStateChanged method applies to VB rather than C#. I forgot what forum I was in for a moment. In C#, the method should first test whether the event is null, i.e. whether no handlers have been registered, and, if it's not, invoke it, which basically means internally looping through each registered handler. It's pretty much the same as RaiseEvent except that you have explicitly test for null first.
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
Thanks. I think you're referring to this type of code.

        protected virtual void OnStateChanged(EventArgs e)
        {
            EventHandler<StateChangedEventArgs> handler = StateChanged;
            if (handler != null)
            {
                handler(this, (statemachineDLL.StateChangedEventArgs) e);
            }
        }
 
Top Bottom