How to initialize wstring [] with wchar ** in D 2.0

In C ++, I can initialize the <wstring> vector with wchar_t **, as in this example:

#include <windows.h>
#include <string>
#include <vector>
#include <cwchar>
using namespace std;

int main() {
    int argc;
    wchar_t** const args = CommandLineToArgvW(GetCommandLineW(), &argc);
    if (args) {
        const vector<wstring> argv(args, args + argc);
        LocalFree(args);
    }
}

However, is there a way to initialize wstring [] with wchar ** in D 2.0?

I can add the contents of wchar ** to wstring [] as follows:

import std.c.windows.windows;
import std.c.wcharh;

extern(Windows) {
    wchar* GetCommandLineW();
    wchar** CommandLineToArgvW(wchar*, int*);
    void* LocalFree(void*);
}

void main() {
    int argc;
    wchar** args = CommandLineToArgvW(GetCommandLineW(), &argc);
    if (args) {
        wstring[] argv;
        for (size_t i = 0; i < argc; ++i) {
            wstring temp;
            const size_t len = wcslen(args[i]);
            for (size_t z = 0; z < len; ++z) {
                temp ~= args[i][z];
            }
            argv ~= temp;
        }
        LocalFree(args);
    }
}

But I would like to find a cleaner and easier way, like a C ++ version. (Performance is not a concern)

+5
source share
2 answers

Here is a simpler version using slices:

import std.c.windows.windows;
import std.c.wcharh;
import std.conv;

extern(Windows) {
    wchar* GetCommandLineW();
    wchar** CommandLineToArgvW(wchar*, int*);
    void* LocalFree(void*);
}

void main() {
    int argc;
    wchar** args = CommandLineToArgvW(GetCommandLineW(), &argc);
    if (args) {
        wstring[] argv = new wstring[argc];
        foreach (i, ref arg; argv)
            arg = to!wstring(args[i][0 .. wcslen(args[i])]);
        LocalFree(args);
    }
}

Another option is to use void main(string[] args)and convert to args wstring if you really need to.

+6
source

you can use

void main(wstring[] args){
//...
}

getting command line arguments is much easier

edit: , char D, - , C , 90% ( ).

+1

All Articles