How to convert a string to TYPES.LPTSTR.targetType.array

75 Views Asked by At

I have supposed some definitions as follows:

var CONFIG = {
    is64bit: ctypes.voidptr_t.size == 4 ? false : true,
    ifdef_UNICODE: true};
var TYPES = {
    CHAR: ctypes.char,
    WCHAR: ctypes.char16_t};
TYPES.LPSTR = TYPES.CHAR.ptr;
TYPES.LPWSTR = TYPES.WCHAR.ptr;
TYPES.LPTSTR = CONFIG.ifdef_UNICODE ? TYPES.LPWSTR : TYPES.LPSTR;

I have a string like: "OMNIKEY AG Smart Card Reader USB 0"

and I want to convert it to TYPES.LPTSTR.targetType.array format in which if it show by toString() it would be as follow:

ctypes.char16_t.array(36)(["O", "M", "N", "I", "K", "E", "Y", " ", 
"A", "G", " ", "S", "m", "a", "r", "t", " ", "C", "a", "r", "d", 
" ", "R", "e", "a", "d", "e", "r", " ", "U", "S", "B", " ", "0", 
"\x00", "\x00"])

My system is 64 bit.

1

There are 1 best solutions below

3
Mimouni On

This will actually be done like this: var st = "OMNIKEY AG Smart Card Reader USB 0"; var st_cArr = TYPES.LPTSTR.targetType.array(st.length+1)(st) I did st.length+1 because by default you gt one null terminator, i did +1 so you get the two null terminators of \x00