LanShareDll.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "StdAfx.h"
  2. #include "LanShareDll.h"
  3. namespace LanShareDll
  4. {
  5. HMODULE g_hLANShareDLL = NULL;
  6. DisConnectSession g_DisConnectSession = NULL;
  7. DelShareDirectoryW g_DelShareDirectoryW = NULL;
  8. DelShareDirectoryA g_DelShareDirectoryA = NULL;
  9. IsTheDirectorySharedW g_IsTheDirectorySharedW = NULL;
  10. IsTheDirectorySharedA g_IsTheDirectorySharedA = NULL;
  11. AccessShareDirectory g_AccessShareDirectory = NULL;
  12. AddShareDirectoryW g_AddShareDirectoryW = NULL;
  13. AddShareDirectoryA g_AddShareDirectoryA = NULL;
  14. SetNetShareSecurity g_SetNetShareSecurity = NULL;
  15. AddAccessRights g_AddAccessRights = NULL;
  16. BOOL LoadLANShareLibrary()
  17. {
  18. if (g_hLANShareDLL == NULL)
  19. g_hLANShareDLL = LoadLibrary(_T("LANShare.dll"));
  20. if (g_hLANShareDLL == NULL)
  21. {
  22. AfxMessageBox(_T("加载LANShare模块失败!"));
  23. return FALSE;
  24. }
  25. g_DisConnectSession = (DisConnectSession)::GetProcAddress(g_hLANShareDLL, "DisConnectSession");
  26. g_DelShareDirectoryW = (DelShareDirectoryW)::GetProcAddress(g_hLANShareDLL, "DelShareDirectoryW");
  27. g_DelShareDirectoryA = (DelShareDirectoryA)::GetProcAddress(g_hLANShareDLL, "DelShareDirectoryA");
  28. g_IsTheDirectorySharedW = (IsTheDirectorySharedW)::GetProcAddress(g_hLANShareDLL, "IsTheDirectorySharedW");
  29. g_IsTheDirectorySharedA = (IsTheDirectorySharedA)::GetProcAddress(g_hLANShareDLL, "IsTheDirectorySharedA");
  30. g_AccessShareDirectory = (AccessShareDirectory)::GetProcAddress(g_hLANShareDLL, "AccessShareDirectory");
  31. g_AddShareDirectoryW = (AddShareDirectoryW)::GetProcAddress(g_hLANShareDLL, "AddShareDirectoryW");
  32. g_AddShareDirectoryA = (AddShareDirectoryA)::GetProcAddress(g_hLANShareDLL, "AddShareDirectoryA");
  33. g_SetNetShareSecurity = (SetNetShareSecurity)::GetProcAddress(g_hLANShareDLL, "SetNetShareSecurity");
  34. g_AddAccessRights = (AddAccessRights)::GetProcAddress(g_hLANShareDLL, "AddAccessRights");
  35. return TRUE;
  36. }
  37. void FreeLANShareLibrary()
  38. {
  39. if (g_hLANShareDLL)
  40. {
  41. if ( !FreeLibrary(g_hLANShareDLL) )
  42. {
  43. _tprintf_s(_T("释放LANShare.dll失败,%d\n"), GetLastError());
  44. }
  45. }
  46. g_hLANShareDLL = NULL;
  47. g_DisConnectSession = NULL;
  48. g_DelShareDirectoryW = NULL;
  49. g_DelShareDirectoryA = NULL;
  50. g_IsTheDirectorySharedW = NULL;
  51. g_IsTheDirectorySharedA = NULL;
  52. g_AccessShareDirectory = NULL;
  53. g_AddShareDirectoryW = NULL;
  54. g_AddShareDirectoryA = NULL;
  55. g_SetNetShareSecurity = NULL;
  56. g_AddAccessRights = NULL;
  57. }
  58. BOOL API_DisConnectSession( IN LPCTSTR lpHostAddr )
  59. {
  60. if ( g_DisConnectSession )
  61. return g_DisConnectSession(lpHostAddr);
  62. else
  63. _tprintf_s(_T("加载DisConnectSession函数失败\n"));
  64. return FALSE;
  65. }
  66. BOOL API_DelShareDirectory( IN LPCTSTR lpShareName )
  67. {
  68. #ifdef UNICODE
  69. if ( g_DelShareDirectoryW )
  70. return g_DelShareDirectoryW(lpShareName);
  71. #else
  72. if ( g_DelShareDirectoryA )
  73. return g_DelShareDirectoryA(lpShareName);
  74. #endif
  75. else
  76. _tprintf_s(_T("加载DelShareDirectory函数失败\n"));
  77. return FALSE;
  78. }
  79. BOOL API_IsTheDirectoryShared(IN LPCTSTR lpDirectory, OUT LPCTSTR lpShareName /* = NULL */, IN const INT& nBufLen /* = 0 */ )
  80. {
  81. if ( lpShareName == NULL || lpDirectory == NULL )
  82. return FALSE;
  83. TCHAR *pszShareName = new TCHAR[nBufLen];
  84. memcpy(pszShareName, lpShareName, _tcslen(lpShareName)*sizeof(TCHAR));
  85. #ifdef UNICODE
  86. if ( g_IsTheDirectorySharedW )
  87. return g_IsTheDirectorySharedW(lpDirectory, pszShareName, nBufLen);
  88. #else
  89. if ( g_IsTheDirectorySharedA )
  90. return g_IsTheDirectorySharedA(lpDirectory, pszShareName, nBufLen);
  91. #endif
  92. else
  93. _tprintf_s(_T("加载IsTheDirectoryShared函数失败\n"));
  94. return FALSE;
  95. }
  96. BOOL API_AccessShareDirectory(IN LPCTSTR lpUserName, IN LPCTSTR lpPasswd, IN LPCTSTR lpShareDirectory)
  97. {
  98. if ( g_AccessShareDirectory )
  99. return g_AccessShareDirectory(lpUserName, lpPasswd, lpShareDirectory);
  100. else
  101. _tprintf_s(_T("加载AccessShareDirectory函数失败\n"));
  102. return FALSE;
  103. }
  104. BOOL API_AddShareDirectory(IN LPCTSTR lpDirectory, IN LPCTSTR lpShareName)
  105. {
  106. #ifdef UNICODE
  107. if ( g_AddShareDirectoryW )
  108. return g_AddShareDirectoryW(lpDirectory, lpShareName);
  109. #else
  110. if ( g_AddShareDirectoryA )
  111. return g_AddShareDirectoryA(lpDirectory, lpShareName);
  112. #endif
  113. else
  114. _tprintf_s(_T("加载AddShareDirectory函数失败\n"));
  115. return FALSE;
  116. }
  117. BOOL API_SetNetShareSecurity(IN LPCTSTR lpShareName, IN LPCTSTR lpUsser, IN const DWORD& dwPermissions /* = STANDARD_RIGHTS_ALL| SPECIFIC_RIGHTS_ALL */ )
  118. {
  119. if ( g_SetNetShareSecurity )
  120. return g_SetNetShareSecurity(lpShareName, lpUsser, dwPermissions);
  121. else
  122. _tprintf_s(_T("加载SetNetShareSecurity函数失败\n"));
  123. return FALSE;
  124. }
  125. BOOL API_AddAccessRights(IN LPCTSTR lpszFileName, IN LPCTSTR lpszAccountName, IN const DWORD& dwAccessMask /* = STANDARD_RIGHTS_ALL| SPECIFIC_RIGHTS_ALL */ )
  126. {
  127. if ( g_AddAccessRights )
  128. return g_AddAccessRights(lpszFileName, lpszAccountName, dwAccessMask);
  129. else
  130. _tprintf_s(_T("加载AddAccessRights函数失败\n"));
  131. return FALSE;
  132. }
  133. };