visual c++ - Converting a String^ to a const void* in C++ -
i using c++/cli create gui controls external gpib device. gui has textbox user can enter voltage. able read voltage textbox this...
string^ v1 = textbox1->text; assuming user enters decimal number textbox, need concatenate value other text , produce const void* pass gpib library command.
so question how can convert string^ const void*? able convert string^ double this...
double volt1 = double::parse(textbox1->text); so solution how convert double const void* work well.
it's odd external library wants const void* , not character pointer, assuming wants ansi string, can use marshal_context class convert string^ const char* pointer:
// marshal_context_test.cpp // compile with: /clr #include <stdlib.h> #include <string.h> #include <msclr\marshal.h> using namespace system; using namespace msclr::interop; int main() { marshal_context^ context = gcnew marshal_context(); string^ message = gcnew string("test string marshal"); const char* result; result = context->marshal_as<const char*>( message ); delete context; return 0; } (example code taken here).
of course if library wanted unicode string use marshal_as<const wchar_t*> instead.
Comments
Post a Comment