ACE3/extensions/clipboard/ace_clipboard.cpp

99 lines
2.6 KiB
C++
Raw Normal View History

2015-05-02 21:18:42 +00:00
/*
* ace_clipboard.cpp
*
2016-02-17 23:07:06 +00:00
* Takes a string and copies it to the clipboard; bypasses arma 8k clippy limit. Windows only.
2015-05-02 21:18:42 +00:00
*
* Takes:
* Localized string as string
*
* Returns:
* None
*/
2016-02-17 23:07:06 +00:00
#include "shared.hpp"
2016-02-17 23:07:06 +00:00
#include <stdlib.h>
2015-05-02 21:18:42 +00:00
#include <vector>
#include <string>
extern "C" {
EXPORT void __stdcall RVExtension(char *output, int outputSize, const char *function);
}
2015-05-02 21:18:42 +00:00
2015-05-02 22:28:40 +00:00
std::string gClipboardData;
2015-05-02 21:18:42 +00:00
void __stdcall RVExtension(char *output, int outputSize, const char *function) {
2015-05-02 22:28:40 +00:00
std::string cur_input(function);
2015-05-02 21:18:42 +00:00
std::string result;
ZERO_OUTPUT();
if (cur_input.length() < 1) {
EXTENSION_RETURN();
}
2015-05-02 21:18:42 +00:00
2015-05-02 22:28:40 +00:00
if (!strcmp(function, "version")) {
2016-02-17 23:07:06 +00:00
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE);
EXTENSION_RETURN();
2015-05-02 21:18:42 +00:00
}
2016-02-19 18:24:08 +00:00
#ifdef _WIN32
2015-05-02 21:18:42 +00:00
2016-02-17 23:07:06 +00:00
if (!strcmp(function, "--COMPLETE--"))
{
if (OpenClipboard(NULL) == 0)
2016-02-17 23:07:06 +00:00
{
result = "OpenClipboard() failed, GetLastError=" + GetLastError();
2015-05-02 22:28:40 +00:00
}
2016-02-17 23:07:06 +00:00
else
{
if (EmptyClipboard() == 0)
2016-02-17 23:07:06 +00:00
{
result = "EmptyClipboard() failed, GetLastError=" + GetLastError();
2015-05-02 22:28:40 +00:00
}
2016-02-17 23:07:06 +00:00
else
{
// GPTR = GMEM_FIXED + GMEM_ZEROINIT, returns a ptr, no need for GlobalLock/GlobalUnlock
char *pClipboardData = (char *)GlobalAlloc(GPTR, gClipboardData.length());
if (pClipboardData == NULL)
{
result = "GlobalAlloc() failed, GetLastError=" + GetLastError();
EXTENSION_RETURN();
}
strncpy_s(pClipboardData, gClipboardData.length(), gClipboardData.c_str(), _TRUNCATE);
// if success, system owns the memory, if fail, free it from the heap
if (SetClipboardData(CF_TEXT, pClipboardData) == NULL)
2016-02-17 23:07:06 +00:00
{
2015-05-02 22:28:40 +00:00
result = "SetClipboardData() failed, GetLastError=" + GetLastError();
GlobalFree(pClipboardData);
2015-05-02 22:28:40 +00:00
}
2016-02-17 23:07:06 +00:00
else
{
if (CloseClipboard() == 0)
2016-02-17 23:07:06 +00:00
{
2015-05-02 22:28:40 +00:00
result = "CloseClipboard() failed, GetLastError=" + GetLastError();
}
2015-05-02 21:18:42 +00:00
}
}
}
2015-05-02 22:28:40 +00:00
gClipboardData = "";
2016-02-17 23:07:06 +00:00
}
else
{
2015-05-02 22:28:40 +00:00
gClipboardData = gClipboardData + cur_input;
2015-05-02 21:18:42 +00:00
}
2016-02-17 23:07:06 +00:00
if (result.length() > 1)
{
strncpy_s(output, outputSize, result.c_str(), _TRUNCATE);
}
2015-05-02 21:18:42 +00:00
2016-02-19 18:24:08 +00:00
#endif
EXTENSION_RETURN();
2015-05-02 21:18:42 +00:00
}