Bytes
Source: excellent products
Http://blog.elitehome.cn
Author: Hua Yu
Over the past few days, I have been studying the managed and unmanaged hybrid programming of VC. net.
As an excellent development platform,. Net enables developers to quickly develop interfaces and applications. However, it is easy to decompile the. NET hosted code, which brings a lot of trouble to developers. The unmanaged code of VC. Net can effectively prevent decompilation, at least not so easy. A very useful development method of VC. NET is the hybrid development of managed code and unmanaged code.
C # as a new type of pure managed development language, it has an extremely simple learning process in terms of usage, so that learners can quickly master and use it. Therefore, we can use the DLL developed by C # For hybrid programming, which can be easily referenced in VC. net.
In VC. Net hybrid programming, the most difficult is not the difficult and complex VC ++ language, but the data conversion between hosting and hosting.
A relatively simple and reliable method is found:
Use CLI: array for intermediate conversion.
CLI: array can be used in both managed and unmanaged code, and can easily change the unmanaged char array type to CLI: array type, and through the managed system: text:: utf8encoding is converted to the hosted string ^ type.
Because a large number of unmanaged data types can be converted to Char arrays, the CLI: array type is actually the byte [] type in the hosted code, therefore, it can be easily converted into various managed data types.
An example is as follows:
- String ^ converttostring (char * inchar, int Len)
- {
- CLI: array <unsigned char> ^ cli_array = gcnew CLI: array <unsigned char> (LEN );
- For (INT I = 0; I <Len; I ++)
- Cli_array [I] = inchar [I];
- System: Text: utf8encoding ^ encoding = gcnew system: Text: utf8encoding ();
- Return encoding-> getstring (cli_array );
- }
- Char * converttochar (string ^ instring)
- {
- Int Len = _ instring-> length;
- CLI: array <unsigned char> ^ cli_array = gcnew CLI: array <unsigned char> (LEN );
- System: Text: utf8encoding ^ encoding = gcnew system: Text: utf8encoding ();
- Encoding-> getbytes (instring, 0, Len, cli_array, 0 );
- Char * outchar = new char [Len];
- For (INT I = 0; I <Len; I ++)
- Outchar [I] = cli_array [I];
- Return outchar
- }
In VC. net, nullptr is used to replace null.