1

First of all hey guys! I would like to access 2 C++ functions from my windows phone app. so i followed every step of this tutorial and i managed to call the function as the poster of the tutorial. Now i wanted to access my own functions so i created the class in the header and in the .cpp file and the project is building ok as long as my functions are not public. Means i cant access them.

 public ref class Base64Encoding sealed
{
  public:
    char *EncodeData(char *data, int length, int  *resultLength); //this doesnt compile
    char *DecodeString(char *data, int *resultLength); //this compiles buts inaccessible
};

i get a return exception saying error C3992: signature of public member contains invalid type char. I did some googling and as far as i understand i cannot send parameters of type char since its unmanaged code or something like that.

So what is the issue here? why i cannot pass parameters of type char?

Update

i followed robwirving suggestion and now the header looks like this.

public ref class Base64Encoding sealed
    {
    public : Platform::String^ EncodeData(String^ StringData);
    public : Platform::String^ DecodeString(String^ StringData);
    };

in order now to get char* data from the String^ StringData parameter i did in my .cpp

#include <string>
#include <iostream>
#include <msclr\marshal_cppstd.h>
using namespace Platform;
using namespace std;
String^ EncodeData(String^ StringData)
{ 
    // base64 lookup table. this is the encoding table for all 64 possible values
    static char *figures = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    msclr::interop::marshal_context context;
    std::string s = context.marshal_as<std::string>(StringData);
    char *data = new char[s.size() + 1];
    int length = s.length;
    int *resultLength = s.length;
    data[s.size()] = 0;
/* bla bla some functions irrelevant*/
.
.
.
return StringFromAscIIChars(result);
}

static String^ StringFromAscIIChars(char* chars)
{
    size_t newsize = strlen(chars) + 1;
    wchar_t * wcstring = new wchar_t[newsize];
    size_t convertedChars = 0;
    mbstowcs_s(&convertedChars, wcstring, newsize, chars, _TRUNCATE);
    String^ str = ref new Platform::String(wcstring);
    delete[] wcstring;
    return str;
}

but now i get 2 errors on building.

1: error C1114: WinRT does not support #using of a managed assembly

2: IntelliSense: an ordinary pointer to a C++/CX mapping ref class or interface class is not allowed

1
  • Updated my answer, hope that helps Commented Mar 14, 2014 at 18:34

1 Answer 1

1

You said the answer right in your question. You can't use a char in a public function of a WinRT component. You can however use strings, I'd suggest changing your functions to the following:

public ref class Base64Encoding sealed
{
  public:
    Platform::String^ EncodeData(Platform::String^ data); 
    Platform::String^ DecodeString(Platform::String^ data);
};

In the definition of your encode/decode functions you can then convert your inputs from a Platform::String^ to a char array, call your original C++ function, then convert the return value back to a Platform::String^

I know this may seem like a lot of extra work, but it makes the interop a lot easier for the C# that will consume your WinRT component.

Update:

I think your additional errors probably come from the including msclr\marshal_cppstd.h and the way you are converting your Platform::String^ to a std::string.

Reference this post for how to convert from Platform::String^ to a char*: How to convert Platform::String to char*?

Sign up to request clarification or add additional context in comments.

3 Comments

i did as u suggested and i got 2 "major" issues on a file i cannot edit on my project call vcclr.h. the bugs are 1: error C1114: WinRT does not support #using of a managed assembly 2: IntelliSense: an ordinary pointer to a C++/CX mapping ref class or interface class is not allowed
You'd have to give me more info then that. The error is probably coming from your existing C++ code though
thank you. got some minor bugs but you helped a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.