Compact 2013 Ebook

14.8 Using a DLL
Created by djones on 6/18/2013 8:04:59 AM

Using a Dynamic Link Library (.DLL)

It is a good software architectural practice to separate the user interface code from computational code. This facilitates simpler testing and debugging and code reuse of the computational components with different UIs. In the time example a static lib or DLL could be created that has a function that returns the System Time as formatted string. That module could then be used with both the console application and the winform app. In a later chapter we will use it with an XAML UI app.

In this activity, the Time processing for WinForm Application is refactored into a DLL. It could just as easily be actioned with the Console app

1. Add a new subproject again but this time choose the WCE Static Library. Call the library TimeDLL. On the next dialog choose the option “A DLL that exports some symbols”.

clip_image002

Figure 14.19: Subproject Wizard DLL options

2. Open the project’s header file and you will see that a class has been created, CTimeDLL with a constructor, an integer value nTimeDLL and a function fnTimeDLL are exported by the DL. Open the source file for the project and examine how they are implemented.

We just want an exported function that returns a Unicode string (LPWSTR or TCHAR * or WCHAR * etc.) .
We could though do this as a method of the class.

3. Open the project header file and replace the code with:

#ifdef TimeDLL_EXPORTS
#define TimeDLL_API __declspec(dllexport)
#include 
#else
#define TimeDLL_API __declspec(dllimport)

#endif

 

 

TimeDLL_API void GetTime(TCHAR * time);

Listing 14.15 The header code

We will return a static string for testing then add the get time functionality.

6. Open the file TimeDLL.cpp and replace the existing code with:
(Remove CTimeDLL class, nTimeDLL and fnTimeDLL code)

TimeDLL_API  void GetTime(TCHAR * time)
{
    _stprintf( time, _T("About now"));
}

Listing 14.15a The code to implement preliminary version of GetTime() in DLL code source file.

6. Set the project as usual to be built, not in the image but built in Debug mode.
Build this project. Then explore the project files. Open the obj\Debug directory. Note that it contains both the .dll and .lib files. The .dll is what is needed at runtime and .lib is required by the application that uses it during the link phase of its build process. This is a lighter weight .lib than that produced by a static lib project.

7. In the main project (HelloWinForm) add the reference the the DLL’s header towards the top of HelloWinForm.cpp:

#include "../TimeDLL/TimeDLL.h"

Listing 14.16 Reference to the DLL header in in the Winform in application code source file.

8. In HelloWinForm sources project file (i.e. the sources file: double-click on the project in Solution Explorer) change the linkage (TARGETLIBS) to include the reference to TimeDLL.lib on a new line:

$(SG_OUTPUT_ROOT)\sdk\lib\$(_CPUINDPATH)\coredll.lib \
..\TimeDLL\obj\$(_TGTCPU)\$(WINCEDEBUG)\TimeDLL.lib 

Listing 14.17 Linkage reference to the DLL in the application’s sources file.

Note in sources there needs to be no spaces after \ on the end of lines.
It is the line continuation character.

9. In HelloWinForm, in HelloWinForm.cpp modify the code following the comment //Get Local Time to:

//Get Local Time
TCHAR dateTime[100];
//_stprintf <-- Comment this function call out
//(dateTime,
//_T("Time  %02d:%02d:%02d Date [DD/MM/YYYY] %02d/%02d/%04d\n"),
//st.wHour,st.wMinute,st.wSecond, st.wDay, 
//st.wMonth, st.wYear
//);
GetTime(dateTime); <-- Add this

Listing 15.8a: Modifications to MyFirstXamlApp.cpp for the DLL

10. Build the application and test run it. Pressing Get Time should display “About now”.

11. Open the TimeDLL source file again and change its code to:

TimeDLL_API  void GetTime(TCHAR * time)
{
//Get the system time
SYSTEMTIME st;
GetLocalTime(&st);
//Write it to the string
_stprintf(time,
	_T("Time  %02d:%02d:%02d Date [DD/MM/YYYY] %02d/%02d/%04d"),
		st.wHour,st.wMinute,st.wSecond, st.wDay, 
		st.wMonth, st.wYear
	);
}

Listing 14.18 The code to final version of GetTime() in DLL code source file.

12. Rebuild the DLL and test run it. The current time should again be displayed in the textbox.

Note:     The application did not need rebuilding because the linkage information didn't change (the function prototype for GetTime( ) didn’t change).


NEXT: Using a Static Link Library (.LIB)

print

Click here to provide feedback and input

  Comments

There is no comment.

Turkish porno izle video site in rokettubeporno izle