日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
代碼拖不托管是浮云:飄過托管的邊界

1.托管代碼中使用非托管代碼

創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)遼陽,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220

給出個可行示例,簡單的說明下下面這段代碼的功能--“灰度化”圖像。

 
 
 
 
  1. //托管代碼調(diào)用非托管代碼
  2. //DebugLZQ以前寫的
  3. //unsafe{}中代碼為非托管代碼
  4. private void pointer_Click(object sender, EventArgs e)
  5.         {
  6.             if (curBitmap != null)
  7.             {
  8.                 myTimer.ClearTimer();
  9.                 myTimer.Start();
  10.                 Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
  11.                 System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
  12.                 byte temp = 0;
  13.                 unsafe
  14.                 {
  15.                     byte* ptr = (byte*)(bmpData.Scan0);
  16.                     for (int i = 0; i < bmpData.Height; i++)
  17.                     {
  18.                         for (int j = 0; j < bmpData.Width; j++)
  19.                         {
  20.                             temp = (byte)(0.299 * ptr[2] + 0.587 * ptr[1] + 0.114 * ptr[0]);
  21.                             ptr[0] = ptr[1] = ptr[2] = temp;
  22.                             ptr += 3;
  23.                         }
  24.                         ptr += bmpData.Stride - bmpData.Width * 3;
  25.                     }
  26.                 }
  27.                 curBitmap.UnlockBits(bmpData);
  28.                 myTimer.Stop();
  29.                 timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒"; 
  30.                 Invalidate();
  31.             }
  32.         }

為了使程序能正確執(zhí)行,需要設(shè)置項目的屬性生成為:“允許不安全代碼”。

這樣程序就可正常運行,效果如下:

2.托管代碼中使用非托管dll

前面在講計時器的時候提到過,下面給出一個完整可用的高性能計時器,順便給出調(diào)用非托管dll的示例。代碼如下:

  
 
 
 
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.ComponentModel;
  4. using System.Threading;
  5. //DebugLZQ
  6. //www.cnblogs.com/DebugLZQ
  7. //這是使用的一個計時器,拿這個來說明如何在托管代碼中使用非托管dll
  8. namespace gray
  9. {
  10.     internal class HiPerfTimer
  11.     {
  12.         [DllImport("Kernel32.dll")]
  13.         private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
  14.         [DllImport("Kernel32.dll")]
  15.         private static extern bool QueryPerformanceFrequency(out long lpFrequency);
  16.         private long startTime, stopTime;
  17.         private long freq;
  18.         // Constructor
  19.         public HiPerfTimer()
  20.         {
  21.             startTime = 0;
  22.             stopTime = 0;
  23.             if (QueryPerformanceFrequency(out freq) == false)
  24.             {
  25.                 // high-performance counter not supported
  26.                 throw new Win32Exception();
  27.             }
  28.         }
  29.         // Start the timer
  30.         public void Start()
  31.         {
  32.             // lets do the waiting threads there work
  33.             Thread.Sleep(0);
  34.             QueryPerformanceCounter(out startTime);
  35.         }
  36.         // Stop the timer
  37.         public void Stop()
  38.         {
  39.             QueryPerformanceCounter(out stopTime);
  40.         }
  41.         // Returns the duration of the timer (in milliseconds)
  42.         public double Duration
  43.         {
  44.             get
  45.             {
  46.                 return (double)(stopTime - startTime) * 1000 / (double)freq;
  47.             }
  48.         }
  49.         public void ClearTimer()
  50.         {
  51.             startTime = 0;
  52.             stopTime = 0;
  53.         }
  54.     }
  55. }

用法很簡單:

  
 
 
 
  1. private HiPerfTimer myTimer=new HiPerfTimer();
  2. myTimer.Start();
  3. myTimer.Stop();
  4. myTimer.Duration//wanted

3-4.非托管代碼中調(diào)用托管dll、寫托管代碼。

前一篇博文談到CLR宿主的時候,遇到過這個問題,托管Assembly代碼如下:

  
 
 
 
  1. using System;
  2. namespace NET.MST.Eighth.SimpleAssembly
  3. {
  4.     /// 
  5.     /// 一個簡單的“托管”程序集,功能是輸出傳入的字符串
  6.     /// 
  7.     public class SimpleAssembly
  8.     {
  9.         static int WriteString(String s)
  10.         {
  11.             Console.WriteLine("CLR Host Output:" + s);
  12.             return 1;
  13.         }
  14.     }
  15. }

在非托管代碼中加載CLR運行托管代碼,代碼如下:

 
 
 
 
  1. //DebugLZQ
  2. //http://www.cnblogs.com/DebugLZQ
  3. //C++工程中加載CLR,運行托管代碼
  4. #include "stdafx.h"
  5. #include 
  6. //這里定義加載哪個版本的CLR
  7. #include    
  8. #pragma   comment(lib,"MSCorEE.lib")
  9. //加載CLR,從而運行托管代碼
  10. void main(int argc, _TCHAR* argv[])
  11. {
  12.     ICLRRuntimeHost *pHost;
  13.     HRESULT hr=CorBindToRuntimeEx(
  14.         NULL,
  15.         NULL,
  16. ,
  17.         CLSID_CLRRuntimeHost,
  18.         IID_ICLRRuntimeHost,
  19.         (PVOID*)&pHost);
  20.     
  21.     pHost->Start();
  22.     ICLRControl* clrControl = NULL;
  23.     hr = pHost->GetCLRControl(&clrControl);
  24.     DWORD* returnvalue=NULL;
  25.     //開始運行托管代碼
  26.     pHost->ExecuteInDefaultAppDomain(
  27.      L"..\\..\\..\\SimpleAssembly\\bin\\Debug\\SimpleAssembly.dll",
  28.      L"NET.MST.Eighth.SimpleAssembly.SimpleAssembly",
  29.      L"WriteString",
  30.      L"http://www.cnblogs.com/DebugLZQ",
  31.      returnvalue);
  32.     
  33.     system("pause");
  34.     //結(jié)束時卸載CLR
  35. }

程序運行結(jié)果如下:

   文章旨在給出了一種“托管”--“非托管”互相調(diào)用的切實可行的方法,沒有什么可圈可點的地方。


網(wǎng)站欄目:代碼拖不托管是浮云:飄過托管的邊界
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/djgedsg.html