[英]D Style Arrays to C Style Arrays
I am creating a Window in D, and the CreateWindowA
function requires pointers to characters, C character arrays basically. 我正在D中创建一个Window,并且
CreateWindowA
函数需要指向字符(基本上是C字符数组)的指针。
How do I convert a D style array ( char[]
) to a C style array ( char*
)? 如何将D样式数组(
char[]
)转换为C样式数组( char*
)?
The two functions to look at are normally std.string.toStringz
and std.utf.toUTFz
. 通常
std.string.toStringz
的两个函数是std.string.toStringz
和std.utf.toUTFz
。
toStringz
will convert string
to immutable(char)*
, which you can pass to a C function which takes const char*
. toStringz
会将string
转换为immutable(char)*
,您可以将其传递给采用const char*
的C函数。 If it can determine that the string
is null-terminated (which usually is only the case for string literals, which have a null terminator one passed their end), then it won't allocate and will just use the string
's ptr
property, but in most cases, it will allocate. 如果它可以确定
string
以null终止(通常只有字符串文字,这种情况的末尾有一个null终止符),则它不会分配,而只会使用string
的ptr
属性,但在大多数情况下,它将分配。
toUTFz
will convert from any string type to any character pointer type. toUTFz
将从任何字符串类型转换为任何字符指针类型。 It's probably most frequently used for converting to const(wchar)*
for Windows, since all of the W
functions for Windows take UTF-16, but it can also be used to convert to char*
- eg str.toUTFz!(char*)()
. 由于Windows的所有
W
函数都采用UTF-16,因此它可能最常用于Windows的const(wchar)*
转换,但是它也可以用于转换为char*
-例如str.toUTFz!(char*)()
。 Like toStringz
, it will try not to allocate if it can determine that it's unnecessary, but it's almost always necessary. 像
toStringz
一样,如果它可以确定不必要,它将尝试不分配,但几乎总是必需的。
Now, for your particular case, you're trying to use one of the A
functions in Windows. 现在,对于您的特殊情况,您尝试使用Windows中的
A
函数之一。 This is almost always a bad idea, and I would strongly advise against it. 这几乎总是一个坏主意,我强烈建议不要这样做。 Use
toUTFz
to convert your string
to const(wchar)*
and pass that to CreateWindowW
. 使用
toUTFz
将您的string
转换为const(wchar)*
并将其传递给CreateWindowW
。 AFAIK, the only advantage to the A
functions is that they work with pre-Win2K. AFAIK,
A
函数的唯一优点是它们可与Win2K之前的版本一起使用。 Everything else about them is worse. 关于他们的其他一切都更糟。 However, if for some reason, you insist on using the
A
functions, then you're going to have to use std.windows.charset.toMBSz
, because the A
functions don't take UTF-8 but rather the "Windows 8-bit character set," and toMBSz
will convert the string to that format. 但是,如果由于某种原因坚持使用
A
函数,则将不得不使用std.windows.charset.toMBSz
,因为A
函数使用的不是UTF-8,而是使用“ Windows 8-位字符集”和toMBSz
会将字符串转换为该格式。
you grab the ptr
field of the D array. 您可以获取D数组的
ptr
字段。 and the length
field to grab the length 和
length
字段来获取长度
however if you need a C-style string then you need the toStringz
method that will add the null terminator and return the pointer to the first char. 但是,如果需要C样式的字符串,则需要
toStringz
方法,该方法将添加空终止符并将指针返回第一个字符。 k
eep a reference to it if the api doesn't create it's own copy to operate on to avoid dangling pointers by GC k
EEP一个它的参考,如果API不创建它自己的拷贝操作,以避免由GC悬摆指针
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.