Click here to Skip to main content
11,484,162 members (72,142 online)
Click here to Skip to main content

Tagged as

Communication between C++ Silverlight Host and Silverlight Application

, 20 May 2010 CPOL 33.2K 799 14
Rate this:
Please Sign up or sign in to vote.
It explains how we can call a method defined in Silverlight from Silverlight C++ host.

Introduction

I would like you to take a look at my previous article Host_Silverlight_In_ATL before continuing with this one. In this article, we will talk about communication between C++ Silverlight host and Silverlight application which means we can call a method of Silverlight app from C++ Silverlight host.

C__ToSilverlightCommunication.PNG

Things to Do in a Silverlight Application

 using System.Windows.Browser;

Use the above mentioned namespace for communication. We will use HTML communication bridge to communicate with the C++ host.

Use the following statement to register the Communicator object. We will get this communicator object in C++ Silverlight host using IDispath.

HtmlPage.RegisterScriptableObject("Communicator", this);

Write another function in Silverlight application which we will call from C++ Silverlight host.

[ScriptableMember]
public void SetDataInTextBox(string strData)
{
	txtData.Text = strData;
}

The complete code for the Silverlight page is as follows:

using System.Windows.Browser;
namespace SilverlightTestApp
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			HtmlPage.RegisterScriptableObject("Communicator", this);
			InitializeComponent();
		}
		private void ClickMe_Click(object sender, RoutedEventArgs e)
		{	
			MessageBox.Show("Button click handler is in Silverlight :)", 
			"SilverlightTestApp", MessageBoxButton.OK);
		}
		[ScriptableMember]
		public void SetDataInTextBox(string strData)
		{
			txtData.Text = strData;
		}
	}
}

I am not trying to explain Silverlight development issues here but I would like to say the same things we do for Silverlight and JavaScript interaction. If someone is not familiar with the above code, Google Silverlight and JavaScript interaction. Almost every Silverlight book covers this topic.

C++ Silverlight Host

Add an edit box and a button in your dialog box in ATL application. We will enter some test in the edit box and write some code in the click handler of the button we just added.

Handler function for button click. Getting the user input in strData...

