user1561108
user1561108

Reputation: 2747

Cannot convert ‘char**’ to ‘wchar_t**’

The code excerpt:

int main(int argc, char *argv[]){  
    PySys_SetArgv(argc, argv);

produces error message

error: cannot convert ‘char**’ to ‘wchar_t**’ for argument ‘2’ to ‘void PySys_SetArgv(int, wchar_t**)’

How do I convert argv?

Upvotes: 3

Views: 5259

Answers (3)

M.M
M.M

Reputation: 141544

You could look into whether your compiler supports wmain. If so, you can replace main with:

int wmain(int argc, wchar_t *argv[])
{  
    PySys_SetArgv(argc, argv);

Upvotes: 3

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

This API is part of python 3.0 and above and in my knowledge, there's no easy way to get this done. However, I think, you can try converting the argv to a w_char ** and then call PySys_SetArgv(). mbstowcs() may come handy in this case.

For example, some pseudo-code (not tested) will look like

wchar_t **changed_argv;

changed_argv = malloc((argc)* sizeof*changed_argv);

for (int i = 0; i < argc, i++)
{
    changed_argv[i] = malloc(strlen(argv[i]) + 1);
    mbstowcs(changed_argv[i], argv[i], strlen(argv[i]) + 1);
}

Upvotes: 1

Bizkit
Bizkit

Reputation: 374

Refer to the following link:

how to convert char array to wchar_t array?

You can write a conversion routine:

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;
using namespace System;


int main(int argc, char* argv[])
{
    char *orig = "Hello, World!";
    cout << orig << " (char *)" << endl;

    // Convert to a wchar_t*
    size_t origsize = strlen(orig) + 1;
    const size_t newsize = 100;
    size_t convertedChars = 0;
    wchar_t wcstring[newsize];
    mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
    wcscat_s(wcstring, L" (wchar_t *)");
    wcout << wcstring << endl;
}

Upvotes: 2

Related Questions