Thread: Use 'string' type in VS2010 starting from empty project

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    77

    Use 'string' type in VS2010 starting from empty project

    I'm using Visual Studio 2010 Professional - old, but I have to use this one. The build toolset is marked as V100.
    I create a C++ empty project, with, for the beginning, just <Windows.h> to include. It was supposed that I then add the necessary libraries files so that to make the code buildable.
    Just for info, the scope of the project is to create a dll and call it in C#. Everything goes fine with one exception, and I didn't find the solution: it's about the 'string' type. If I define as 'string' a variable in C++ it is not recognized. I have tried with 'using namespace std;' but got the error that 'std' is not recognized either.
    I have used 'char *' instead and it works when marshalling to C#. The need to use 'string' appears when I want to export a C++ class to C# and the C++ constructor called in C# must instantiate a 'string'. I've tried all kinds of C# and C++ methods and functions, I'm not happy with the result or the complexity.
    Therefore, how can I, for the given development environment - VS2010, C++, empty project - have access to a 'string' type?

  2. #2
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    When performing cross language work, Opariti, strings become a large problem because string representations are not standardized as is the case, for example, with 32 bit integral quantities, i.e., signed integers, unsigned integers, etc. Use of the C++ std::string or std::wstring is particularly problematic. That is an entity unique to the C++ Standard Library. I don't believe .NET is aware of it in any sense.

    The lowest common denominator with regard to strings are character or wchar_t null terminated arrays. With regard to .NET specifically, I believe the BSTR data type will likely take you farthest. It is defined in various Windows headers, and implemented in various Windows specific libs such as oluauto32.lib. .NET is built upon COM (Component Object Model), and that is essentially the COM string data type, and .NET recognizes it. Essentially, a BSTR is a null terminated wide character array with the length cached right at the start of the memory allocation for the string. I could give you examples of its use if you like.

    Thing is I interoperate between different languages all the time, and either simple null terminated character arrays, or COM BSTRs are the way to go. But my belief is that the C++ std::string or std::wstring class will get you nowhere. The only way you would be able to make any use of that whatsoever would be through its std::wstring::c_str() member, which brings you right back around to what I told you, that is, null terminated strings are the lowest common denominator.

    Here is a pretty good link about interoperating between C++ and BSTR consuming clients ...

    https://social.msdn.microsoft.com/Fo...rum=vblanguage

    I'm using Visual Studio 2010 Professional - old, but I have to use this one.
    Don't feel bad. I still mostly use Visual Studio 2008, which works perfectly, which is more than I can say for the C++ compiler that ships with Visual Studio 2015. I've just spent the past four days trying to overcome a bug in this latter compiler, which in some cases can't satisfactorily execute operator delete [] calls, and throws all kinds of bizarre linker errors on completely valid code.
    Last edited by freddie; 02-22-2016 at 08:42 PM. Reason: add something

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    77
    Many thanks freddie, that's it! Somewhere in my mind 'string' was in the C++ language set, which is not true, it is a class. The reference to use is char* or char[].

  4. #4
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    One thing I might add is that using std::wstring within your C++ dll would be perfectly fine, and likely you are comfortable with that. Where the problem comes in is the interface between the C++ dll and .NET. Of course that interface is through exported functions with their parameter lists. Incoming strings of whatever form could be converted to std::wstring within your dll for you to make use of the std::wstring members. Same thing with outgoing strings. You could convert them from std::wstring to BSTRs quite easily.

    The more I think about it, I'm thinking the .NET string type must internally be a BSTR. The reason I say that is because a number of years ago I wrote an ActiveX Grid Control using all low level COM code, i.e., no ATL wrappers or anything like that, and I tested it in VB.NET, and it works fine there. The BSTR parameters I used just showed up as regular .NET strings, and the intellisense IDE recognized them as such.

    If you have a std::wstring object such as this...

    Code:
    std::wstring s1(L"Hello, World!");
    ...to convert that to a BSTR its just something ike this...

    Code:
    BSTR strHello=SysAllocString(s1.c_str());
    The main issue in using these creatures from C++ is you need to be very careful about memory leaks, as most allocated BSTRs need to be released. That article by Allen McKinney above goes into that.
    Last edited by freddie; 02-23-2016 at 09:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting a serious project
    By Epy in forum General Discussions
    Replies: 18
    Last Post: 12-27-2011, 01:55 AM
  2. [VS2010] adding *.dll files to a project
    By kulfon in forum C++ Programming
    Replies: 3
    Last Post: 06-27-2011, 07:08 AM
  3. Creating and Empty MFC project in Visual Studio?
    By Swerve in forum Windows Programming
    Replies: 7
    Last Post: 11-01-2008, 04:43 PM
  4. Thinking about starting a project??
    By code2d in forum Tech Board
    Replies: 17
    Last Post: 01-11-2007, 09:55 PM
  5. MFC in an empty project
    By cunnus88 in forum Windows Programming
    Replies: 0
    Last Post: 10-09-2005, 09:19 AM

Tags for this Thread