Created
September 29, 2020 07:48
-
-
Save rkttu/efc32040c931f093829c195ed5386dec to your computer and use it in GitHub Desktop.
Jagged String Array (VC++) Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tchar.h> | |
#include <stdio.h> | |
#include <combaseapi.h> | |
int __stdcall GetStringArrayDataW(OUT LPWSTR** lpArrayDest, OUT SIZE_T* pArrayLength); | |
void __stdcall FreeStringArrayW(OUT LPWSTR** lpArrayDest, IN SIZE_T nArrayLength); | |
int __stdcall GetStringArrayDataA(OUT LPSTR** lpArrayDest, OUT SIZE_T* pArrayLength); | |
void __stdcall FreeStringArrayA(OUT LPSTR** lpArrayDest, IN SIZE_T nArrayLength); | |
#ifdef UNICODE | |
#define GetStringArrayData GetStringArrayDataW | |
#define FreeStringArray FreeStringArrayW | |
#else // UNICODE | |
#define GetStringArrayData GetStringArrayDataA | |
#define FreeStringArray FreeStringArrayA | |
#endif // UNICODE | |
int main() { | |
LPTSTR* lpArray; | |
SIZE_T len; | |
if (!GetStringArrayData(&lpArray, &len)) { | |
return (-1); | |
} | |
for (SIZE_T i = 0; i < len; i++) { | |
_ftprintf(stdout, _T("[%d] %s\r\n"), i, lpArray[i]); | |
} | |
FreeStringArray(&lpArray, len); | |
return 0; | |
} | |
static const SIZE_T nStringLength = 32; | |
static const SIZE_T nArraySize = 8; | |
int __stdcall GetStringArrayDataW(OUT LPWSTR** lpArrayDest, OUT SIZE_T* pArrayLength) { | |
*pArrayLength = nArraySize; | |
LPWSTR* lpArray = (LPWSTR*)CoTaskMemAlloc(nArraySize * sizeof(LPWSTR)); | |
if (!lpArray) { | |
return 0; | |
} | |
for (SIZE_T i = 0; i < nArraySize; i++) { | |
lpArray[i] = (LPWSTR)CoTaskMemAlloc((nStringLength + 1) * sizeof(WCHAR)); | |
if (!lpArray[i]) { | |
return i; | |
} | |
swprintf_s(lpArray[i], nStringLength + 1, | |
L"%d%c%c%c%c", i, 'a' + i, 'a' + i, 'a' + i, 'a' + i); | |
} | |
*lpArrayDest = lpArray; | |
return nArraySize; | |
} | |
void __stdcall FreeStringArrayW(OUT LPWSTR** lpArrayDest, IN SIZE_T nArrayLength) { | |
LPWSTR* lpArray = *lpArrayDest; | |
if (!lpArray) return; | |
for (SIZE_T i = 0; i < nArrayLength; i++) { | |
if (!lpArray[i]) continue; | |
CoTaskMemFree(lpArray[i]); | |
lpArray[i] = NULL; | |
} | |
CoTaskMemFree(lpArray); | |
lpArray = NULL; | |
} | |
int __stdcall GetStringArrayDataA(OUT LPSTR** lpArrayDest, OUT SIZE_T* pArrayLength) { | |
*pArrayLength = nArraySize; | |
LPSTR* lpArray = (LPSTR*)CoTaskMemAlloc(nArraySize * sizeof(LPSTR)); | |
if (!lpArray) { | |
return 0; | |
} | |
for (SIZE_T i = 0; i < nArraySize; i++) { | |
lpArray[i] = (LPSTR)CoTaskMemAlloc((nStringLength + 1) * sizeof(CHAR)); | |
if (!lpArray[i]) { | |
return i; | |
} | |
sprintf_s(lpArray[i], nStringLength + 1, | |
"%d%c%c%c%c", i, 'a' + i, 'a' + i, 'a' + i, 'a' + i); | |
} | |
*lpArrayDest = lpArray; | |
return nArraySize; | |
} | |
void __stdcall FreeStringArrayA(OUT LPSTR** lpArrayDest, IN SIZE_T nArrayLength) { | |
LPSTR* lpArray = *lpArrayDest; | |
if (!lpArray) return; | |
for (SIZE_T i = 0; i < nArrayLength; i++) { | |
if (!lpArray[i]) continue; | |
CoTaskMemFree(lpArray[i]); | |
lpArray[i] = NULL; | |
} | |
CoTaskMemFree(lpArray); | |
lpArray = NULL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment