dsi_cm_library.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except in compliance
  4. with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2014
  6. All rights reserved.
  7. */
  8. #include "types.h"
  9. #if defined(DSI_TYPES_WINDOWS)
  10. #include "dsi_cm_library.hpp"
  11. #include <memory>
  12. using namespace std;
  13. #define LIBRARY_NAME "DSI_CP210xManufacturing_3_1.dll"
  14. //Static variable initializations
  15. std::auto_ptr<CMLibrary> CMLibrary::clAutoInstance(NULL);
  16. BOOL CMLibrary::bStaticSet = FALSE;
  17. //return an auto_ptr?
  18. BOOL CMLibrary::Load(auto_ptr<const CMLibrary>& clAutoLibrary_) //!!Should we lose the dependency on auto_ptr and just return the pointer (and let the user make their own auto_ptr)?
  19. {
  20. try
  21. {
  22. clAutoLibrary_.reset(new CMLibrary());
  23. }
  24. catch(...)
  25. {
  26. clAutoLibrary_.reset(NULL);
  27. return FALSE;
  28. }
  29. return TRUE;
  30. }
  31. CMLibrary::CMLibrary() //throw(CMError::Enum&)
  32. :
  33. GetDeviceProductString(NULL),
  34. GetDeviceSerialNumber(NULL),
  35. GetDeviceVid(NULL),
  36. GetDevicePid(NULL),
  37. ResetDevice(NULL),
  38. hLibHandle(NULL)
  39. {
  40. //Check static instance
  41. if(bStaticSet == FALSE)
  42. {
  43. bStaticSet = TRUE;
  44. try
  45. {
  46. clAutoInstance.reset(new CMLibrary());
  47. }
  48. catch(...)
  49. {
  50. bStaticSet = FALSE;
  51. throw;
  52. }
  53. }
  54. //load library
  55. CMError::Enum ret = LoadFunctions();
  56. if(ret != CMError::NONE)
  57. throw(ret);
  58. return;
  59. }
  60. CMLibrary::~CMLibrary() throw()
  61. {
  62. FreeFunctions();
  63. }
  64. ///////////////////////////////////////////////////////////////////////
  65. // Loads USB interface functions from the DLLs.
  66. ///////////////////////////////////////////////////////////////////////
  67. CMError::Enum CMLibrary::LoadFunctions()
  68. {
  69. hLibHandle = LoadLibrary(LIBRARY_NAME);
  70. if(hLibHandle == NULL)
  71. return CMError::NO_LIBRARY;
  72. BOOL bStatus = TRUE;
  73. GetDeviceProductString = (GetDeviceProductString_t)GetProcAddress(hLibHandle, "CP210x_GetDeviceProductString");
  74. if(GetDeviceProductString == NULL)
  75. bStatus = FALSE;
  76. GetDeviceSerialNumber = (GetDeviceSerialNumber_t)GetProcAddress(hLibHandle, "CP210x_GetDeviceSerialNumber");
  77. if(GetDeviceSerialNumber == NULL)
  78. bStatus = FALSE;
  79. GetDeviceVid = (GetDeviceVid_t)GetProcAddress(hLibHandle, "CP210x_GetDeviceVid");
  80. if(GetDeviceVid == NULL)
  81. bStatus = FALSE;
  82. GetDevicePid = (GetDevicePid_t)GetProcAddress(hLibHandle, "CP210x_GetDevicePid");
  83. if(GetDevicePid == NULL)
  84. bStatus = FALSE;
  85. ResetDevice = (Reset_t)GetProcAddress(hLibHandle, "CP210x_Reset");
  86. if(ResetDevice == NULL)
  87. bStatus = FALSE;
  88. if(bStatus == FALSE)
  89. {
  90. FreeFunctions();
  91. return CMError::NO_FUNCTION;
  92. }
  93. return CMError::NONE;
  94. }
  95. ///////////////////////////////////////////////////////////////////////
  96. // Unloads USB DLLs.
  97. ///////////////////////////////////////////////////////////////////////
  98. void CMLibrary::FreeFunctions()
  99. {
  100. if(hLibHandle != NULL)
  101. {
  102. FreeLibrary(hLibHandle);
  103. hLibHandle = NULL;
  104. }
  105. return;
  106. }
  107. #endif //defined(DSI_TYPES_WINDOWS)