// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#include "ANSUtilities.h"
#include <cstdint>
#include <unordered_map>
#include <condition_variable>
#include <mutex>

// ── ANSUtilities handle registry ──
static std::unordered_map<ANSCENTER::ANSUtilities*, int>& UtilHandleRegistry() {
    static std::unordered_map<ANSCENTER::ANSUtilities*, int> s;
    return s;
}
static std::mutex& UtilHandleRegistryMutex() {
    static std::mutex m;
    return m;
}
static std::condition_variable& UtilHandleRegistryCV() {
    static std::condition_variable cv;
    return cv;
}

static void RegisterUtilHandle(ANSCENTER::ANSUtilities* h) {
    std::lock_guard<std::mutex> lk(UtilHandleRegistryMutex());
    UtilHandleRegistry()[h] = 1;
}

static ANSCENTER::ANSUtilities* AcquireUtilHandle(ANSCENTER::ANSUtilities* h) {
    std::lock_guard<std::mutex> lk(UtilHandleRegistryMutex());
    auto it = UtilHandleRegistry().find(h);
    if (it == UtilHandleRegistry().end()) return nullptr;
    it->second++;
    return h;
}

static bool ReleaseUtilHandleRef(ANSCENTER::ANSUtilities* h) {
    std::lock_guard<std::mutex> lk(UtilHandleRegistryMutex());
    auto it = UtilHandleRegistry().find(h);
    if (it == UtilHandleRegistry().end()) return false;
    it->second--;
    if (it->second <= 0) {
        UtilHandleRegistry().erase(it);
        UtilHandleRegistryCV().notify_all();
        return true;
    }
    return false;
}

static bool UnregisterUtilHandle(ANSCENTER::ANSUtilities* h) {
    std::unique_lock<std::mutex> lk(UtilHandleRegistryMutex());
    auto it = UtilHandleRegistry().find(h);
    if (it == UtilHandleRegistry().end()) return false;
    it->second--;
    bool ok = UtilHandleRegistryCV().wait_for(lk, std::chrono::seconds(30), [&]() {
        auto it2 = UtilHandleRegistry().find(h);
        return it2 == UtilHandleRegistry().end() || it2->second <= 0;
    });
    if (!ok) {
        OutputDebugStringA("WARNING: UnregisterUtilHandle timed out waiting for in-flight operations\n");
    }
    UtilHandleRegistry().erase(h);
    return true;
}

class UtilHandleGuard {
    ANSCENTER::ANSUtilities* engine;
public:
    explicit UtilHandleGuard(ANSCENTER::ANSUtilities* e) : engine(e) {}
    ~UtilHandleGuard() { if (engine) ReleaseUtilHandleRef(engine); }
    ANSCENTER::ANSUtilities* get() const { return engine; }
    explicit operator bool() const { return engine != nullptr; }
    UtilHandleGuard(const UtilHandleGuard&) = delete;
    UtilHandleGuard& operator=(const UtilHandleGuard&) = delete;
};

// ── ANSAWSS3 handle registry ──
static std::unordered_map<ANSCENTER::ANSAWSS3*, int>& AWSHandleRegistry() {
    static std::unordered_map<ANSCENTER::ANSAWSS3*, int> s;
    return s;
}
static std::mutex& AWSHandleRegistryMutex() {
    static std::mutex m;
    return m;
}
static std::condition_variable& AWSHandleRegistryCV() {
    static std::condition_variable cv;
    return cv;
}

static void RegisterAWSHandle(ANSCENTER::ANSAWSS3* h) {
    std::lock_guard<std::mutex> lk(AWSHandleRegistryMutex());
    AWSHandleRegistry()[h] = 1;
}

static ANSCENTER::ANSAWSS3* AcquireAWSHandle(ANSCENTER::ANSAWSS3* h) {
    std::lock_guard<std::mutex> lk(AWSHandleRegistryMutex());
    auto it = AWSHandleRegistry().find(h);
    if (it == AWSHandleRegistry().end()) return nullptr;
    it->second++;
    return h;
}

static bool ReleaseAWSHandleRef(ANSCENTER::ANSAWSS3* h) {
    std::lock_guard<std::mutex> lk(AWSHandleRegistryMutex());
    auto it = AWSHandleRegistry().find(h);
    if (it == AWSHandleRegistry().end()) return false;
    it->second--;
    if (it->second <= 0) {
        AWSHandleRegistry().erase(it);
        AWSHandleRegistryCV().notify_all();
        return true;
    }
    return false;
}

static bool UnregisterAWSHandle(ANSCENTER::ANSAWSS3* h) {
    std::unique_lock<std::mutex> lk(AWSHandleRegistryMutex());
    auto it = AWSHandleRegistry().find(h);
    if (it == AWSHandleRegistry().end()) return false;
    it->second--;
    bool ok = AWSHandleRegistryCV().wait_for(lk, std::chrono::seconds(120), [&]() {
        auto it2 = AWSHandleRegistry().find(h);
        return it2 == AWSHandleRegistry().end() || it2->second <= 0;
    });
    if (!ok) {
        OutputDebugStringA("WARNING: UnregisterAWSHandle timed out waiting for in-flight operations\n");
    }
    AWSHandleRegistry().erase(h);
    return true;
}

class AWSHandleGuard {
    ANSCENTER::ANSAWSS3* engine;
public:
    explicit AWSHandleGuard(ANSCENTER::ANSAWSS3* e) : engine(e) {}
    ~AWSHandleGuard() { if (engine) ReleaseAWSHandleRef(engine); }
    ANSCENTER::ANSAWSS3* get() const { return engine; }
    explicit operator bool() const { return engine != nullptr; }
    AWSHandleGuard(const AWSHandleGuard&) = delete;
    AWSHandleGuard& operator=(const AWSHandleGuard&) = delete;
};

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

