Oct 1, 2010

Marshalling string to char* from C# to C++ in .NET

If you are looking for a way how to convert C# string variable to C++ char* variable you probably can't make it without knowing the "magic word" Marshalling. In .NET it's basicly a conversion between an unmanaged and a managed type. If you are calling C++ method from C# program and you want to pass a string as a parameter to this method (for example you have a GUI application written in C# and you want to proccess some user input by algorithm in library written in C++), without any conversion between types, you will get this error
cannot convert from 'System::String ^' to 'char *'
Here comes to scene marshalling. In namespace System.Runtime.InteropServices you can find the class Marshal. You simply add reference to you C++ project.
using namespace System::Runtime::InteropServices;
Then you just get pointer to your parameter using Marshal::StringToHGlobalAnsi. Use static_cast operator to convert the pointer to char* type.
void Adapter::SetSavePath(String^ path)
{
   IntPtr p = Marshal::StringToHGlobalAnsi(path);
   char* newCharStr = static_cast<char*>(p.ToPointer());
}
Then you just call your method in C#.
private void saveAsButton_Click(object sender, EventArgs e)
{
   FolderBrowserDialog fbd = new FolderBrowserDialog();

   if (fbd.ShowDialog() != DialogResult.Cancel)
   {
      if (fbd.SelectedPath != null)
      {
         string savePath = fbd.SelectedPath;
         adapter.SetSavePath(savePath); 
         //adapter is instance of Adapter
      }
   }
}
Easy right? I wish I knew two months ago... It costs me a lot of hair :))

D.Š.

Sep 29, 2010

Introduction

First of all I want to welcome you here. After pretty long time flirting with an idea of creating my own blog, I've finally crossed that bridge :) So let's take a closer look at me and my future activity here.

I am 24 years old male from Middle European country called Slovakia. Currently I'm also a student in final year of futher study in department of computer science in Vysoká škola báňská - Technická univerzita Ostrava, so if you are assuming, that this blog will be technically focused, you are probably right. This whole year I'll be working on my master's degree thesis and I'm pretty sure there will be some issues and presenting solutions for this issues is main topic of this blog right now.

Of course there will be some other stuff published too. Something personal, thoughts, feeling, etc... At lasts, I'll try to do so. Who knows, it may turns to free therapy sessions :) which I hope will not happend. I don't wanna be like one of those guys on youtube, talking to themselves (some of them acctualy have over 1M views!?) about some crap. (this one doesn't counts)

I don't mentioned, that I will publish in english, however it's not my native language, but I need to improve myself and I want information published here to be accessible for everyone and this is the best way to achiev that.

In the end I want to encourage that of you, who will actually read/use what i publish, to ask for things which are not clearly understandable or leave your solution/opinion/question/anything reasonable in a comment below and I'll answer you gratefully ;)

Best regards
D.Š.