VSTPlugin-win.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 = (vstPluginMain)GetProcAddress(dllHandle, "VSTPluginMain");
  41. if (mainEntryPoint == nullptr) {
  42. mainEntryPoint = (vstPluginMain)GetProcAddress(dllHandle, "VstPluginMain()");
  43. }
  44. if (mainEntryPoint == nullptr) {
  45. mainEntryPoint = (vstPluginMain)GetProcAddress(dllHandle, "main");
  46. }
  47. if (mainEntryPoint == nullptr) {
  48. blog(LOG_WARNING, "Couldn't get a pointer to plug-in's main()");
  49. return nullptr;
  50. }
  51. // Instantiate the plug-in
  52. try {
  53. plugin = mainEntryPoint(hostCallback_static);
  54. } catch (...) {
  55. blog(LOG_WARNING, "VST plugin initialization failed");
  56. return nullptr;
  57. }
  58. if (plugin == nullptr) {
  59. blog(LOG_WARNING, "Couldn't create instance for '%s'", pluginPath.c_str());
  60. return nullptr;
  61. }
  62. plugin->user = this;
  63. return plugin;
  64. }
  65. void VSTPlugin::unloadLibrary()
  66. {
  67. if (dllHandle) {
  68. FreeLibrary(dllHandle);
  69. dllHandle = nullptr;
  70. }
  71. }
  72. bool VSTPlugin::vstLoaded()
  73. {
  74. return (dllHandle != nullptr);
  75. }