# Specifying the VisionPro Install Location at Run Time

Add a new file startup.cpp to the project, with the following source code:

Copy

    #include "pch.h"

    // Refer https://stackoverflow.com/a/2390626
    #pragma section(".CRT$XCT", read)
    #define INITIALIZER(f)                                              \
      static void f(void);                                              \
      __declspec(allocate(".CRT$XCT")) void (*f##_)(void) = f;          \
      __pragma(comment(linker, "/include:" #f "_")) static void f(void)

    extern "C"
    {
      // This function will be called before any C++ static object constructors.
      INITIALIZER(initialize)
      {
        SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);

        // Specify the directories to delay-load DLLs from:
        // Option A: using hard-coded path and/or environment variable.
        std::array<WCHAR, MAX_PATH> programFilesPath{};
        GetEnvironmentVariable(L"ProgramW6432", programFilesPath.data(), programFilesPath.size());

        auto visionProPath = std::wstring(programFilesPath.data()) + L"\\Cognex\\VisionPro\\10.0 Preview 2\\bin";
        AddDllDirectory(visionProPath.c_str());

        auto eBusSdkPath = std::wstring(programFilesPath.data()) + L"\\Common Files\\Pleora\\eBUS SDK";
        AddDllDirectory(eBusSdkPath.c_str());

        // Option B: using custom registry key, e.g. HKEY_LOCAL_MACHINE\SOFTWARE\Key\SubKey@Value:
        // std::wstring visionProPath{};
        // HKEY hkey{};
        // if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Key\\SubKey", 0, KEY_READ, &hkey) == ERROR_SUCCESS)
        // {
        //   std::array<WCHAR, 1024> keyValue{};
        //   DWORD keyLen = keyValue.size();
        //   if (RegGetValue(hkey, nullptr, L"Value", RRF_RT_REG_SZ, nullptr, (LPBYTE)keyValue, &keyLen) == ERROR_SUCCESS)
        //   {
        //     visionProPath = std::wstring(keyValue) + L"\\Cognex\\VisionPro\\10.0 Preview 2\\bin";
        //   }
        //   RegCloseKey(hkey);
        // }
        //
        // if (!visionProPath.empty())
        // {
        //   AddDllDirectory(visionProPath.c_str());
        // }
        // else
        // {
        //   // Registry key not found, or another error occurred.
        // }
        //
        // // Repeat as above for eBusSdkPath.
      }
    }