0

I'm creating Windows Runtime Component (c++) to use it later in windows phone 8.1 application (c#). My problem is preety simple but I cannot find any answer to it:

I need to create a method which take string/char */anything which is filepath as parameter and pass it to external method which takes char * as parameter.

I've tried with std::string, String^, char *. But I still get errors like such types are not supported (not in String^ case) or some other one.

Is there some simple answer which tells me how should I do this?

Code samples with errors

beginning

#include "pch.h"
#include "Class1.h"

using namespace Platform;

namespace WindowsRuntimeComponent2
{
    public ref class Class1 sealed
    {

various types:

int Foo(std::string param) {

cause: Foo': signature of public member contains native type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' WindowsRuntimeComponent2 and few more containing same information about string's dependencies

int Foo(char * param) {

cause: 'Foo': signature of public member contains native type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' WindowsRuntimeComponent2

int Foo(String^ photoPath) {

this one do not cause any errors but I don't know how to parse it to my char *.

Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62

1 Answers1

0

Essentially here you just need to know how to convert from System.String^ to std::string, this can be done in C++/CLI mode as follows:

#include <msclr\marshal_cppstd.h>

//...

int Foo(String^ photoPath) {
    std::string unmanaged_photoPath = msclr::interop::marshal_as<std::string>(photoPath);
    // then convert to c-style string as normal using unmanaged_photoPath.c_str();
    return 0;
}
sjrowlinson
  • 3,297
  • 1
  • 18
  • 35
  • Thanks, for your answer I's sure I'm a step closer, but such approach cause some integration problem: `'c:\windows\microsoft.net\framework\v4.0.30319\mscorlib.dll' : WinRT does not support #using of a managed assembly WindowsRuntimeComponent2` in vcclr.h line:16. Do you have any idea of how to handle it? – Mateusz Rogulski Jun 08 '16 at 21:22
  • @MateuszRogulski I can't be sure without seeing the code structure - does `line 16` correspond to `std::string unmanaged_photoPath = ...` in my answer? – sjrowlinson Jun 08 '16 at 21:33
  • @ArchibishopOfBanterbury Sorry for missunderstaning, vcclr.h is a file of Windows Runtime Component. In this line we have `#using `. So I guess your way of parsing uses it. – Mateusz Rogulski Jun 09 '16 at 06:23