代码拖不托管是浮云:飘过托管的边界

开发 项目管理
写这篇博文为了说明如何"托管"与'"非托管"互用问题。具体来讲包括:如何在托管代码中使用非托管代码、如何在托管代码中使用非托管dll、如何在非托管代码中使用托管dll以及托管代码。直接给出最直接的描述---代码。

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

给出个可行示例,简单的说明下下面这段代码的功能--“灰度化”图像。 

  1. //托管代码调用非托管代码 
  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.         } 

为了使程序能正确执行,需要设置项目的属性生成为:“允许不安全代码”。

这样程序就可正常运行,效果如下:

[[90669]] 

[[90670]]

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

前面在讲计时器的时候提到过,下面给出一个完整可用的高性能计时器,顺便给出调用非托管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.     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.     } 

用法很简单:

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

3-4.非托管代码中调用托管dll、写托管代码。

前一篇博文谈到CLR宿主的时候,遇到过这个问题,托管Assembly代码如下:

  1. using System; 
  2. namespace NET.MST.Eighth.SimpleAssembly 
  3.     /// <summary> 
  4.     /// 一个简单的“托管”程序集,功能是输出传入的字符串 
  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运行托管代码,代码如下:

  1. //DebugLZQ 
  2. //http://www.cnblogs.com/DebugLZQ 
  3. //C++工程中加载CLR,运行托管代码 
  4. #include "stdafx.h" 
  5. #include <windows.h> 
  6. //这里定义加载哪个版本的CLR 
  7. #include <MSCorEE.h>    
  8.  
  9. #pragma   comment(lib,"MSCorEE.lib") 
  10.  
  11. //加载CLR,从而运行托管代码 
  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.     //开始运行托管代码 
  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.     //结束时卸载CLR 

程序运行结果如下:

   文章旨在给出了一种“托管”--“非托管”互相调用的切实可行的方法,没有什么可圈可点的地方。

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

【编辑推荐】

责任编辑:彭凡 来源: 博客园
相关推荐

2013-08-02 13:32:29

开源代码代码托管开源

2014-10-13 15:17:59

代码托管

2023-05-30 16:02:34

云托管云计算自托管

2017-04-13 11:46:11

Linux VPS虚拟专属服务器

2023-08-31 15:14:48

托管数据中心服务器

2017-04-13 14:04:12

互联网

2017-06-29 10:45:18

互联网

2009-08-07 13:22:04

服务器托管

2011-06-21 09:38:25

托管代码非托管代码

2010-01-25 15:55:50

托管C++

2017-05-31 14:14:11

互联网

2023-07-05 16:02:04

开发后端工具

2009-04-02 15:21:43

c#IDisposeFinalize

2010-04-22 10:26:16

.NET互操作

2011-08-24 12:49:56

SQL Server托管代码

2022-06-06 16:18:44

云计算云托管安全

2021-09-06 10:12:55

云计算

2023-06-16 15:27:05

SaaSHeroku网络

2020-09-14 09:00:43

托管合同服务提供商数据中心

2013-06-28 14:23:54

云计算
点赞
收藏

51CTO技术栈公众号