Drag-and-drop is a cloud-float the boundaries of hosting

Source: Internet
Author: User

This blog post explains how to interuse "hosting" with "unmanaged. Specifically, it includes: How to Use unmanaged code in managed code, how to use Unmanaged DLL in managed code, how to use hosted DLL in unmanaged code, and managed code. Directly give the most direct description-code.

1. Use unmanaged code in managed code

A feasible example is provided to briefly describe the function of the following code-grayscale images.

// The managed code calls the code written before the unmanaged code // debuglzq // unsafe {} private void pointer_click (Object sender, eventargs e) {If (curbitmap! = NULL) {mytimer. cleartimer (); mytimer. start (); rectangle rect = new rectangle (0, 0, curbitmap. width, curbitmap. height); system. drawing. imaging. bitmapdata bmp data = curbitmap. lockbits (rect, system. drawing. imaging. imagelockmode. readwrite, curbitmap. pixelformat); byte temp = 0; unsafe {byte * PTR = (byte *) (BMP data. scan0); For (INT I = 0; I <BMP data. height; I ++) {for (Int J = 0; j <BMP data. width; j ++) {temp = (byte) (0.299 * PTR [2] + 0.587 * PTR [1] + 0.114 * PTR [0]); PTR [0] = PTR [1] = PTR [2] = temp; PTR + = 3;} PTR + = BMP data. stride-BMP data. width * 3 ;}} curbitmap. unlockbits (BMP data); mytimer. stop (); timebox. TEXT = mytimer. duration. tostring ("####. # ") +" millisecond "; invalidate ();}}

To make the program run correctly, you need to set the project attribute to "allow Insecure code ".

In this way, the program runs properly, and the effect is as follows:

 

2. Use Unmanaged DLL in managed code

As mentioned earlier in timer, the following provides a complete and available high-performance timer, and an example of calling an unmanaged DLL is provided by the way. The Code is as follows:

Using system; using system. runtime. interopservices; using system. componentmodel; using system. threading; // debuglzq // specify gray {internal class hiperftimer {[dllimport ("kernel32.dll")] Private Static extern bool queryperformancecounter (out long lpperformancecount ); [dllimport ("kernel32.dll")] Private Static extern bool queryperformancefrequency (out long lpfrequency); private long starttime, stoptime; private long freq; // constructor public hiperftimer () {starttime = 0; stoptime = 0; If (queryperformancefrequency (Out freq) = false) {// high-performance counter not supported throw new win32exception ();}} // start the timer public void start () {// lets do the waiting threads there work thread. sleep (0); queryperformancecounter (Out starttime);} // stop the timer public void stop () {queryperformancecounter (Out stoptime );} // returns the duration of the timer (in milliseconds) Public double duration {get {return (double) (stoptime-starttime) * 1000/(double) freq ;}} public void cleartimer () {starttime = 0; stoptime = 0 ;}}}

Easy to use:

private HiPerfTimer myTimer=new HiPerfTimer();myTimer.Start();myTimer.Stop();myTimer.Duration//wanted

Write another example

Using system; using system. componentmodel; using system. runtime. interopservices; using system. Threading; namespace pinvoke {class program {static void main (string [] ARGs) {If (! Messagebeep (0) {// int32 err = marshal is not executed. getlastwin32error (); throw new win32exception (ERR) ;}} [dllimport ("user32.dll")] Private Static extern Boolean messagebeep (uint32 beeptype );}}

3-4. Call the managed DLL and write the managed code in the unmanaged code.

When talking about the CLR host in the previous blog, I encountered this problem. The managed assembly code is as follows:

Using system; namespace net. MST. eighth. simpleassembly {/// <summary> /// a simple "hosted" assembly, function is to output the input string // </Summary> public class simpleassembly {static int writestring (string s) {console. writeline ("CLR host output:" + S); return 1 ;}}}

Load the CLR running managed code in the unmanaged code. The Code is as follows:

// Debuglzq // http://www.cnblogs.com/debuglzq//c??+cl, run the runtime code #include "stdafx. H "# include <windows. h> // define the version of CLR # include <mscoree. h> # pragma comment (Lib, "mscoree. lib ") // load CLR to run the managed code void main (INT argc, _ tchar * argv []) {iclrruntimehost * phost; hresult hR = corbindtoruntimeex (null, null ,, clsid_clrruntimehost, iid_iclrruntimehost, (pvoid *) & phost); phost-> Start (); iclrcontrol * clrcontrol = NULL; HR = phost-> getclrcontrol (& clrcontrol ); DWORD * returnvalue = NULL; // start running the managed code phost-> executeindefaappappdomain (L ".. \\.. \\.. \ simpleassembly \ bin \ debug \ simpleassembly. DLL ", l" net. MST. eighth. simpleassembly. simpleassembly ", l" writestring ", l" http://www.cnblogs.com/DebugLZQ ", returnvalue); System (" pause "); // uninstall CLR at end}

The program running result is as follows:

The purpose of this article is to provide a practical method of "hosting"-"non-hosting" mutual calling. There is nothing to be circled. Please do not spray the debuglzq. Of course, if you think this blog post is helpful to you, please click "Green Channel" --- "follow debuglzq" below to share progress ~

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.