0

I'm working on a WinRT app in which I need to pass Device ID from C# class to C++ object. The format of ID is:

\\?\SWD#MMDEVAPI#{0.0.0.00000000}.{572cb138-dda1-4723-90d7-373f04b795fb}#{e6327cad-dcec-4949-ae8a-991e976a79d2}

This works well in C++ if I cast it as:

L"\\\\?\\SWD#MMDEVAPI#{0.0.0.00000000}.{572cb138-dda1-4723-90d7-373f04b795fb}#{e6327cad-dcec-4949-ae8a-991e976a79d2}"

However, if I pass it as System.String from C# class to C++ and cast it as LPCWSTR, it doesn't work.

C++ code:

UniversalAudioPlayer::UniversalAudioPlayer(String^ deviceID)
{
HRESULT hr = XAudio2Create(&xAudio);

if (FAILED(hr))
    ref new COMException(hr, "XAudio2Create failure");

XAUDIO2_DEBUG_CONFIGURATION debugConfig = { 0 };
debugConfig.TraceMask = XAUDIO2_LOG_DETAIL | XAUDIO2_LOG_WARNINGS;
xAudio->SetDebugConfiguration(&debugConfig);

hr = xAudio->CreateMasteringVoice(&masteringVoice, 0U, 0U, 0U, (LPCWSTR)deviceID);

if (FAILED(hr))
    ref new COMException(hr, "Could not create mastdsdering voice");

xAudio->StartEngine();
}

C# code:

UniversalAudioPlayer player = new UniversalAudioPlayer("\\\\?\\SWD#MMDEVAPI#{0.0.0.00000000}.{41a5b8e4-ad46-4bcb-b8a0-3621edafd176}#{e6327cad-dcec-4949-ae8a-991e976a79d2}");

I'm not able to include

#include < vcclr.h >

in my C++ class either as WinRT doesn't support managed assemblies.

Please suggest me any solution. Thanks in advance.

Muhammad Hassan
  • 1,037
  • 1
  • 8
  • 22

1 Answers1

2

You can convert from a Platform::String^ to a wchar_t* with the String::Data method.

hr = xAudio->CreateMasteringVoice(&masteringVoice, 0U, 0U, 0U, deviceID->Data);
Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54