extern "C" ANSULT_API int  CreateANSUtilityHandle(ANSCENTER::ANSUtilities** Handle, const char* licenseKey) {
	if (Handle == nullptr || licenseKey == nullptr) return 0;
	if (*Handle) {
		if (UnregisterUtilHandle(*Handle)) {
			delete *Handle;
		}
		*Handle = nullptr;
	}
	try {
		*Handle = new ANSCENTER::ANSUtilities();
		if (*Handle == nullptr) return 0;
		if ((*Handle)->Initialize(licenseKey)) {
			RegisterUtilHandle(*Handle);
			return 1;
		}
		// Initialize failed - clean up to prevent memory leak
		delete *Handle;
		*Handle = nullptr;
		return 0;
	}
	catch (std::exception& e) {
		// Clean up on exception to prevent memory leak
		if (*Handle != nullptr) {
			delete *Handle;
			*Handle = nullptr;
		}
		return 0;
	}
	catch (...) {
		if (*Handle != nullptr) {
			delete *Handle;
			*Handle = nullptr;
		}
		return 0;
	}

}
static int ReleaseANSUtilityHandle_Impl(ANSCENTER::ANSUtilities** Handle) {
    try {
        if (!Handle || !*Handle) return 1;
        if (!UnregisterUtilHandle(*Handle)) {
            *Handle = nullptr;
            return 1;
        }
        delete *Handle;
        *Handle = nullptr;
        return 1;
    }
    catch (...) {
        if (Handle) *Handle = nullptr;
        return 0;
    }
}