LRESULT CCMainDlg::OnBnClickedBtnSendDataToSilverlight
	(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
 	CAtlStringW strData;
 	GetDlgItem(IDC_EDIT_DATA).GetWindowTextW(strData);

Now coming to the main point - getting DISPID of Communicator object we just registered in Silverlight app.

IDispatch *pContent;
HRESULT hr = CXcpControlHost::GetXcpControlPtr()->get_Content(&pContent);
CComBSTR bstrMember(L"Communicator");
DISPID dispIDCommunicator = NULL;
hr = pContent->GetIDsOfNames(IID_NULL, &bstrMember, 1, 
	LOCALE_SYSTEM_DEFAULT, &dispIDCommunicator); 
if(FAILED(hr))
{
	::MessageBox(NULL, L"Failed to get the DISPID for Communicator", 
		L"Error :(", 0);
	return 0;
}

Once we have a valid dispID for Communicator object, we are ready to get the IDispatch* for Communicator object. It's a bit tricky here, we will make an invoke call with DISPATCH_PROPERTYGET.

VARIANT pVarResult;
DISPPARAMS dispparams; 
memset(&dispparams, 0, sizeof dispparams);
dispparams.cArgs = 0;
dispparams.rgvarg = NULL;
dispparams.cNamedArgs = 0;
// Getting IDispatch from for Scriptable object exposed by Silverlight Application. 
// Passing dispid for "Communicator" to get its IDispatch* using DISPATCH_PROPERTYGET.
hr = pContent->Invoke(dispIDCommunicator,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET,
&dispparams, &pVarResult, NULL, NULL);
if(FAILED(hr))
{
	::MessageBox(NULL, L"Failed to get the IDsipatch* of Communicator", 
		L"Error :(", 0);
	return 0;
}

IDispatch* pCommunicatorDispatch = pVarResult.pdispVal;

We stored IDispatch* for Communicator in pCommunicatorDispatch. Now we have to play with this pCommunicatorDispatch. This is our key player now, with pCommunicatorDispatch, we will get the DISPID for our exposed method (SetDataInEditBox) from a Silverlight app and then make an Invoke() call to call SetDataInEditBox() by passing that string into that function.

bstrMember = L"SetDataInTextBox";
DISPID dispIDSetDataInTextBox = NULL;
hr = pCommunicatorDispatch->GetIDsOfNames(IID_NULL,&bstrMember,1, 
	LOCALE_SYSTEM_DEFAULT,&dispIDSetDataInTextBox); 
EXCEPINFO pexcepinfo ;
if(SUCCEEDED(hr))
{
	dispparams.cArgs = 1;
	dispparams.cNamedArgs = 1;
	dispparams.rgvarg = new VARIANT[1];
	dispparams.rgvarg[0].vt = VT_BSTR;
	dispparams.rgvarg[0].bstrVal = SysAllocString(strData.GetString());
	hr = pCommunicatorDispatch->Invoke(dispIDSetDataInTextBox, 
		IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, 
		&dispparams, &pVarResult,&pexcepinfo,NULL);
	delete [] dispparams.rgvarg;

	if (hr != S_OK)
	{
		::MessageBox(NULL, L"Failed to Invoke Silverlight function", 
			L"Error :(", 0);
		return 0;
	}
}
else
{
	::MessageBox(NULL, L"Failed to get the DISPID for SetDataInTextBox 
		function defined in Silverlight.", L"Error :(", 0);
}

Conclusion

In this article, I've shown you how to call a function defined in a Silverlight application from C++ Silverlight host.

Hopefully, this article will help you a little bit for Silverlight and C++ interaction.

For questions, comments and remarks, please use the commenting section at the bottom of this article.

History

  • 20th May, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Syed Aftab Naqvi
Software Developer (Senior)
United States United States
Syed Aftab Hassan Naqvi

Comments and Discussions

 
Questionquestion about drag and drop Pin
icexile1-Dec-11 20:08
membericexile1-Dec-11 20:08 
QuestionAnd the keyboard ? Pin
sergiobuonanno4-Nov-11 6:07
membersergiobuonanno4-Nov-11 6:07 
GeneralMy vote of 5 Pin
Member 340074723-Oct-11 8:32
memberMember 340074723-Oct-11 8:32 
Questionunable to load xap file if i use smoothstreaming api to implement the sliverlight application. Pin
stevensxiong22-Feb-11 15:55
memberstevensxiong22-Feb-11 15:55 
AnswerRe: unable to load xap file if i use smoothstreaming api to implement the sliverlight application. Pin
hieronymus10029-May-14 3:41
memberhieronymus10029-May-14 3:41 
Generalwindowless and OleDraw Pin
Knives8922-Jan-11 15:27
memberKnives8922-Jan-11 15:27 
GeneralRe: windowless and OleDraw Pin
Syed Aftab Naqvi22-Jan-11 17:21
memberSyed Aftab Naqvi22-Jan-11 17:21 
GeneralRe: windowless and OleDraw [modified] Pin
Knives8922-Jan-11 22:50
memberKnives8922-Jan-11 22:50 
GeneralNeed code to send data from silverlight to c++ Pin
Member 184635823-Dec-10 11:55
memberMember 184635823-Dec-10 11:55 
GeneralRe: Need code to send data from silverlight to c++ Pin
Syed Aftab Naqvi2-Jun-11 10:23
memberSyed Aftab Naqvi2-Jun-11 10:23 
GeneralOnly works within the context of the UI thread Pin
driverscience16-Dec-10 8:30
memberdriverscience16-Dec-10 8:30 
GeneralRe: Only works within the context of the UI thread Pin
Syed Aftab Naqvi22-Jan-11 17:23
memberSyed Aftab Naqvi22-Jan-11 17:23 
QuestionC# Version? Pin
tomaten6-Nov-10 9:29
membertomaten6-Nov-10 9:29 
Generalhelp Pin
liuliu27-Jun-10 18:26
memberliuliu27-Jun-10 18:26 
GeneralRe: help Pin
Syed Aftab Naqvi28-Jun-10 14:11
memberSyed Aftab Naqvi28-Jun-10 14:11 
GeneralRe: help Pin
liuliu29-Jun-10 17:44
memberliuliu29-Jun-10 17:44 
GeneralRe: help Pin
Syed Aftab Naqvi30-Jun-10 6:57
memberSyed Aftab Naqvi30-Jun-10 6:57 
GeneralRe: help Pin
liuliu30-Jun-10 22:39
memberliuliu30-Jun-10 22:39 
GeneralRe: help Pin
Syed Aftab Naqvi1-Jul-10 11:45
memberSyed Aftab Naqvi1-Jul-10 11:45 
GeneralRe: help Pin
liuliu1-Jul-10 17:35
memberliuliu1-Jul-10 17:35 
GeneralRe: help Pin
Syed Aftab Naqvi2-Jul-10 10:54
memberSyed Aftab Naqvi2-Jul-10 10:54 
GeneralSilverlight 4 - COM integration Pin
Ernest Laurentin24-May-10 11:07
memberErnest Laurentin24-May-10 11:07 
GeneralRe: Silverlight 4 - COM integration Pin
Syed Aftab Naqvi24-May-10 11:26
memberSyed Aftab Naqvi24-May-10 11:26 
GeneralRe: Silverlight 4 - COM integration [modified] Pin
Ernest Laurentin24-May-10 20:21
memberErnest Laurentin24-May-10 20:21 
GeneralRe: Silverlight 4 - COM integration Pin
Syed Aftab Naqvi25-May-10 11:32
memberSyed Aftab Naqvi25-May-10 11:32 
Thanks for sending the link but I think my approach is little different than launching the silverlight app out of browser. e.g. In my case, I am hosting the silverlight control in a C++ app and it can give us more control like loading the different assemblies synchronously and/or asynchronously through C++ host.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 20 May 2010
Article Copyright 2010 by Syed Aftab Naqvi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid