Wishing you all a Happy and Prosperous New Year 2009


Nov 24, 2008

Using MPUSBAPI.CPP directly instead of MPUSBAPI.DLL

Related Links
1. For learning how to Usie MPUSBAPI.DLL with VC++ 6.0 click here
2. For main.c file used in this example click here.

1. Introduction

All the time, we are trying to use the MPUSBAPI.DLL in various development environments. We tried to use the .DLL in Load-time Linking and Run-time Linking methods. Even Microchip provided samples that show how to use the .DLL in both methods.

After trying few things I realized that the _mpusbapi.cpp file can be added to the project directly. The two important pointes to be noted are:
   - When using along with Borland C++ Builder, _mpusbapi.cpp can be used without modifications.
   - When using with other environments few changes are required.

The project is created in Visual C++ 6.0. But almost the same steps/concepts can be adopted while developing your project in other C++ environments also. 

2. Steps to use the _mpusbapi.cpp instead of mpusbapi.dll in Visual C++ 6.0

  1. Create a Win32 Console application as an Empty Project.
  2. Create main.cpp file and add it to the project.
  3. Now, using Windows Explorer copy the files _mpusbapi.cpp, _mpusbapi.h and Ioctls.h files into your project folder. These files are available in C:\MCHPFSUSB\Pc\Mpusbapi\Dll\Borland_C\Source folder.
  4. Go back to VC++ environment, and add the files to the newly created project. For this select the FileView tab in Workspace window. This window provides options to add the files in 3 sections. Source Files (.CPP) and Header Files(.H) and Resource Files(.RES). In our example we do not have .RES files. Add _mpusbapi.cpp under Source Files section and _mpusbapi.h & _Ioctls.h in Header Files Section.
    NOTE: If you compile the project at this point, it will throw errors. We havn't added any code in main.cpp. And some libraries are missing in the project settings at this point of time.
    Errors:
          Compiling...
          main.cpp
          _mpusbapi.cpp
          C:\VCExamples\test\_mpusbapi.cpp(53) : warning C4068: unknown pragma
          Linking...
             Creating library Debug/test.lib and object Debug/test.exp
          _mpusbapi.obj : error LNK2001: unresolved external symbol __imp__SetupDiGetDeviceInterfaceDetailA@24
          _mpusbapi.obj : error LNK2001: unresolved external symbol __imp__SetupDiEnumDeviceInterfaces@20
          _mpusbapi.obj : error LNK2001: unresolved external symbol __imp__SetupDiDestroyDeviceInfoList@4
          _mpusbapi.obj : error LNK2001: unresolved external symbol __imp__SetupDiGetClassDevsA@16
          LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
          Debug/test.exe : fatal error LNK1120: 5 unresolved externals
          Error executing link.exe.

          test.exe - 6 error(s), 1 warning(s)
  5. The next step is fixing these errors.
       - unknown pragma : This warning comes because the file is originally built for Borland C++ and Borland C++ prgrams can not be understood by VC++. So remove the line #pragma argsused from _mpusbapi.cpp file.

       - error LNK2001: unresolved external symbol : This error came because we need to add setupapi.lib file to the libararies list in the project settings. The functions missing are from this library. So, goto << Project menu->Settings (Alt+F7)->Link Tab >>. Under Object/Library Modules add setupapi.lib and click on ok.

       - error LNK2001: unresolved external symbol _main : This error came as we did not add any code to main.cpp. We will do that at the end.

  6. The next step is setting the byte alignment to 1 byte. For this goto << Project menu->Settings (Alt+F7)->C/C++ Tab >>. Under Category List box select Code Generation. Selecting this displays four more list boxes. Under Struct member alignment select 1 byte.

    Then from same tab, this time from Category List box select Precompiled Headers. Then select Not using precompiled headers and click on OK. If you create some other type of project, then make sure if the precompiled headers agree for 1-byte struct alignment. Normally the compiler throws a warning in case of any differences. If not change the setting to "Not using precompiled headers".
  7. Now open to main.cpp file by selecting it from << Workspace Window->FileView tab>>.
    Copy&Paste the content from C:\MCHPFSUSB\Pc\Mpusbapi\Example Applications\Borland_C\Example 01 - Load-time Linking\console.cpp.
    Dont forget to remove the pragma "#pragma argsused" from this file also.

    NOTE:I added some more code to main.c used in this article. The link to the file is available in the resources box(top-right hand side of screen).
  8. Connect your demo board in the usual way and build and run this application.


In _MPUSBAPI.CPP file there is a DllEntryPoint entry function. To my knowledge the compiler will consider this function as a normal function rather than an entry point as we are not building a DLL here. For exe files main() is the entry point.

If get into trouble because of this function, then you can remove it. Other functions can be called straight away like how we do in Load-time linking.

To use the functions like MPUSBOpen, MPUSBRead etc.. you can follow the same syntax as Load-time linking. All then you need to do is writing a #include "_mpusbapi.h" on top of your source code. Before that you also need to include windows.h. 
   
         

No comments: