HotTester
HotTester

Reputation: 5768

Confused on CTS in .net

CTS does all the required conversion of datatypes so as to enable the execution of code by .net. Then how come when we try to implement pointers it is not mapped by it rather we have to explicitly convert the code ?

Upvotes: 0

Views: 158

Answers (1)

Hans Passant
Hans Passant

Reputation: 941525

I'll assume you mean Common Type System. The CLR has a very capable interop engine, called the P/Invoke marshaller. It is no trouble converting 'C' strings to System.String, it is automatic with the [DllImport] and [MarshalAs] attributes. Not all possible 'C' function signatures or struct declarations are compatible, you may occasionally have to marshal an IntPtr yourself with the Marshal class. Best thing to do is to provide an example of such a troublesome function.

Note that if you use String^ then you are probably writing code in C++/CLI. You then would be using C++ Interop to interop with legacy unmanaged code. [DllImport] certainly still works but is not commonly used in C++/CLI. C++ Interop is very powerful, much more so than the P/Invoke marshaller. But with power comes a price, you have to paper over the gross incompatibility between a char* and a Unicode string yourself. It isn't especially difficult with the marshal_as<> template class.

Upvotes: 1

Related Questions