locked
How to transfer a vector object from WinRT DLL back to Windows store app?

    Question

  • hello guys,  

    I have a WinRT DLL and a windows store app which both written with CPP. 

    The project is about find all paired bluetooth devices  in WinRT DLL (not WinRT Component DLL), then save them into a vector and transfer it back to the windows store app for display.

    The question is: retDeviceInfoVector already got a correct value before return from CBTWinRTDLL::FindPairedDevices(), but in the store app, deviceInfoVector doesn't get anything: (deviceInfoVector = btDLL.FindPairedDevices();)

    the relative code snippet is below, Thanks!

    WinRT DLL  .h code:

    #pragma once
    
    #ifdef BTWinRTDLL_EXPORTS
    #define BTWinRTDLL_API  __declspec(dllexport)
    #else
    #define BTWinRTDLL_API __declspec(dllimport)
    #endif
    
    
    Windows::Networking::Sockets::StreamSocket^ _socket;
    Windows::Storage::Streams::DataWriter^ _writer;
    Windows::Storage::Streams::DataReader^ _reader;
    
    class BTWinRTDLL_API CBTWinRTDLL
    {
    	public:
    		CBTWinRTDLL(void);
    		Platform::Collections::Vector<Windows::Devices::Enumeration::DeviceInformation^>^  FindPairedDevices();
    
    };

    WinRT DLL .cpp code:

    Platform::Collections::Vector<Windows::Devices::Enumeration::DeviceInformation^>^  CBTWinRTDLL::FindPairedDevices()
    {
    	Platform::Collections::Vector<Windows::Devices::Enumeration::DeviceInformation^>^ retDeviceInfoVector = ref new Platform::Collections::Vector<Windows::Devices::Enumeration::DeviceInformation^>();
    	auto deviceEnumTask = create_task(DeviceInformation::FindAllAsync(RfcommDeviceService::GetDeviceSelector(RfcommServiceId::SerialPort)));
    	int s = 0;
    
    	deviceEnumTask.then([this, &s, retDeviceInfoVector](DeviceInformationCollection^ devices)->Platform::Collections::Vector<Windows::Devices::Enumeration::DeviceInformation^>^
    	{
    		s = devices->Size;
    		Platform::Collections::Vector<Windows::Devices::Enumeration::DeviceInformation^>^ deviceInfoVector = ref new Platform::Collections::Vector<Windows::Devices::Enumeration::DeviceInformation^>();
    		DeviceInformation^ di;
    		for (int i = 0; i < devices->Size; i++)
    		{
    			di = devices->GetAt(i);
    			deviceInfoVector->Append(di);
    		}
    		return deviceInfoVector;
    	}).then([this, retDeviceInfoVector](Platform::Collections::Vector<Windows::Devices::Enumeration::DeviceInformation^>^ Vector)
    	{
    		int ss = Vector->Size;
    		for (int i = 0; i < Vector->Size; i++)
    		{
    			DeviceInformation^ di;
    			di = Vector->GetAt(i);
    			retDeviceInfoVector->Append(di);
    		}
    	});
    
    	return retDeviceInfoVector;
    }
    

    Windows Store app code:

    void BTApp_CPP::MainPage::Btn_FindPairedDevices_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	deviceInfoVector = ref new Platform::Collections::Vector<Windows::Devices::Enumeration::DeviceInformation^>();
    
    	deviceInfoVector = btDLL.FindPairedDevices();
    	
    	for each(Windows::Devices::Enumeration::DeviceInformation^ devInfo in deviceInfoVector)
    	{
    		this->PairedDevicesList->Items->Append(devInfo->Name + "-AND-" + devInfo->Id);
    	}
    		
    
    }


    nio

    Tuesday, December 10, 2013 8:13 AM

Answers