Hi,

How can I pass a c# string to C++ side of the app?

Right now, I have all handling in Const char format . When I pass c# string, c++ working fine when using Platform::String . But there is no good way of converting Platform::String to const char .

Any inputs from your side?

Any help will be appreciated.

Thanks.

Recommended Answers

All 9 Replies

When you say "side of the app" I assume you mean a dll. In that case you can use PInvoke and DllImportAttribute.

If you have a separate binary running elsewhere you'll need an API.

Actually, I have an UWP application having one solution in which I have cs & c++ modules. I need to pass a cs based string to cpp side & vice-versa.

I don't understand if we have dll involved here.

Please give your inputs.

Are you saying you're using C# and C++ projects in one solution?

Not projects. Basically I have one solution & in that I have a c++ library integrated as a submodule for image rendering etc. & my cs side code which is UI and other things. I have 10-11 submodules in the app & c++ library is one of them.

App (cs code, c#, UI) --> Cpp Library (c++ code)

Need to:

string (cs) --> const char * (cpp) & same in back direction as well.

I hope it clears the issue. Let me know if more info is needed.

Thanks.

And by C++ Lib you mean a DLL written in C++? Then PTY would be right and you could use Pinvoke

Basically, I checked the project properties of the Component and it's as a dll. I tried to add msclr/mashal.h but it gives me error:

WinRT does not support #using of a managed assembly

It's because by adding the above .h, it uses few #using which seems not supported.

any thoughts on this? FYI, I am developing on UWP application.

Can you please give an example on how it works? A code snippet will do the job for me.

Thanks.

Maybe it's stupid but you can try to dialog between your 2 apps by using client-server and vice versa. Send data with code that contains your string. And use event handler to get the deal.

commented: "It's not stupid if it works." +15
commented: Actually...it works +0

Even with a client server you'll have to convert strings. For this I open the manual at https://docs.microsoft.com/en-us/cpp/cppcx/strings-c-cx?view=vs-2017

A Platform::String can contain only char16 characters, or the NULL character. If your application has to work with 8-bit characters, use the String::Data to extract the text as a const wchar_t*.

There are example functions in the manual. But finding the right pages can be challenging. Hope this helps.

Perhaps it is possible for you to manipulate the C# string.
C# strings have a method called ToCharArray. See here for an example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.