?
Solved

Convert c++ pointer to vb.net string

Posted on 2003-03-23
4
Medium Priority
?
663 Views
Last Modified: 2007-12-19
A c++ dll returns an int32 pointer to a string. How do I get the string value, not the pointer or the address, but the actual string value in vb.net?
0
Comment
Question by:charliecb2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
Salte earned 80 total points
ID: 8196189
err... what do you mean by an 'int32 pointer'?

do you mean an int32 holding a pointer to the string?

If so it depends on what kind of string the int32 pointer is pointing to. If it is a pointer to a System::String __gc * string object then it is simply that the variable is of the right type already and you just declare it in VB to return string and you should be all fine.

If the function have a 'char *' and return that as an int32 then for one thing that seems very odd but anyway if it does then you must first create a string object from that char string. This can probably not be done by VB so you have to write some C code that can interface to the function and do something like this:

System::String __gc * get_string(int32 val)
{
   // val is assumed to be a value so that
   // reinterpret_cast<const char *>(val)[0] is
   // the first character of the string etc..
   const char * str = reinterpret_cast<const char *>(val);

   // convert the string to wchar_t
   int len = strlen(str);
   wchar_t * wstr = new wchar_t[len];
   mbstowcs(wstr,str,len);
   System::String __gc * s = new System::String(wstr,len);
   delete [] wstr;
   return s;
}

Note, the mbstowcs conversion will depend upon your locale, but if the char is a plain ascii string it should be correct whatever your locale is.

If the value in the int32 is a pointer to a wchar_t string you skip that conversion and you get:

System::String __gc * get_string(int32 val)
{
   // val is assumed to be a value so that
   // reinterpret_cast<const char *>(val)[0] is
   // the first character of the string etc..
   const wchar_t * wstr = reinterpret_cast<const wchar_t *>(val);
   int len = wcslen(wstr);

   return new System::String(wstr,len);
}

Now, it is possible you didn't mean either of these but that the 'int32 pointer' is an int32 pointer (int *) which points to the string. In this case I really wonder what kind of string this is:

Is it a unicode string where each element is a unicode value 0..0x10ffff?

If so you can simply convert it to wchar_t. I believe that .net uses UTF-16 to store strings internally in memory so that a unicode code above 0xffff is stored using two codes (one in the range 0xd800..0xdbff and the other in the range 0xdc00..0xdfff) if so I am not quite sure how to make that wchar_t string for System::String, I would assume it is supposed to be a regular wchar_t string and not in the .net format so that .net will itself convert the values above 0x10000 to two element codes. In any case if you have no unicode values in the range 0x010000..0x10ffff this is not an issue and you can just go ahead and store each unicode value in a wchar_t element (make sure that no value are in the range 0xd800..0xdfff as that is an illegal range for unicode - it is used to mark codes in UTF-16 above 0x10000) and then go use it.

In any case your explanation of the situation wasn't really very clear so I had to do some guessing here but I hope I made the right guesses and that this code will help you out.

Alf
0
 
LVL 12

Expert Comment

by:Salte
ID: 8196197
Btw, if that pointer is already pointing to a System::String * object then you should of course just return that object to VB and tell VB that the function returns a string.

Alf
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9544077
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to Salte

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question