extern "C" ANSULT_API int  ReleaseANSUtilityHandle(ANSCENTER::ANSUtilities** Handle) {
    __try {
        return ReleaseANSUtilityHandle_Impl(Handle);
    }
    __except (EXCEPTION_EXECUTE_HANDLER) {
        if (Handle) *Handle = nullptr;
        return 0;
    }
}
extern "C" ANSULT_API int  GetFCMAccessToken(ANSCENTER::ANSUtilities** Handle,const char* privateKey, LStrHandle accessToken) {
	if (Handle == nullptr || *Handle == nullptr || privateKey == nullptr || accessToken == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->GetFirebaseCloudMessageAcccessToken(privateKey);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(accessToken, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*accessToken)->cnt = size;
			memcpy((*accessToken)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  CreateAWSSNSTopic(ANSCENTER::ANSUtilities** Handle, const char* snsTopicName, LStrHandle arnTopic) {
	if (Handle == nullptr || *Handle == nullptr || snsTopicName == nullptr || arnTopic == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->CreateAWSSNSTopic(snsTopicName);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(arnTopic, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*arnTopic)->cnt = size;
			memcpy((*arnTopic)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  GetFCMAccessTokenCpp(ANSCENTER::ANSUtilities** Handle, const char* privateKey, std::string& accessToken) {
	if (Handle == nullptr || *Handle == nullptr || privateKey == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		accessToken = guard.get()->GetFirebaseCloudMessageAcccessToken(privateKey);
		if (accessToken.empty() || accessToken.find("Error:") != std::string::npos) {
			return 0;
		}
		else {
			return 1;
		}
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;

	}
}
extern "C" ANSULT_API int  CreateAWSSNSTopicCpp(ANSCENTER::ANSUtilities** Handle, const char* snsTopicName, std::string& arnTopic) {
	if (Handle == nullptr || *Handle == nullptr || snsTopicName == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		arnTopic = guard.get()->CreateAWSSNSTopic(snsTopicName);
		if (arnTopic.empty() || arnTopic.find("Error:") != std::string::npos) {
			return 0;
		}
		else {
			return 1;
		}
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  DeleteAWSSNSTopic(ANSCENTER::ANSUtilities** Handle, const char* arnTopic) {
	if (Handle == nullptr || *Handle == nullptr || arnTopic == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->DeleteAWSSNSTopic(arnTopic)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SubcribeSMSPhoneNumberAWSSNSTopic(ANSCENTER::ANSUtilities** Handle, const char* snsTopicName, const char* phoneNumber, LStrHandle subscribedARN) {
	if (Handle == nullptr || *Handle == nullptr || snsTopicName == nullptr || phoneNumber == nullptr || subscribedARN == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->SubcribeSMSPhoneNumber(snsTopicName, phoneNumber);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(subscribedARN, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*subscribedARN)->cnt = size;
			memcpy((*subscribedARN)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}

}
extern "C" ANSULT_API int  SubcribeEmailAddressAWSSNSTopic(ANSCENTER::ANSUtilities** Handle, const char* snsTopicName, const char* emailAddress, LStrHandle subscribedARN) {
	if (Handle == nullptr || *Handle == nullptr || snsTopicName == nullptr || emailAddress == nullptr || subscribedARN == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->SubscribeEmailAddress(snsTopicName, emailAddress);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(subscribedARN, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*subscribedARN)->cnt = size;
			memcpy((*subscribedARN)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SendMessageToAWSSNSTopic(ANSCENTER::ANSUtilities** Handle, const char* snsTopicName, const char* subjectContent, const char* messageContent, LStrHandle messageId) {
	if (Handle == nullptr || *Handle == nullptr || snsTopicName == nullptr || subjectContent == nullptr || messageContent == nullptr || messageId == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->SendMessageToTopic(snsTopicName, subjectContent, messageContent);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(messageId, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*messageId)->cnt = size;
			memcpy((*messageId)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SendMessageToPhoneNumber(ANSCENTER::ANSUtilities** Handle, const char* phoneNumber, const char* messageContent, LStrHandle messageId) {
	if (Handle == nullptr || *Handle == nullptr || phoneNumber == nullptr || messageContent == nullptr || messageId == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->SendMessageToPhoneNumber(phoneNumber, messageContent);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(messageId, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*messageId)->cnt = size;
			memcpy((*messageId)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SubcribeSMSPhoneNumberAWSSNSTopicCpp(ANSCENTER::ANSUtilities** Handle, const char* snsTopicName, const char* phoneNumber, std::string& subscribedARN) {
	if (Handle == nullptr || *Handle == nullptr || snsTopicName == nullptr || phoneNumber == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		subscribedARN = guard.get()->SubcribeSMSPhoneNumber(snsTopicName, phoneNumber);
		if (subscribedARN.empty() || subscribedARN.find("Error:") != std::string::npos) {
			return 0;
		}
		else {
			return 1;
		}
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SubcribeEmailAddressAWSSNSTopicCpp(ANSCENTER::ANSUtilities** Handle, const char* snsTopicName, const char* emailAddress, std::string& subscribedARN) {
	if (Handle == nullptr || *Handle == nullptr || snsTopicName == nullptr || emailAddress == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		subscribedARN = guard.get()->SubscribeEmailAddress(snsTopicName, emailAddress);
		if (subscribedARN.empty() || subscribedARN.find("Error:") != std::string::npos) {
			return 0;
		}
		else {
			return 1;
		}
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SendMessageToAWSSNSTopicCpp(ANSCENTER::ANSUtilities** Handle, const char* snsTopicName, const char* subjectContent, const char* messageContent, std::string& messageId) {
	if (Handle == nullptr || *Handle == nullptr || snsTopicName == nullptr || subjectContent == nullptr || messageContent == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		messageId = guard.get()->SendMessageToTopic(snsTopicName, subjectContent, messageContent);
		if (messageId.empty() || messageId.find("Error:") != std::string::npos) {
			return 0;
		}
		else {
			return 1;
		}
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SendMessageToPhoneNumberCpp(ANSCENTER::ANSUtilities** Handle, const char* phoneNumber, const char* messageContent, std::string& messageId) {
	if (Handle == nullptr || *Handle == nullptr || phoneNumber == nullptr || messageContent == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		messageId = guard.get()->SendMessageToPhoneNumber(phoneNumber, messageContent);
		if (messageId.empty() || messageId.find("Error:") != std::string::npos) {
			return 0;
		}
		else {
			return 1;
		}
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  ListASWTopics(ANSCENTER::ANSUtilities** Handle, LStrHandle arnTopics) {
	if (Handle == nullptr || *Handle == nullptr || arnTopics == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->ListAWSSNSTopic();
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(arnTopics, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*arnTopics)->cnt = size;
			memcpy((*arnTopics)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  AESEncryption(const char* inputString, const char* inputKey, LStrHandle encryptionMessage) {
	if (inputString == nullptr || inputKey == nullptr || encryptionMessage == nullptr) return 0;
	try {
		std::string st = ANSCENTER::ANSUtilities::AESEncryption(inputString, inputKey);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(encryptionMessage, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*encryptionMessage)->cnt = size;
			memcpy((*encryptionMessage)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}

}
extern "C" ANSULT_API int  MD5HashFile(const char* filePath, LStrHandle decryptionMessage)
{
	if (filePath == nullptr || decryptionMessage == nullptr) return 0;
	try {
		std::string st = ANSCENTER::ANSUtilities::MD5HashFile(filePath);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(decryptionMessage, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*decryptionMessage)->cnt = size;
			memcpy((*decryptionMessage)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  AESDecryption(const char* encryptedString, const char* inputKey, LStrHandle decryptionMessage) {
	if (encryptedString == nullptr || inputKey == nullptr || decryptionMessage == nullptr) return 0;
	try {
		std::string st = ANSCENTER::ANSUtilities::AESDecryption(encryptedString, inputKey);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(decryptionMessage, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*decryptionMessage)->cnt = size;
			memcpy((*decryptionMessage)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}

}
extern "C" ANSULT_API int  ListASWTopicsCpp(ANSCENTER::ANSUtilities** Handle, std::string& arnTopics) {
	if (Handle == nullptr || *Handle == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		arnTopics = guard.get()->ListAWSSNSTopic();
		if (arnTopics.empty() || arnTopics.find("Error:") != std::string::npos) {
			return 0;
		}
		else {
			return 1;
		}
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  AESEncryptionCpp(const char* inputString, const char* inputKey, std::string& encryptedString) {
	if (inputString == nullptr || inputKey == nullptr) return 0;
	try {
		encryptedString = ANSCENTER::ANSUtilities::AESEncryption(inputString, inputKey);
		if (encryptedString.empty() || encryptedString.find("Error:") != std::string::npos) {
			return 0;
		}
		else {
			return 1;
		}
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  AESDecryptionCpp(const char* encryptedString, const char* inputKey, std::string& decryptionMessage) {
	if (encryptedString == nullptr || inputKey == nullptr) return 0;
	try {
		decryptionMessage = ANSCENTER::ANSUtilities::AESDecryption(encryptedString, inputKey);
		if (decryptionMessage.empty() || decryptionMessage.find("Error:") != std::string::npos) {
			return 0;
		}
		else {
			return 1;
		}
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  AuthenticateGCS(ANSCENTER::ANSUtilities** Handle, const char* jsonKeyString) {
	if (Handle == nullptr || *Handle == nullptr || jsonKeyString == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->AuthenticateGCS(jsonKeyString)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadMatToGCS(ANSCENTER::ANSUtilities** Handle, const char* bucketName, const char* objectName, unsigned char* jpeg_string, int32 bufferLength) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || objectName == nullptr || jpeg_string == nullptr || bufferLength <= 0) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		//cv::Mat image = cv::imdecode(cv::Mat(1, bufferLength, CV_8U, jpeg_string), cv::IMREAD_COLOR);
		//if (image.empty()) return 0;
		//if (guard.get()->UploadMatToGCS(bucketName, objectName, image)) return 1;
		//else return 0;
		if (guard.get()->UploadMatToGCS(bucketName, objectName, jpeg_string, bufferLength)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadImageToGCS(ANSCENTER::ANSUtilities** Handle, const char* bucketName, const char* objectName, cv::Mat image) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || objectName == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		if (image.empty()) return 0;
		if (guard.get()->UploadMatToGCS(bucketName, objectName, image)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SetupServerProxy(ANSCENTER::ANSUtilities** Handle, const char* hostName, int port, const char* userName, const char* passWord) {
	if (Handle == nullptr || *Handle == nullptr || hostName == nullptr || userName == nullptr || passWord == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->SetServerProxy(hostName, port, userName, passWord)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SendEmail(const char* smtpServer, int port,
									const char* userName, const char* password,
									const char* title,
									const char* bodyHTMLContent,
									const char* bodyTextContent,
									const char* fromEmailSender,
									const char* toEmails,
									const char* ccEmails,
									const char* bccEmails)
{
	if (smtpServer == nullptr || userName == nullptr || password == nullptr ||
		title == nullptr || fromEmailSender == nullptr) return 0;
	try {
		std::vector<std::string> addToEmails;
		std::vector<std::string> addCCEmails;
		std::vector<std::string> addBCCEmails;
		// convert to vectors of strings from semicolon separated strings
		if (toEmails != nullptr && strlen(toEmails) > 0) {
			std::string s = toEmails;
			size_t pos = 0;
			std::string token;
			while ((pos = s.find(';')) != std::string::npos) {
				token = s.substr(0, pos);
				addToEmails.push_back(token);
				s.erase(0, pos + 1);
			}
			if (!s.empty()) addToEmails.push_back(s);
		}
		if (ccEmails != nullptr && strlen(ccEmails) > 0) {
			std::string s = ccEmails;
			size_t pos = 0;
			std::string token;
			while ((pos = s.find(';')) != std::string::npos) {
				token = s.substr(0, pos);
				addCCEmails.push_back(token);
				s.erase(0, pos + 1);
			}
			if (!s.empty()) addCCEmails.push_back(s);
		}
		if (bccEmails != nullptr && strlen(bccEmails) > 0) {
			std::string s = bccEmails;
			size_t pos = 0;
			std::string token;
			while ((pos = s.find(';')) != std::string::npos) {
				token = s.substr(0, pos);
				addBCCEmails.push_back(token);
				s.erase(0, pos + 1);
			}
			if (!s.empty()) addBCCEmails.push_back(s);
		}
		if (ANSCENTER::ANSUtilities::SendEmail(smtpServer, port, userName, password, title, bodyHTMLContent, bodyTextContent, fromEmailSender,
								addToEmails, addCCEmails, addBCCEmails)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}


}

extern "C" ANSULT_API int  RebootSystem() {
	try {
		if (ANSCENTER::ANSUtilities::RestartPC()) return 1;
		else return 0;
	}
	catch (...) {
		return 0;
	}
}




// AWSS3 
extern "C" ANSULT_API int  CreateANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* licenseKey) {
	if (Handle == nullptr || licenseKey == nullptr) return 0;
	if (*Handle) {
		if (UnregisterAWSHandle(*Handle)) {
			delete *Handle;
		}
		*Handle = nullptr;
	}
	try {
		*Handle = new ANSCENTER::ANSAWSS3();
		if (*Handle == nullptr) return 0;
		if ((*Handle)->Initialize(licenseKey)) {
			RegisterAWSHandle(*Handle);
			return 1;
		}
		// Initialize failed - clean up to prevent memory leak
		delete *Handle;
		*Handle = nullptr;
		return 0;
	}
	catch (std::exception& e) {
		// Clean up on exception to prevent memory leak
		if (*Handle != nullptr) {
			delete *Handle;
			*Handle = nullptr;
		}
		return 0;
	}
	catch (...) {
		if (*Handle != nullptr) {
			delete *Handle;
			*Handle = nullptr;
		}
		return 0;
	}
}
static int ReleaseANSAWSHandle_Impl(ANSCENTER::ANSAWSS3** Handle) {
    try {
        if (!Handle || !*Handle) return 1;
        if (!UnregisterAWSHandle(*Handle)) {
            *Handle = nullptr;
            return 1;
        }
        delete *Handle;
        *Handle = nullptr;
        return 1;
    }
    catch (...) {
        if (Handle) *Handle = nullptr;
        return 0;
    }
}

extern "C" ANSULT_API int  ReleaseANSAWSHandle(ANSCENTER::ANSAWSS3** Handle) {
    __try {
        return ReleaseANSAWSHandle_Impl(Handle);
    }
    __except (EXCEPTION_EXECUTE_HANDLER) {
        if (Handle) *Handle = nullptr;
        return 0;
    }
}
extern "C" ANSULT_API int  ConnectANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* baseDomain, const char* bucketRegion, const char* serviceName, int port, int bTls, int autoReconnect, int* awsPath) {
	if (Handle == nullptr || *Handle == nullptr || awsPath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		bool bTlsBool = (bTls == 1);
		bool autoReconnectBool = (autoReconnect == 1);
		bool awsPathBool = true;
		int result = guard.get()->Connect(baseDomain, bucketRegion, serviceName, port, bTlsBool, autoReconnectBool, awsPathBool);
		*awsPath = awsPathBool ? 1 : 0;
		return result;  // 1 = connected, 0 = failed, 2 = no internet (retrying)
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SetProxyANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* proxyHost, int proxyPort, const char* proxyUsername, const char* proxyPassword) {
	if (Handle == nullptr || *Handle == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->SetServerProxy(proxyHost, proxyPort, proxyUsername, proxyPassword)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  SetAuthenticationANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* accessKey, const char* secretKey) {
	if (Handle == nullptr || *Handle == nullptr ) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->SetAuthentication(accessKey, secretKey)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}

extern "C" ANSULT_API int  ListBucketANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, std::string& bucketList) {
	try {
		// Validate input parameters
		if (Handle == nullptr || *Handle == nullptr) {
			return 0;
		}
		AWSHandleGuard guard(AcquireAWSHandle(*Handle));
		if (!guard) return 0;

		std::vector<std::string> bucketListVec = guard.get()->ListBuckets();

		// Clear the output string first
		bucketList.clear();

		// Return 0 if no buckets found (or you could return 1 with empty string)
		if (bucketListVec.empty()) {
			return 1; // Changed to 1 - successfully got empty list
		}

		// Build the bucket list as a single string with semicolons
		// Reserve approximate space to avoid multiple reallocations
		size_t estimatedSize = 0;
		for (const auto& bucket : bucketListVec) {
			estimatedSize += bucket.length() + 1; // +1 for semicolon
		}
		bucketList.reserve(estimatedSize);

		// Concatenate bucket names
		for (const auto& bucket : bucketListVec) {
			bucketList += bucket + ";";
		}

		// Optional: Remove trailing semicolon
		if (!bucketList.empty() && bucketList.back() == ';') {
			bucketList.pop_back();
		}

		return 1;
	}
	catch (const std::exception& e) {
		// Optional: Log the error if you have access to logger
		// Could also set bucketList to error message if needed
		bucketList.clear();
		return 0;
	}
	catch (...) {
		bucketList.clear();
		return 0;
	}
}
extern "C" ANSULT_API int  ListBucketObjectsANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, std::string& bucketNameList) {
	try {
		// Validate input parameters
		if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr) {
			return 0;
		}
		AWSHandleGuard guard(AcquireAWSHandle(*Handle));
		if (!guard) return 0;

		std::vector<std::string> bucketObjects = guard.get()->ListBucketObjects(bucketName);
		if (bucketObjects.empty()) {
			return 1; // Successfully got empty list
		}

		// Build the bucket object list as a single string with semicolons
		bucketNameList.clear();
		for (const auto& object : bucketObjects) {
			bucketNameList += object + ";";
		}

		// Optional: Remove trailing semicolon
		if (!bucketNameList.empty() && bucketNameList.back() == ';') {
			bucketNameList.pop_back();
		}

		return 1;
	}
	catch (const std::exception& e) {
		// Optional: Log the error if you have access to logger
		// Could also set bucketNameList to error message if needed
		bucketNameList.clear();
		return 0;
	}
	catch (...) {
		bucketNameList.clear();
		return 0;
	}
}
extern "C" ANSULT_API int  ListBucketObjectsWithPrefixANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, std::string& bucketNameList) {
	try {
		// Validate input parameters
		if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr) {
			return 0;
		}
		AWSHandleGuard guard(AcquireAWSHandle(*Handle));
		if (!guard) return 0;

		std::vector<std::string> bucketObjects = guard.get()->ListBucketObjectsWithPrefix(bucketName, prefix);
		if (bucketObjects.empty()) {
			return 1; // Successfully got empty list
		}

		// Build the bucket object list as a single string with semicolons
		bucketNameList.clear();
		for (const auto& object : bucketObjects) {
			bucketNameList += object + ";";
		}

		// Optional: Remove trailing semicolon
		if (!bucketNameList.empty() && bucketNameList.back() == ';') {
			bucketNameList.pop_back();
		}

		return 1;
	}
	catch (const std::exception& e) {
		// Optional: Log the error if you have access to logger
		// Could also set bucketNameList to error message if needed
		bucketNameList.clear();
		return 0;
	}
	catch (...) {
		bucketNameList.clear();
		return 0;
	}
}
extern "C" ANSULT_API int  GetRegionANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, std::string& region) {
	try {
		// Validate input parameters
		if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr) {
			return 0;
		}
		AWSHandleGuard guard(AcquireAWSHandle(*Handle));
		if (!guard) return 0;

		region = guard.get()->GetBucketRegion(bucketName);
		if (region.empty()) {
			return 1; // Successfully got empty region
		}

		return 1;
	}
	catch (const std::exception& e) {
		// Optional: Log the error if you have access to logger
		// Could also set region to error message if needed
		region.clear();
		return 0;
	}
	catch (...) {
		region.clear();
		return 0;
	}
}

extern "C" ANSULT_API int  ListBucketANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, LStrHandle bucketList) {
	if (Handle == nullptr || *Handle == nullptr) {
		return 0;
	}
	try {
		std::string st;
		ListBucketANSAWSHandle_CPP(Handle, st);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(bucketList, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*bucketList)->cnt = size;
			memcpy((*bucketList)->str, st.c_str(), size);
			return 1;
		}
		else return 0;

	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  ListBucketObjectsANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, LStrHandle bucketNameList) {
	if (Handle == nullptr || *Handle == nullptr) {
		return 0;
	}
	try {
		std::string st;
		ListBucketObjectsANSAWSHandle_CPP(Handle, bucketName,st);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(bucketNameList, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*bucketNameList)->cnt = size;
			memcpy((*bucketNameList)->str, st.c_str(), size);
			return 1;
		}
		else return 0;

	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  ListBucketObjectsWithPrefixANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, LStrHandle bucketNameList) {
	if (Handle == nullptr || *Handle == nullptr) {
		return 0;
	}
	try {
		std::string st;
		ListBucketObjectsWithPrefixANSAWSHandle_CPP(Handle, bucketName, prefix,st);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(bucketNameList, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*bucketNameList)->cnt = size;
			memcpy((*bucketNameList)->str, st.c_str(), size);
			return 1;
		}
		else return 0;

	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}

extern "C" ANSULT_API int  GetRegionANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, LStrHandle region) {
		if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || region == nullptr) {
		return 0;
	}
	try {
		std::string st;
		GetRegionANSAWSHandle_CPP(Handle, bucketName, st);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(region, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr)
		{
			(*region)->cnt = size;
			memcpy((*region)->str, st.c_str(), size);
			return 1;
		}
		else return 0;

	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}

extern "C" ANSULT_API int  CreateBucketANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->CreateBucket(bucketName)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  DeleteBucketANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->DeleteBucket(bucketName)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}

}
extern "C" ANSULT_API int  CreateBucketPrefixANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->CreateFolder(bucketName,prefix)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  DeleteBucketPrefixANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->DeleteFolder(bucketName, prefix)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  DeleteBucketObjectANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* bucketObject) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->DeleteBucketObject(bucketName, bucketObject)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadTextDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* textDataPath, LStrHandle uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || textDataPath == nullptr || uploadedFilePath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadTextData(bucketName, textDataPath, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* binaryDataPath, LStrHandle uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || binaryDataPath == nullptr || uploadedFilePath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadBinaryData(bucketName, binaryDataPath, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadFileStreamDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, LStrHandle uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || fileDataPath == nullptr || uploadedFilePath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadFileStream(bucketName, fileDataPath, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadMultiPartDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, int fileSize, LStrHandle uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || fileDataPath == nullptr || uploadedFilePath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadMultipartData(bucketName, fileDataPath, outPath, fileSize)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  DownloadFileStreamANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* objectName, const char* savedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || objectName == nullptr || savedFilePath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		if (guard.get()->DownloadFile(bucketName, objectName, savedFilePath)) return 1;
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadJpegImageANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, LStrHandle uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr || uploadedFilePath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadJpegImage(bucketName, jpeg_string, bufferLength, fileName, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadPrefixJpegImageANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, LStrHandle uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr || uploadedFilePath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadPrefixMultiPartDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* fileDataPath, const char* objectName, int fileSize, LStrHandle uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || fileDataPath == nullptr || fileSize <= 0 || uploadedFilePath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, outPath, fileSize)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}
extern "C" ANSULT_API int  UploadPrefixBinaryDataANSAWSHandle(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, LStrHandle uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || binaryDataPath == nullptr || objectName == nullptr || uploadedFilePath == nullptr) {
		return 0;
	}
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) {
		return 0;
	}
	catch (...) {
		return 0;
	}
}

// ── C++ test APIs (std::string& instead of LStrHandle) ──

extern "C" ANSULT_API int UploadTextDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* textDataPath, std::string& uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || textDataPath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		return guard.get()->UploadTextData(bucketName, textDataPath, uploadedFilePath) ? 1 : 0;
	} catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* binaryDataPath, std::string& uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || binaryDataPath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		return guard.get()->UploadBinaryData(bucketName, binaryDataPath, uploadedFilePath) ? 1 : 0;
	} catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, std::string& uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || fileDataPath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		return guard.get()->UploadFileStream(bucketName, fileDataPath, uploadedFilePath) ? 1 : 0;
	} catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* fileDataPath, int fileSize, std::string& uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || fileDataPath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		return guard.get()->UploadMultipartData(bucketName, fileDataPath, uploadedFilePath, fileSize) ? 1 : 0;
	} catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadJpegImageANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, std::string& uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		return guard.get()->UploadJpegImage(bucketName, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : 0;
	} catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, std::string& uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || binaryDataPath == nullptr || objectName == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		return guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, uploadedFilePath) ? 1 : 0;
	} catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, const char* fileDataPath, const char* objectName, int fileSize, std::string& uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || fileDataPath == nullptr || fileSize <= 0) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		return guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, uploadedFilePath, fileSize) ? 1 : 0;
	} catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_CPP(ANSCENTER::ANSAWSS3** Handle, const char* bucketName, const char* prefix, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, std::string& uploadedFilePath) {
	if (Handle == nullptr || *Handle == nullptr || bucketName == nullptr || prefix == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(*Handle));
	if (!guard) return 0;
	try {
		return guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, uploadedFilePath) ? 1 : 0;
	} catch (...) { return 0; }
}

// ════════════════════════════════════════════════════════════════════════════
// V2 API — handle-by-value for LabVIEW (uint64_t instead of Handle**)
// ════════════════════════════════════════════════════════════════════════════

// ── ANSUtilities V2 ──

extern "C" ANSULT_API int GetFCMAccessToken_V2(uint64_t handleVal, const char* privateKey, LStrHandle accessToken) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (privateKey == nullptr || accessToken == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->GetFirebaseCloudMessageAcccessToken(privateKey);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(accessToken, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*accessToken)->cnt = size;
			memcpy((*accessToken)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int CreateAWSSNSTopic_V2(uint64_t handleVal, const char* snsTopicName, LStrHandle arnTopic) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (snsTopicName == nullptr || arnTopic == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->CreateAWSSNSTopic(snsTopicName);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(arnTopic, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*arnTopic)->cnt = size;
			memcpy((*arnTopic)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int DeleteAWSSNSTopic_V2(uint64_t handleVal, const char* arnTopic) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (arnTopic == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->DeleteAWSSNSTopic(arnTopic)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int ListASWTopics_V2(uint64_t handleVal, LStrHandle arnTopics) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (arnTopics == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->ListAWSSNSTopic();
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(arnTopics, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*arnTopics)->cnt = size;
			memcpy((*arnTopics)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int SubcribeSMSPhoneNumberAWSSNSTopic_V2(uint64_t handleVal, const char* snsTopicName, const char* phoneNumber, LStrHandle subscribedARN) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (snsTopicName == nullptr || phoneNumber == nullptr || subscribedARN == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->SubcribeSMSPhoneNumber(snsTopicName, phoneNumber);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(subscribedARN, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*subscribedARN)->cnt = size;
			memcpy((*subscribedARN)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int SubcribeEmailAddressAWSSNSTopic_V2(uint64_t handleVal, const char* snsTopicName, const char* emailAddress, LStrHandle subscribedARN) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (snsTopicName == nullptr || emailAddress == nullptr || subscribedARN == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->SubscribeEmailAddress(snsTopicName, emailAddress);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(subscribedARN, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*subscribedARN)->cnt = size;
			memcpy((*subscribedARN)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int SendMessageToAWSSNSTopic_V2(uint64_t handleVal, const char* snsTopicName, const char* subjectContent, const char* messageContent, LStrHandle messageId) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (snsTopicName == nullptr || subjectContent == nullptr || messageContent == nullptr || messageId == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->SendMessageToTopic(snsTopicName, subjectContent, messageContent);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(messageId, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*messageId)->cnt = size;
			memcpy((*messageId)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int SendMessageToPhoneNumber_V2(uint64_t handleVal, const char* phoneNumber, const char* messageContent, LStrHandle messageId) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (phoneNumber == nullptr || messageContent == nullptr || messageId == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->SendMessageToPhoneNumber(phoneNumber, messageContent);
		if (st.empty()) return 0;
		int size = st.length();
		MgErr error;
		error = DSSetHandleSize(messageId, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*messageId)->cnt = size;
			memcpy((*messageId)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int SetupServerProxy_V2(uint64_t handleVal, const char* hostName, int port, const char* userName, const char* passWord) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (hostName == nullptr || userName == nullptr || passWord == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->SetServerProxy(hostName, port, userName, passWord)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int AuthenticateGCS_V2(uint64_t handleVal, const char* jsonKeyString) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (jsonKeyString == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->AuthenticateGCS(jsonKeyString)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadMatToGCS_V2(uint64_t handleVal, const char* bucketName, const char* objectName, unsigned char* jpeg_string, int32 bufferLength) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || objectName == nullptr || jpeg_string == nullptr || bufferLength <= 0) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->UploadMatToGCS(bucketName, objectName, jpeg_string, bufferLength)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadImageToGCS_V2(uint64_t handleVal, const char* bucketName, const char* objectName, cv::Mat image) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSUtilities*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || objectName == nullptr) return 0;
	UtilHandleGuard guard(AcquireUtilHandle(_v2h));
	if (!guard) return 0;
	try {
		if (image.empty()) return 0;
		if (guard.get()->UploadMatToGCS(bucketName, objectName, image)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

// ── ANSAWSS3 V2 ──

extern "C" ANSULT_API int ConnectANSAWSHandle_V2(uint64_t handleVal, const char* baseDomain, const char* bucketRegion, const char* serviceName, int port, int bTls, int autoReconnect, int* awsPath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (awsPath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		bool bTlsBool = (bTls == 1);
		bool autoReconnectBool = (autoReconnect == 1);
		bool awsPathBool = true;
		int result = guard.get()->Connect(baseDomain, bucketRegion, serviceName, port, bTlsBool, autoReconnectBool, awsPathBool);
		*awsPath = awsPathBool ? 1 : 0;
		return result;  // 1 = connected, 0 = failed, 2 = no internet (retrying)
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int SetProxyANSAWSHandle_V2(uint64_t handleVal, const char* proxyHost, int proxyPort, const char* proxyUsername, const char* proxyPassword) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->SetServerProxy(proxyHost, proxyPort, proxyUsername, proxyPassword)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int SetAuthenticationANSAWSHandle_V2(uint64_t handleVal, const char* accessKey, const char* secretKey) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->SetAuthentication(accessKey, secretKey)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int ListBucketANSAWSHandle_V2(uint64_t handleVal, LStrHandle bucketList) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketList == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::vector<std::string> bucketListVec = guard.get()->ListBuckets();
		std::string st;
		if (!bucketListVec.empty()) {
			for (const auto& bucket : bucketListVec) {
				st += bucket + ";";
			}
			if (!st.empty() && st.back() == ';') {
				st.pop_back();
			}
		}
		if (st.empty()) return 0;
		int size = (int)st.length();
		MgErr error = DSSetHandleSize(bucketList, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*bucketList)->cnt = size;
			memcpy((*bucketList)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int ListBucketObjectsANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, LStrHandle bucketNameList) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || bucketNameList == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::vector<std::string> bucketObjects = guard.get()->ListBucketObjects(bucketName);
		std::string st;
		if (!bucketObjects.empty()) {
			for (const auto& object : bucketObjects) {
				st += object + ";";
			}
			if (!st.empty() && st.back() == ';') {
				st.pop_back();
			}
		}
		if (st.empty()) return 0;
		int size = (int)st.length();
		MgErr error = DSSetHandleSize(bucketNameList, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*bucketNameList)->cnt = size;
			memcpy((*bucketNameList)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int ListBucketObjectsWithPrefixANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, LStrHandle bucketNameList) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || bucketNameList == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::vector<std::string> bucketObjects = guard.get()->ListBucketObjectsWithPrefix(bucketName, prefix);
		std::string st;
		if (!bucketObjects.empty()) {
			for (const auto& object : bucketObjects) {
				st += object + ";";
			}
			if (!st.empty() && st.back() == ';') {
				st.pop_back();
			}
		}
		if (st.empty()) return 0;
		int size = (int)st.length();
		MgErr error = DSSetHandleSize(bucketNameList, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*bucketNameList)->cnt = size;
			memcpy((*bucketNameList)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int GetRegionANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, LStrHandle region) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || region == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string st = guard.get()->GetBucketRegion(bucketName);
		if (st.empty()) return 0;
		int size = (int)st.length();
		MgErr error = DSSetHandleSize(region, sizeof(int32) + size * sizeof(uChar));
		if (error == noErr) {
			(*region)->cnt = size;
			memcpy((*region)->str, st.c_str(), size);
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int CreateBucketANSAWSHandle_V2(uint64_t handleVal, const char* bucketName) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->CreateBucket(bucketName)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int DeleteBucketANSAWSHandle_V2(uint64_t handleVal, const char* bucketName) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->DeleteBucket(bucketName)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int CreateBucketPrefixANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->CreateFolder(bucketName, prefix)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int DeleteBucketPrefixANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || prefix == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->DeleteFolder(bucketName, prefix)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int DeleteBucketObjectANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* bucketObject) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->DeleteBucketObject(bucketName, bucketObject)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadTextDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* textDataPath, LStrHandle uploadedFilePath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || textDataPath == nullptr || uploadedFilePath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadTextData(bucketName, textDataPath, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadBinaryDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* binaryDataPath, LStrHandle uploadedFilePath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || binaryDataPath == nullptr || uploadedFilePath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadBinaryData(bucketName, binaryDataPath, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadFileStreamDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* fileDataPath, LStrHandle uploadedFilePath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || fileDataPath == nullptr || uploadedFilePath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadFileStream(bucketName, fileDataPath, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadMultiPartDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* fileDataPath, int fileSize, LStrHandle uploadedFilePath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || fileDataPath == nullptr || uploadedFilePath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadMultipartData(bucketName, fileDataPath, outPath, fileSize)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadJpegImageANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, LStrHandle uploadedFilePath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr || uploadedFilePath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadJpegImage(bucketName, jpeg_string, bufferLength, fileName, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadPrefixJpegImageANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, unsigned char* jpeg_string, int32 bufferLength, const char* fileName, LStrHandle uploadedFilePath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || prefix == nullptr || jpeg_string == nullptr || bufferLength <= 0 || fileName == nullptr || uploadedFilePath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadPrefixJpegImage(bucketName, prefix, jpeg_string, bufferLength, fileName, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadPrefixMultiPartDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, const char* fileDataPath, const char* objectName, int fileSize, LStrHandle uploadedFilePath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || prefix == nullptr || fileDataPath == nullptr || fileSize <= 0 || uploadedFilePath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadPrefixMultipartData(bucketName, prefix, fileDataPath, objectName, outPath, fileSize)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int UploadPrefixBinaryDataANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* prefix, const char* binaryDataPath, const char* objectName, LStrHandle uploadedFilePath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || prefix == nullptr || binaryDataPath == nullptr || objectName == nullptr || uploadedFilePath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		std::string outPath;
		if (guard.get()->UploadPrefixBinaryData(bucketName, prefix, binaryDataPath, objectName, outPath)) {
			int size = (int)outPath.length();
			MgErr error = DSSetHandleSize(uploadedFilePath, sizeof(int32) + size * sizeof(uChar));
			if (error == noErr) {
				(*uploadedFilePath)->cnt = size;
				memcpy((*uploadedFilePath)->str, outPath.c_str(), size);
			}
			return 1;
		}
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}

extern "C" ANSULT_API int DownloadFileStreamANSAWSHandle_V2(uint64_t handleVal, const char* bucketName, const char* objectName, const char* savedFilePath) {
	auto* _v2h = reinterpret_cast<ANSCENTER::ANSAWSS3*>(handleVal); if (!_v2h) return 0;
	if (bucketName == nullptr || objectName == nullptr || savedFilePath == nullptr) return 0;
	AWSHandleGuard guard(AcquireAWSHandle(_v2h));
	if (!guard) return 0;
	try {
		if (guard.get()->DownloadFile(bucketName, objectName, savedFilePath)) return 1;
		else return 0;
	}
	catch (std::exception& e) { return 0; }
	catch (...) { return 0; }
}
