String^ declaration in C++ - visual-c++

As far as I am aware C++ string declaration follows the form:
std::string param;
I was walking through a code and realised the declaration of string is done this way:
System::String^ param;
Can anyone share light on this declaration?! Is this Microsoft Visual C++ string declaration or a special library which provides another alternative to using C++ string.

It's Microsoft-specific, and is part of a language they call C++/CLI. This syntax declares a Common Language Runtime (CLR) String variable (the same kind you get when you declare a string in C#). These are not directly interchangeable with the several C/C++ string types, but Microsoft provides marshalling facilities to convert CLR String objects to unmanaged strings and vice versa.
C++/CLI enables developers to create programs that bridge regular C++ classes/functions (otherwise called "unmanaged code") with CLR classes/functions (otherwise called "managed code"). Microsoft also exposes lower-level features of the CLR to C++/CLI, some that are exposed to C# too (like pointers), and some that aren't (like finer granularity over member access levels).
It's useful if you want to use an existing C or C++ library in a language like C# (by making the bindings in C++/CLI then exposing them to the CLR without going through P/Invokes), or if you want to port an existing unmanaged C/C++ library or application to a managed environment.

This is C++/CLI syntax for a handle to an object on the managed heap.

Related

C++/CX - How to convert a number stored as String^ to byte?

I have a C++/CX Windows 8 application and I need to do something similar to the following conversion:
String^ foo = "32";
byte bar = <the numeric value of foo>
How can I convert the number stored within the String^ into the byte type? I am lost without all of the C# magic that I normally use to achieve this!
Thanks in advance for any help on this.
You are getting into trouble by assuming that C++/CX resembles C#. That's not the case at all, it is pure C++ with just some language extensions to make dealing with WinRT types easier. This is not appropriate use of the Platform::String type, it is not a general purpose string class. That's already covered by the standard C++ library. The class was intentionally crippled to discourage the usage you have in mind. This MSDN library article explains it well:
Use the Platform::String Class when you pass strings back and forth to methods in Windows Runtime classes, or when you are interacting with other Windows Runtime components across the application binary interface (ABI) boundary. The Platform::String Class provides methods for several common string operations, but it's not designed to be a full-featured string class. In your C++ module, use standard C++ string types such as wstring for any significant text processing, and then convert the final result to Platform::String^ before you pass it to or from a public interface. It's easy and efficient to convert between wstring or wchar_t* and Platform::String.
So the appropriate code ought to resemble:
#include <string>
...
std::wstring foo(L"32");
auto bar = static_cast<unsigned char>(std::stol(foo));

Delphi XE3: What are the new types, MarshaledString and MarshaledAString used for?

In Winapi.Windows.pas of Delphi XE3, many C types that were formerly defined as PWideChar and PAnsiChar now are defined as MarshaledString and MarshaledAString respectively (e.g. PWChar, LPSTR):
PWChar = MarshaledString;
LPSTR = MarshaledAString;
Indeed, in System.pas, MarshaledString and MarshaledAString are equivalent to PWideChar and PAnsiChar respectively,
MarshaledString = PWideChar;
MarshaledAString = PAnsiChar;
but what is the background behind this decision? I mean, why Embarcadero should redefine such C string types?
Embarcadero is working on a next-generation compiler for mobile development. The changes you see are related to that effort. The technical details are not public yet, so nobody with info about it is allowed to say anything further about it.
From official documentation:
System.MarshaledString is an alias for PChar.
System.MarshaledAString is an alias for PAnsiChar.
MarshaledString is meant to be used on mobile
platform to keep with the notion of eliminating pointers and looking
forward, the new Delphi Next-Generation Mobile Compiler requires a
more explicit style of marshalling data to/from external APIs and
libraries.

c# : how to convert c# string to c++ wstring and vice-versa

c# code-
string s="おはよう";
I want to send s to c++ dll, as wstring..
how to convert string to wstring in c# ?
A std::wstring is a C++ object, allocated by the C++ runtime and having an implementation-dependent internal format. You might be able to figure out how to create one of those in a C# program and pass it to the unmanaged C++ code, but doing so would be somewhat difficult and fraught with peril. Because the internal structure of a std::wstring is implementation dependent, any change to the C++ compiler or runtime libraries break your solution.
What you're trying to do is typically done by writing an interface layer in C++ that takes an LPTStr parameter, converts it to a std::wstring, and then calls the C++ function that you wanted to call. That is, if the function you want to call is declared as:
int Foo(std::wstring p);
You would write an interface function:
int FooCaller(LPTSTR p)
{
std::wstring str = p;
return Foo(str);
}
And you call FooCaller from your C# program.
In short, C# can't create and pass a std::wstring, so you use a translation layer.

How to Marshall C++ Native Objects to Managed C++ CLI

