-
Notifications
You must be signed in to change notification settings - Fork 5
/
StringExample.cpp
67 lines (60 loc) · 2.43 KB
/
StringExample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
////////////////////////////////////////////////////////////////////////////
// StringExample.cpp
//
// This file demonstrates how to take string arguments from and return
// string to Excel.
//
// The following types are supported as arguments and return value:
//
// [const] char * [const]
// [const] wchar_t * [const]
// [const] std::wstring [&]
//
// In Excel 2003 and earlier, strings are ansi-encoded and can contain
// up to 255 characters (not including the nul-terminator). A passed-in
// string argument will never contain more bytes. If a string longer than
// 255 bytes is returned, the wrapper throws std::invalid_argument() and
// returns #VALUE! to Excel.
//
// In Excel 2007 and later, strings are unicode-encoded and can contain
// up to 32,767 characters, not including the nul-terminator. A passed-in
// argument will never contain more characters. If a string longer than
// 32,767 characters is returned, the wrapper throws std::invalid_argument()
// and returns #VALUE! to Excel.
//
// The wrapper automatically performs ansi-unicode conversion where
// necessary. If some characters cannot be converted, it is replaced by a
// placeholder.
//
// When passed as argument, wchar_t* and char* are more efficient than
// their std::[w]string counterparts because they are passed directly
// from Excel and incurs no allocation or copying overhead. When used
// in return value, strings are always copied, so there is not much
// difference in performance.
#include "XllAddin.h"
// ReverseString:
// Simple example that takes a string as input and returns a string
// with its characters reversed.
std::wstring ReverseString(const std::wstring &s)
{
return std::wstring(s.crbegin(), s.crend());
}
EXPORT_XLL_FUNCTION(ReverseString, XLL_NOT_VOLATILE | XLL_THREADSAFE);
// GetTooLongString:
// Returns a string of 40,000 characters. The wrapper throws an exception
// and returns #VALUE! to Excel.
std::wstring GetTooLongString()
{
return std::wstring(40000, L'x');
}
EXPORT_XLL_FUNCTION(GetTooLongString, XLL_NOT_VOLATILE | XLL_THREADSAFE);
// MultiByteStrLen:
// Returns the length of a byte string. The purpose of this function
// is to test string marshalling. If marshalled as "C", the maximum
// input allowed is 255 bytes; if a longer string is input, Excel returns
// #VALUE! directly without calling the function at all.
int MultiByteStrLen(const char *s)
{
return static_cast<int>(strlen(s));
}
EXPORT_XLL_FUNCTION(MultiByteStrLen);