MessageBox.Show with a CharArray

Hi all,

I've been trying to run the following code, without success:

char CharArray[6]

strcpy_s(CharArray, sizeof(CharArray), "Hello");

MessageBox::Show(CharArray);

It gives an error saying "error C2665 'System::Windows::Forms::MessageBox::Show': None of the 21 overloads could convert all the argument types".

I was suprised to get this error - as I assumed Show could deal with a Character Array...after all MessageBox::Show("Hello") is a character array as well, just a const one.

Does anyone have any ideas as to why this isn't working? I've tried all sorts of things, but none of them have worked. What do you suggest I do to get this working?

Cheers,

Robin
[768 byte] By [robintw] at [2007-11-20 1:38:19]
# 1 Re: MessageBox.Show with a CharArray
1st of all you should post this question in the managed C++ forum.

Anyways for your problem at hand it looks like your solution is configured for Unicode and you are trying to pass a char array to a function expecting a wchar_t array. Either declare a wide char array or convert you char array to wide char before sending it to the MessageBox::Show function.
PadexArt at 2007-11-9 12:07:22 >
# 2 Re: MessageBox.Show with a CharArray
Hi,

I'm sorry about that - I saw a Visual C++ forum and immediately decided that was the right one - my fault. Is there any way for me to move it now, or does a moderator have to do that?

I didn't realise this solution was using unicode - but then again I'm dealing with someone else's code...

I've tried declaring the CharArray as a wchar_t and then passing it to the MessageBox::Show function, but it still doesn't seem to work.

Cheers,

Robin
robintw at 2007-11-9 12:08:31 >
# 3 Re: MessageBox.Show with a CharArray
Oops. :) Simple C++ logic didn't extrapolated well to C++/Cli. here, you can use this code to achieve the desired result:

char CharArray[6];
strcpy_s(CharArray, sizeof(CharArray), "Hello");

String^ msg= gcnew String( CharArray);
MessageBox::Show( msg);
PadexArt at 2007-11-9 12:09:29 >
# 4 Re: MessageBox.Show with a CharArray
Thank you very much for the reply.

That way does indeed work, but I am suprised there is not a simpler way to do this. I find it very strange that the MessageBox::Show function will not take a Character Array...

The only reason I'm asking about a simpler way is that this software is being written to work in a system which is only one level removed from a safety critical system. Therefore we are trying to avoid all complexity as much as possible, and also trying to avoid (if possible) garbage collection. I have read the help and found that gcnew creates a new type with garbage collection, which might be a bit of a problem for us.

Thanks,

Robin
robintw at 2007-11-9 12:10:24 >
# 5 Re: MessageBox.Show with a CharArray
The only reason I'm asking about a simpler way is that this software is being written to work in a system which is only one level removed from a safety critical system. Therefore we are trying to avoid all complexity as much as possible, and also trying to avoid (if possible) garbage collection. I have read the help and found that gcnew creates a new type with garbage collection, which might be a bit of a problem for us.

I'm afraid you cannot avoid the garbage collector mechanism if you use C++/Cli. If you are just beginning the project and want to avoid this kind of issues you should consider using C++. What was the reason behind selecting C++/Cli?
PadexArt at 2007-11-9 12:11:34 >
# 6 Re: MessageBox.Show with a CharArray
[ redirected ]
cilu at 2007-11-9 12:12:26 >