Interacting with Microchip Full-Speed USB USB Demo Board

Visit  home page for more
USB related articles.


Interacting with Microchip Full-Speed USB Demo Board using Visual Studio Tools (Part I) - Windows Basics

Download the source code for VC++ Example





The next section covers MPUSBAPI.DLL. Click here to goto next section.

Click here to goto previous section - Introduction.

1. Libraries in Windows Operating System

A Software Library is a collection of functions, data and resources(bitmaps, string tables etc..) in object code format. The libraries provide modularization and re-usability of code. So, by developing a library we can group related code together and use this code whenever any application requires that kind of functionality. The code in the libraries are added to application code during the process of LINKING.

The compiler tool chains used by Embedded Systems developers will implement a technique called, Static Linking to link libraries to the application. In this technique, all the libraries + source code object files are combined together and a final single HEX file will be generated, which will be used to program the device or board.

But, in a OS like Windows where Multi-tasking is implemented, two or more copies of same application can be executed. And more than two copies of applications that use same library code can also be executed.

What this means is, if Static Linking technique is used then, there is redundant code and data in the memory at the same time causing lot of memory wastage, and also the operating system has to lot of work loading unloading applications with larger in size into memory which can consume more time.

To avoid these problems, Multi-tasking OSs like Windows came out with another technique of for linking libraries to the applications, which is known as Dynamic-Linking. Those libraries that used for Dynamic-Linking are called as Dynamic-Link Libraries. And they will have an extension of .DLL.

So, The C:\MCHPFSUSB\Pc\Mpusbapi\Dll\Borland_C\mpusbapi.dll is a DLL, or Dynamic Link Library.

When two applications are using the same DLL, then there will only one code copy of the DLL in memory which will be shared by both applications. This will eventually reduce the size of user application and as the library code is not combined to the application, it will speed up loading of applications. For more information on DLLs, goto Microsoft MSDN site. Alternatively, you can read the books on Windows Programming.

One note is, even though Windows supports DLLs extensively, it still supports usage of Static Link Libraries, but Microchip's DLL(MPUSBAPI.DLL) is a Dynamic Link Library.


2. Techniques for invoking functions from the library in Windows Operating Systems

When a library project is built two files will be created. One is Library reference file(.LIB) and the other is Object Code file( .OBJ, .DLL). Object code is Machine Code with unresolved memory map and references. An Executable normally will have the references and memory map resolved.

Once the .LIB file and .OBJ file are generated, for using them in C or C++ programs, a Header File (.H) will also be provided/required to be created. The Header file will be included into the source files using #include in the project and .LIB file will be added to Files to be Linked list of the project. The .OBJ will be placed in the source file folder. Sometimes, the .LIB file itself contains the Object Code also, depending on the compiler chain, and there will not be any .OBJ file generated.

Now, if the generated files are .LIB and .OBJ, or only .LIB file, then these files will be used in Static linking,and if the generated files are .DLL and .LIB files then they will be used in Dynamic-Linking.

In Static Linking, the method of calling the included library functions is straight forward, and it is as good as calling any other function in the source files which are part of the project. The .H file need to be included in the sources files at required locations.

In Dynamic Linking, again there are two methods to access the functions available in the library(DLL). They are,
2.1. Load-Time Linking: In this method, the library is linked to the application using a .LIB in the same manner, how it is done with Static Linking. The only difference is the .DLL file associated with the .LIB file will be loaded at the time of execution. i.e when the application is loaded into the memory. In this technique, the .LIB file will be added to the project library list and the .DLL file will be placed in the system PATH. Functions will be called in the same manner as Static Linking.
The example given in the following path of C:\MCHPFSUSB\Pc\Mpusbapi\Example Applications\Borland_C\Example 01 - Load-time Linking demonstrate this technique.

