hifive
06-17-2009, 11:18 AM
I am trying for the first time to use Windows Forms from C++/CLI but mixing it with my unmanaged code base.
Even after reading some articles on passing values between the two I am still a little confused.
Say I have an unmanaged header file that declares:
struct Options
{
std::string text;
int value;
};
Now I have an unmanaged .cpp that does this:
int __stdcall RunManagedOptionsForm(Options& options);
void main()
{
Options options;
RunManagedOptionsForm(options);
}
Then in my managed file I have:
int __stdcall RunManagedOptionsForm(Options& options)
{
OptionsForm^ form = gcnew OptionsForm();
form->ShowDialog();
options.text= form->text; //What to do here?
options.value = form->value;
}
I have seen code like:
String^ s = gcnew String("sample string");
IntPtr ip = Marshal::StringToHGlobalAnsi(s);
const char* str = static_cast<const char*>(ip.ToPointer());
Console::WriteLine("(managed) passing string...");
NativeTakesAString( str );
Marshal::FreeHGlobal( ip );
But I am not sure how to apply it in this situation?
Also if I play around with managed code that creates std::string's etc and pass that to unmanaged code is that dangerous?
Thanks
Even after reading some articles on passing values between the two I am still a little confused.
Say I have an unmanaged header file that declares:
struct Options
{
std::string text;
int value;
};
Now I have an unmanaged .cpp that does this:
int __stdcall RunManagedOptionsForm(Options& options);
void main()
{
Options options;
RunManagedOptionsForm(options);
}
Then in my managed file I have:
int __stdcall RunManagedOptionsForm(Options& options)
{
OptionsForm^ form = gcnew OptionsForm();
form->ShowDialog();
options.text= form->text; //What to do here?
options.value = form->value;
}
I have seen code like:
String^ s = gcnew String("sample string");
IntPtr ip = Marshal::StringToHGlobalAnsi(s);
const char* str = static_cast<const char*>(ip.ToPointer());
Console::WriteLine("(managed) passing string...");
NativeTakesAString( str );
Marshal::FreeHGlobal( ip );
But I am not sure how to apply it in this situation?
Also if I play around with managed code that creates std::string's etc and pass that to unmanaged code is that dangerous?
Thanks