Managed and unmanaged hybrid programming of VC. net

Source: Internet
Author: User
Tags hosting
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:

  1. String ^ converttostring (char * inchar, int Len)
  2. {
  3. CLI: array <unsigned char> ^ cli_array = gcnew CLI: array <unsigned char> (LEN );
  4. For (INT I = 0; I <Len; I ++)
  5. Cli_array [I] = inchar [I];
  6. System: Text: utf8encoding ^ encoding = gcnew system: Text: utf8encoding ();
  7. Return encoding-> getstring (cli_array );
  8. }
  9. Char * converttochar (string ^ instring)
  10. {
  11. Int Len = _ instring-> length;
  12. CLI: array <unsigned char> ^ cli_array = gcnew CLI: array <unsigned char> (LEN );
  13. System: Text: utf8encoding ^ encoding = gcnew system: Text: utf8encoding ();
  14. Encoding-> getbytes (instring, 0, Len, cli_array, 0 );
  15. Char * outchar = new char [Len];
  16. For (INT I = 0; I <Len; I ++)
  17. Outchar [I] = cli_array [I];
  18. Return outchar
  19. }

In VC. net, nullptr is used to replace null.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.