Compact 2013 Ebook

17.7 .NET CF DLL
Created by djones on 6/29/2013 4:27:51 AM

Managed Code C# DLL

In this activity the local time string will be generated in a managed code DLL rather than pinvoked from  a native code DLL. This is implemented by using a static class as per the PInvoke one in 17.5 and 17.6 but implements the actual local time string generation within the class. The class is compiled in a separate project into a standalone DLL. The class will be coded in C#  but could just as easily be coded in Visual Basic.

Note: The PInvoke class could also be similarly compiled into a DLL to simplify it reuse with other applications.

Initially the application from 17.5 will be used. The only change to it will be to change the name of the class being called by it.  The 17.6 app will also be used to demonstrate cross-language debugging.

  1. In The App Development IDE, add a new class library project to the solution:
    Add—>New Project—>C#/Windows Embedded Compact/SDK—>Class Library
  2. Call it ManagedTimeDLL
  3. Replace the code in the class file with:
    using System;
    
    using System.Text;
    
    namespace ManagedTime
    
    {
    
        public static class Time
    
        {
    
        }
    
    }
  4. Insert the GetTime method into the class:
            public static void GetTime( StringBuilder dateTime)
    
            {
    
                //Get local time
    
                DateTime localTime = DateTime.Now;
    
                // Generate formated string
    
                string dateTimeStr =  string.Format(
    
                    "Time  {0}:{1}:{2} Date  {3:2}//{4:2}//{5:4}",
    
                    localTime.Hour, localTime.Minute, localTime.Second,
    
                    localTime.Day, localTime.Month, localTime.Year);
    
                //Assumes that dateTime has allocated space
    
                dateTime.Insert(0, dateTimeStr);
    
            }
    
  5. Add a reference to the DLL project in the the ManagedTime app:
    ManagedTime app context—>Add Reference—>Projects—>ManageTimeDLL
  6. Remove the PInvoke class from the ManagedTime app.

    In the ManagedCode app modify the TimeButton event handler by changing the class referenced from PInvoke to TimeDLL:

          //Create a StringBuilder object with capacity for 100 characters 
    
          StringBuilder timeStr = new StringBuilder(100);
    
          //Call the native code 
    
          Time.GetTime(timeStr); //<—Change this 
           //Display the string 
    
          textBox1.Text = timeStr.ToString( );
    
       
  7. Set the app and DLL projects to Debug mode.
  8. Build the ManagedTime app. This will also build the DLL project.
  9. Test run the app. It should function correctly.
  10. Put a breakpoint in the ManagedTimeDLL GetTime method. Rerun the app and establish that the DLL code gets debugged.

    Now to directly use the same DLL with the VB application.
  11. Modify the ManagedTimeVB app (17.6) by:
    1. Remove the PInvoke class
    2. Add a reference to TimeDLL project.
    3. Modify the TimeButton handler by replacing the call to PInvoke with Time, as above in the C# app.
  12. Rebuild the app and test run it with the breakpoint still in the GetTime method.
    Establish that the cross-code debugging works.

NEXT:  17.8 Managed Code Callbacks

print

Click here to provide feedback and input

  Comments

There is no comment.

Turkish porno izle video site in rokettubeporno izle