Ask Your Question
0

convert string^ to char* windows form application c++

asked 2013-10-16 22:14:56 -0600

zulfiqar gravatar image

updated 2013-10-17 06:13:05 -0600

i m reading path from textbox and then tried to open image

String^ P = path->Text;

IplImage* img = cvLoadImage(P);

It gives me the following error Error 1 error C2664: 'cvLoadImage' : cannot convert parameter 1 from 'System::String ^' to 'const char *'

Can anyone tell me how to convert it to char *.

edit retag flag offensive close merge delete

Comments

1

I have not worked on Windows Form applications, but what you ask for can be found in this link, hope this helps..

http://support.microsoft.com/kb/311259

Robionic gravatar imageRobionic ( 2013-10-17 02:46:51 -0600 )edit

@Robionic, it is best if you have a solution, that you copy the answer here. If that link dies, noone will find the solution anymore.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-10-17 06:06:48 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-10-17 06:11:11 -0600

To solve your problem, please first follow peoples guidance in your other topics and use the C++ interface!

For windows, the forms indeed use a terrible format, in a specified protected string format, but translating them to a Mat element container (which is used in C++ - api of openCV) can be done straightforward by this approach.

String^ temp_input = textbox_you_want_to_retrieve->Text;    
IntPtr pointer_to_text_string = Marshal::StringToHGlobalAnsi(temp_input);   
const char* string_for_opencv = static_cast<const char*>(pointer_to_text_string.ToPointer());
Mat image = imread(string_for_opencv);

This code will need you to do one more thing, to be able to use the marshalling functionality and that is to add the following on top of your header:

using namespace System::Runtime::InteropServices;

Or you can just print that complete command in front of the marchalling operations.

edit flag offensive delete link more

Comments

2

@StevenPuttemans Yes, sure I would do that next time.. :)

Robionic gravatar imageRobionic ( 2013-10-17 09:05:07 -0600 )edit

Question Tools

Stats

Asked: 2013-10-16 22:14:56 -0600

Seen: 2,276 times

Last updated: Oct 20 '13