#ifndef ANSOCRWRAPPER_H
#define ANSOCRWRAPPER
#define ANSOCR_API __declspec(dllexport)
#pragma once
#include <iostream>
#include <Windows.h>

class OCRHandler {
public:
    OCRHandler(const char* dllName, const char* functionName)
        : dllName_(dllName), functionName_(functionName), hDll_(nullptr) {}

    ~OCRHandler() {
        if (hDll_ != nullptr) {
            FreeLibrary(hDll_);
        }
    }

    template <typename ReturnType, typename... Args>
    ReturnType CallFunction(Args... args) {
        // Load the DLL
        if (hDll_ == nullptr) {
            hDll_ = LoadLibrary(dllName_);
            if (hDll_ == nullptr) {
                std::cerr << "Failed to load DLL: " << dllName_ << std::endl;
                return ReturnType(); // Default-constructed return value
            }
        }

        // If the DLL is loaded successfully, get the function address
        using FunctionType = ReturnType(*)(Args...);
        FunctionType myFunction = (FunctionType)GetProcAddress(hDll_, functionName_);

        if (myFunction != nullptr) {
            // Call the function from the DLL with arguments
            return myFunction(args...);
        }
        else {
            std::cerr << "Failed to get function address from DLL: " << functionName_ << std::endl;
            return ReturnType(); // Default-constructed return value
        }
    }

private:
    const char* dllName_;
    const char* functionName_;
    HMODULE hDll_;
};

//int main() {
//    // Create an instance of DLLHandler
//    OCRHandler dllHandler("YourDLLName.dll", "YourFunctionWithArgsAndReturnValue");
//
//    // Call the function with arguments and a return value
//    int arg1 = 5;
//    double arg2 = 3.14;
//    double result = dllHandler.CallFunction<double>(arg1, arg2);
//
//    std::cout << "Result from DLL function: " << result << std::endl;
//
//    return 0;
//}

#endif