C# Delegates in vc++
-
Hi, Im using C# in VC++ for WPF. I create one Delegates in C# as follow
public delegate void NameTab(string msg);
public static event NameTab CallClick;private void Callbutton_Click(object sender, RoutedEventArgs e)
{
String msg = comboBox1.Text;
CallClick(msg);
}Im using in my VC++ code as follow
Tabcontrol::SecondTab::CallClick += gcnew Tabcontrol::SecondTab::NameTab(OnCallClick);
And OnCallClick fucntion is
static void OnCallClick(CString msg)
{
}But it shows error error C3352: 'OnCallClick' : the specified function does not match the delegate type 'void (System::String ^)' Im using button click event handler and its woking perfectly.But this only shows error.As i want to get the text in combox,im passing that text as parametes and im uisng that text in vc++ code. pls help me.
Anu
-
Hi, Im using C# in VC++ for WPF. I create one Delegates in C# as follow
public delegate void NameTab(string msg);
public static event NameTab CallClick;private void Callbutton_Click(object sender, RoutedEventArgs e)
{
String msg = comboBox1.Text;
CallClick(msg);
}Im using in my VC++ code as follow
Tabcontrol::SecondTab::CallClick += gcnew Tabcontrol::SecondTab::NameTab(OnCallClick);
And OnCallClick fucntion is
static void OnCallClick(CString msg)
{
}But it shows error error C3352: 'OnCallClick' : the specified function does not match the delegate type 'void (System::String ^)' Im using button click event handler and its woking perfectly.But this only shows error.As i want to get the text in combox,im passing that text as parametes and im uisng that text in vc++ code. pls help me.
Anu
that is because your callback needs to accept a System.String datatype instead of CString. If you need to marshal String^ to Cstring just do the following.
static void OnCallClick(String ^msg)
{
CString nativeString = msg;
}And guess what! You may have noticed that you don't have to marshal System.String into a CString in your code. That is because of a little trick in cstringt.h
#if defined(__cplusplus_cli)
template <class SystemString> CStringT( SystemString^ pString ) : CThisSimpleString( StringTraits::GetDefaultManager() ) { cli::pin\_ptr<const System::Char> pChar = PtrToStringChars( pString ); const wchar\_t \*psz = pChar; \*this = psz; }
this function handles the marshaling for you :)
Don't be overcome by evil, but overcome evil with good