Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ngscopeclient/scopehal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 984ed99a73c9
Choose a base ref
...
head repository: ngscopeclient/scopehal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3a71d729e0e8
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Jun 21, 2020

  1. Implemented plugin loading for Win32

    nshcat committed Jun 21, 2020
    Copy the full SHA
    1407c8d View commit details
  2. Merge pull request #158 from nshcat/win32-plugin-loader

    Implemented plugin loading for Win32
    azonenberg authored Jun 21, 2020
    Copy the full SHA
    3a71d72 View commit details
Showing with 104 additions and 1 deletion.
  1. +6 −1 scopehal/CMakeLists.txt
  2. +98 −0 scopehal/scopehal.cpp
7 changes: 6 additions & 1 deletion scopehal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -3,6 +3,11 @@ link_directories(${GTKMM_LIBRARY_DIRS} ${SIGCXX_LIBRARY_DIRS})

find_library(LXI_LIB lxi)

# Additional WinAPI libraries
if(WIN32)
set(WIN_LIBS shlwapi)
endif()

if(LXI_LIB)
set(HAS_LXI true)
set(LXI_LIBRARIES ${LXI_LIB})
@@ -49,7 +54,7 @@ set(SCOPEHAL_SOURCES

add_library(scopehal SHARED
${SCOPEHAL_SOURCES})
target_link_libraries(scopehal ${SIGCXX_LIBRARIES} ${GTKMM_LIBRARIES} xptools log graphwidget yaml-cpp ${LXI_LIBRARIES})
target_link_libraries(scopehal ${SIGCXX_LIBRARIES} ${GTKMM_LIBRARIES} xptools log graphwidget yaml-cpp ${LXI_LIBRARIES} ${WIN_LIBS})

target_include_directories(scopehal
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
98 changes: 98 additions & 0 deletions scopehal/scopehal.cpp
Original file line number Diff line number Diff line change
@@ -45,10 +45,14 @@

#ifndef _WIN32
#include <dlfcn.h>
#else
#include <windows.h>
#include <shlwapi.h>
#endif

using namespace std;


/**
@brief Static initialization for SCPI transports
*/
@@ -167,5 +171,99 @@ void InitializePlugins()

closedir(hdir);
}
#else
// Get path of process image
TCHAR binPath[MAX_PATH];

if( GetModuleFileName(NULL, binPath, MAX_PATH) == 0 )
{
LogError("Error: GetModuleFileName() failed.\n");
return;
}

// Remove file name from path
if( !PathRemoveFileSpec(binPath) )
{
LogError("Error: PathRemoveFileSpec() failed.\n");
return;
}

TCHAR searchPath[MAX_PATH];
if( PathCombine(searchPath, binPath, "plugins\\*.dll") == NULL )
{
LogError("Error: PathCombine() failed.\n");
return;
}

// For now, we only search in the folder that contains the binary.
WIN32_FIND_DATA findData;
HANDLE findHandle = INVALID_HANDLE_VALUE;

// First file entry
findHandle = FindFirstFile(searchPath, &findData);

// Is there at least one file?
if(findHandle == INVALID_HANDLE_VALUE)
{
return;
}

do
{
// Exclude directories
if(!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
auto fileName = findData.cFileName;
auto fileNameCStr = reinterpret_cast<const char*>(fileName);
auto extension = PathFindExtension(fileName);

// The file name does not contain the full path, which poses a problem since the file is
// located in the plugins subdirectory
TCHAR filePath[MAX_PATH];

if( PathCombine(filePath, "plugins", fileName) == NULL )
{
LogError("Error: PathCombine() failed.\n");
return;
}

// Try to open it as a library
auto module = LoadLibrary(filePath);

if(module != NULL)
{
// Try to retrieve plugin entry point address
auto procAddr = GetProcAddress(module, "PluginInit");

if(procAddr != NULL)
{
typedef void (*PluginInit)();
auto proc = reinterpret_cast<PluginInit>(procAddr);
proc();
}
else
{
LogWarning("Warning: Found plugin %s, but has no init symbol\n", fileNameCStr);
}

FreeLibrary(module);
}
else
{
LogWarning("Warning: Found plugin %s, but isn't valid library\n", fileNameCStr);
}
}
}
while(0 != FindNextFile(findHandle, &findData));

auto error = GetLastError();

if(error != ERROR_NO_MORE_FILES)
{
LogError("Error: Enumeration of plugin files failed.\n");
}

FindClose(findHandle);

#endif
}