I have bunch of native C++ objects and classes contains DTL maps, maps of maps and lists and vectors.
I need to call managed C++ functions from C++ native code and need to pass these native objects and STL containers(lists,maps , maps of maps) to C++/CLI. It needs to marshal or some how serialize these objects. How can I do that with out any problem. So that After marshalling and serializing back to managed C++/CLI , maps should marshalled with dictionaries and dictionaries of dictionaries, stl list with managed List<> and so on..
how can I achieve this for all cases? Is it requires complex handling of marshalling issues...?
Regards
Usman
STL memory layout is implementation specific. E.g. sizeof(std::vector) is 16 in release and 20 in debug mode when you use the implementation comes with Visual C++. And you have pointers in STL classes that can't be marshaled meaningfully to managed memory. You can switch to platform-independent C or COM types in the interface (e.g. pass an array with a count parameter or a safe array) if you want to do marshaling as .Net has better understanding on these types. I recommend COM because it has richer types and support other languages in case you need it.
Alternatively if you need speed you can write a marshal_as template function to do the conversion so you can reuse the marshaling code or even the marshaling buffer, or write a managed wrapper for your C++ objects.
If the data being marshaled is too large to fit in the memory you can also serialize the data to temp file or database and read them back from managed code in chunks.

Is there a compiled* programming language with dynamic, maybe even weak typing?

I wondered if there is a programming language which compiles to machine code/binary (not bytecode then executed by a VM, that's something completely different when considering typing) that features dynamic and/or weak typing, e.g:
Think of a compiled language where:
Variables don't need to be declared
Variables can be created during runtime
Functions can return values of different types
Questions:
Is there such a programming language?
(Why) not?
I think that a dynamically yet strong typed, compiled language would really sense, but is it possible?
I believe Lisp fits that description.
http://en.wikipedia.org/wiki/Common_Lisp
Yes, it is possible. See Julia. It is a dynamic language (you can write programs without types) but it never runs on a VM. It compiles the program to native code at runtime (JIT compilation).
Objective-C might have some of the properties you seek. Classes can be opened and altered in runtime, and you can send any kind of message to an object, whether it usually responds to it or not. In that way, you can implement duck typing, much like in Ruby. The type id, roughly equivalent to a void*, can be endowed with interfaces that specify a contract that the (otherwise unknown) type will adhere to.
C# 4.0 has many, if not all of these characteristics. If you really want native machine code, you can compile the bytecode down to machine code using a utility.
In particular, the use of the dynamic keyword allows objects and their members to be bound dynamically at runtime.
Check out Anders Hejlsberg's video, The Future of C#, for a primer:
http://channel9.msdn.com/pdc2008/TL16/
Objective-C has many of the features you mention: it compiles to machine code and is effectively dynamically typed with respect to object instances. The id type can store any class instance and Objective-C uses message passing instead of member function calls. Methods can be created/added at runtime. The Objective-C runtime can also synthesize class instance variables at runtime, but local variables still need to be declared (just as in C).
C# 4.0 has many of these features, except that it is compiled to IL (bytecode) and interpreted using a virtual machine (the CLR). This brings up an interesting point, however: if bytecode is just-in-time compiled to machine code, does that count? If so, it opens to the door to not only any of the .Net languages, but Python (see PyPy or Unladed Swallow or IronPython) and Ruby (see MacRuby or IronRuby) and many other dynamically typed languages, not mention many LISP variants.
In a similar vein to Lisp, there is Factor, a concatenative* language with no variables by default, dynamic typing, and a flexible object system. Factor code can be run in the interactive interpreter, or compiled to a native executable using its deploy function.
* point-free functional stack-based
VB 6 has most of that
I don't know of any language that has exactly those capabilities. I can think of two that have a significant subset, though:
D has type inference, garbage collection, and powerful metaprogramming facilities, yet compiles to efficient machine code. It does not have dynamic typing, however.
C# can be compiled directly to machine code via the mono project. C# has a similar feature set to D, but again without dynamic typing.
Python to C probably needs these criteria.
Write in Python.
Compile Python to Executable. See Process to convert simple Python script into Windows executable. Also see Writing code translator from Python to C?
Elixir does this. The flexibility of dynamic variable typing helps with doing hot-code updates (for which Erlang was designed). Files are compiled to run on the BEAM, the Erlang/Elixir VM.
C/C++ both indirectly support dynamic typing using void*. C++ example:
#include <string>
int main() {
void* x = malloc(sizeof(int))
*(int*)x = 5;
x = malloc(sizeof(std::string));
*(std::string*x) = std::string("Hello world");
free(x);
return 0;
}
In C++17, std::any can be used as well:
#include <string>
#include <any>
int main() {
std::any x = 5;
x = std::string("Hello world");
return 0;
}
Of course, duck typing is rarely used or needed in C/C++, and both of these options have issues (void* is unsafe, std::any is a huge performance bottleneck).
Another example of what you may be looking for is the V8 engine for JavaScript. It is a JIT compiler, meaning the source code is compiled to bytecode and then machine code at runtime, although this is hidden from the user.

Resources