Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Convert c++ pointer to vb.net string

Posted on 2003-03-23
Medium Priority
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?
Question by:charliecb2
  • 2
LVL 12

Accepted Solution

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];
   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.

LVL 12

Expert Comment

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.

LVL 11

Expert Comment

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.


-bcl (bcladd)
EE Cleanup Volunteer

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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

571 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