2.2. Run-time Linking: In this technique, the .LIB file will not be used. The application assumes that the .DLL file is available in the system PATH. The application uses LoadLibrary(), GetProcAddress() and FreeLibrary() Win32 API calls for invoking the functions from the DLL.
The .DLL file is supposed to have the exported public symbols for using these functions. This technique is primarily helpful, when trying to access a DLL library for which a .LIB and/or a .H file are not available. We will use this technique for accessing the MPUSBAPI.DLL file as we don't have proper .LIB for Visual Studio.

The example given in the following path of C:\MCHPFSUSB\Pc\Mpusbapi\Example Applications\Borland_C\Example 01 - Load-time Linking demonstrate this technique.

3. Identifying the Function Names in a .DLL file with Dependency Walker.

The Dependency Walker tool comes with Visual Studio 6.0 . This tool displays what all the functions available in a .DLL file. When the MPUSBAPI.DLL file is opened with Dependency Walker, it looks like this.
View of MPUSBAPI.DLL in Dependency Walker

So, from this view we can make out that there are 7 functions available in the DLL and, all of them have underscore ("_") prefixed to them. The same functions along with comments can found out in the files. C:\MCHPFSUSB\Pc\Mpusbapi\Dll\Borland_C\Source\_mpusbapi.cpp, C:\MCHPFSUSB\Pc\Mpusbapi\Dll\Borland_C\Source\_mpusbapi.h.
The folder, C:\MCHPFSUSB\Pc\Mpusbapi\Dll\Borland_C\Source\ has two important header files. _mpusbapi.h and mpusbapi.h(without underscore).

Now we know what functions are available in MPUSBAPI.DLL file. Next step is understand how to use these functions for application development. The _mpusbapi.h (with underscore) file is the actual header file. What it means is it declares the actual functions. This file can only be used during Load Time Linking.

As we know that Load Time Linking is not possible with Visual Studio and MPUSBAPI.DLL, We need to use the Run Time Linking Technique.

4. Introduction to LoadLibrary(), GetProcAddress() and FreeLibrary()

4.1 LoadLibrary():
This function is used to load any .DLL at run-time of the application.

SYNTAX:
HMODULE LoadLibrary( LPCTSTR lpFileName);
This function takes the .DLL file name or a file name including full path as argument. Then it loads the DLL into the memory for use by application and returns HANDLE (HANDLE is a unique unsigned long number given by Windows OS to each object when it is loaded/created. The object can be application, DLL, Bitmap etc...). If the .DLL is already loaded this function returns the an existing handle.
If the DLL can not be loaded due to incorrect path or any other reason, this function returns NULL. So, the user application need to check for this condition.

4.2 GetProcAddress():
This function is used to get a pointer to a function in the .DLL file.

SYNTAX:
FARPROC GetProcAddress( HMODULE hModule, LPCSTR lpProcName );
This function takes two arguments. The HANDLE returned by the LoadLibrary function for hModule, and the name or ordinal of the function. So, if you take a look at the Dependency Walker picture shown earlier, _MPUSBGetDLLVersion will be name of the function. And 1 is it's ordinal number.
If the specified function with the name given is not found in the .DLL then it returns NULL.
The returns value from the function need to be collected in to appropriate pointer variable. This is when the mpusbapi.h (without underscore) comes to use. Microchip has done all the work in this case. The author of MPUSBAPI.DLL created this file keeping in mind the situation. This file has all the required pointer declarations for all the functions supported by DLL and this file need to be included in to the appropriate source files as required so that those declarations need not be made again.

4.3 FreeLibrary():
This function is used to close the .DLL library that is opened with LoadLibrary.

SYNTAX:
BOOL FreeLibrary( HMODULE hModule );
The hModule argument what is returned by LoadLibrary is used as argument here. This function need to be called at the end of the program or for unloading the library from memory.

The next section covers MPUSBAPI.DLL. Click here to goto next section.

Click here to goto previous section - Introduction.



Shop for USB Devices

Books and Gadgets

Site Resources

External Resources

Contact Us



Copyright 2006, www.comvcon.com