自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

代碼拖不托管是浮云:飄過托管的邊界

開發(fā) 項(xiàng)目管理
寫這篇博文為了說明如何"托管"與'"非托管"互用問題。具體來講包括:如何在托管代碼中使用非托管代碼、如何在托管代碼中使用非托管dll、如何在非托管代碼中使用托管dll以及托管代碼。直接給出最直接的描述---代碼。

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

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

  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è)置項(xiàng)目的屬性生成為:“允許不安全代碼”。

這樣程序就可正常運(yùn)行,效果如下:

[[90669]] 

[[90670]]

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

前面在講計(jì)時(shí)器的時(shí)候提到過,下面給出一個(gè)完整可用的高性能計(jì)時(shí)器,順便給出調(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. //這是使用的一個(gè)計(jì)時(shí)器,拿這個(gè)來說明如何在托管代碼中使用非托管dll 
  8. namespace gray 
  9.     internal class HiPerfTimer 
  10.     { 
  11.         [DllImport("Kernel32.dll")] 
  12.         private static extern bool QueryPerformanceCounter(out long lpPerformanceCount); 
  13.  
  14.         [DllImport("Kernel32.dll")] 
  15.         private static extern bool QueryPerformanceFrequency(out long lpFrequency); 
  16.  
  17.         private long startTime, stopTime; 
  18.         private long freq; 
  19.  
  20.         // Constructor 
  21.         public HiPerfTimer() 
  22.         { 
  23.             startTime = 0; 
  24.             stopTime = 0; 
  25.  
  26.             if (QueryPerformanceFrequency(out freq) == false
  27.             { 
  28.                 // high-performance counter not supported 
  29.                 throw new Win32Exception(); 
  30.             } 
  31.         } 
  32.  
  33.         // Start the timer 
  34.         public void Start() 
  35.         { 
  36.             // lets do the waiting threads there work 
  37.             Thread.Sleep(0); 
  38.  
  39.             QueryPerformanceCounter(out startTime); 
  40.         } 
  41.  
  42.         // Stop the timer 
  43.         public void Stop() 
  44.         { 
  45.             QueryPerformanceCounter(out stopTime); 
  46.         } 
  47.  
  48.         // Returns the duration of the timer (in milliseconds) 
  49.         public double Duration 
  50.         { 
  51.             get 
  52.             { 
  53.                 return (double)(stopTime - startTime) * 1000 / (double)freq; 
  54.             } 
  55.         } 
  56.  
  57.         public void ClearTimer() 
  58.         { 
  59.             startTime = 0; 
  60.             stopTime = 0; 
  61.         } 
  62.     } 

用法很簡(jiǎn)單:

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

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

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

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

在非托管代碼中加載CLR運(yùn)行托管代碼,代碼如下:

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

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

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

原文鏈接:http://www.cnblogs.com/DebugLZQ/archive/2012/08/13/2636919.html

【編輯推薦】

責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2013-08-02 13:32:29

開源代碼代碼托管開源

2014-10-13 15:17:59

代碼托管

2023-05-30 16:02:34

云托管云計(jì)算自托管

2017-04-13 11:46:11

Linux VPS虛擬專屬服務(wù)器

2023-08-31 15:14:48

托管數(shù)據(jù)中心服務(wù)器

2017-04-13 14:04:12

互聯(lián)網(wǎng)

2009-08-07 13:22:04

服務(wù)器托管

2017-06-29 10:45:18

互聯(lián)網(wǎng)

2011-06-21 09:38:25

托管代碼非托管代碼

2010-01-25 15:55:50

托管C++

2023-07-05 16:02:04

開發(fā)后端工具

2017-05-31 14:14:11

互聯(lián)網(wǎng)

2009-04-02 15:21:43

c#IDisposeFinalize

2010-04-22 10:26:16

.NET互操作

2022-06-06 16:18:44

云計(jì)算云托管安全

2023-06-16 15:27:05

SaaSHeroku網(wǎng)絡(luò)

2021-09-06 10:12:55

云計(jì)算

2020-09-14 09:00:43

托管合同服務(wù)提供商數(shù)據(jù)中心

2011-08-24 12:49:56

SQL Server托管代碼

2013-06-28 14:23:54

云計(jì)算
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)