VSTPlugin-win.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*****************************************************************************
  2. Copyright (C) 2016-2017 by Colin Edwards.
  3. Additional Code Copyright (C) 2016-2017 by c3r1c3 <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *****************************************************************************/
  15. #include "../headers/VSTPlugin.h"
  16. #include "../headers/vst-plugin-callbacks.hpp"
  17. #include <util/platform.h>
  18. #include <windows.h>
  19. AEffect *VSTPlugin::loadEffect()
  20. {
  21. AEffect *plugin = nullptr;
  22. wchar_t *wpath;
  23. os_utf8_to_wcs_ptr(pluginPath.c_str(), 0, &wpath);
  24. dllHandle = LoadLibraryW(wpath);
  25. bfree(wpath);
  26. if (dllHandle == nullptr) {
  27. DWORD errorCode = GetLastError();
  28. // Display the error message and exit the process
  29. if (errorCode == ERROR_BAD_EXE_FORMAT) {
  30. blog(LOG_WARNING, "Could not open library, "
  31. "wrong architecture.");
  32. } else {
  33. blog(LOG_WARNING,
  34. "Failed trying to load VST from '%s'"
  35. ", error %d\n",
  36. pluginPath.c_str(), GetLastError());
  37. }
  38. return nullptr;
  39. }
  40. vstPluginMain mainEntryPoint =
  41. (vstPluginMain)GetProcAddress(dllHandle, "VSTPluginMain");
  42. if (mainEntryPoint == nullptr) {
  43. mainEntryPoint = (vstPluginMain)GetProcAddress(
  44. dllHandle, "VstPluginMain()");
  45. }
  46. if (mainEntryPoint == nullptr) {
  47. mainEntryPoint =
  48. (vstPluginMain)GetProcAddress(dllHandle, "main");
  49. }
  50. if (mainEntryPoint == nullptr) {
  51. blog(LOG_WARNING, "Couldn't get a pointer to plug-in's main()");
  52. return nullptr;
  53. }
  54. // Instantiate the plug-in
  55. try {
  56. plugin = mainEntryPoint(hostCallback_static);
  57. } catch (...) {
  58. blog(LOG_WARNING, "VST plugin initialization failed");
  59. return nullptr;
  60. }
  61. if (plugin == nullptr) {
  62. blog(LOG_WARNING, "Couldn't create instance for '%s'",
  63. pluginPath.c_str());
  64. return nullptr;
  65. }
  66. plugin->user = this;
  67. return plugin;
  68. }
  69. void VSTPlugin::unloadLibrary()
  70. {
  71. if (dllHandle) {
  72. FreeLibrary(dllHandle);
  73. dllHandle = nullptr;
  74. }
  75. }
  76. bool VSTPlugin::vstLoaded()
  77. {
  78. return (dllHandle != nullptr);
  79. }