非託管 呼叫非託管資源的使用

2022-02-09 12:15:55 字數 1476 閱讀 3097

呼叫非託管資源的使用

一.了解:

首先我們應該得了解什麼是託管資源,什麼又是非託管資源?帶著問題去找答案,解決問題是不是就容易多了呢,接下來我們先來了解關於託管和非託管。

好了廢話到此,是不是感覺好玩啊,不管做什麼,當做玩,那麼你會覺得很輕鬆。接下來進入正題:

託管:託管資源(managed resource)是dot net的乙個概念,指那些資源的**工作由.net

clr 的gc 機制自動完成,無需顯式釋放的資源。比如int,string,float,datetime等等物件,要說的是.net中超過80%的資源都是託管資源

非託管:

對於這類資源雖然垃圾**器(gc)可以跟蹤封裝非託管資源的物件的生存期,但它不了解具體如何清理這些資源。常見的非託管源有:

filestream,font,icon,image,matrix,object,odbcdatareader,oledbdatareader,pen,

regex,socket,streamwriter,timer,tooltip 等。

二.示例:

#region dllimport

[unmanagedfunctionpointer(callingconvention.stdcall)]         public delegate int onstate(intptr ptr);

[dllimport("wffmpeg64.dll", callingconvention = callingconvention.stdcall, charset = charset.ansi, entrypoint = "wf_transcode")]         public static extern int transcode(ref _context ctx, int async);

[dllimport("wffmpeg64.dll", callingconvention = callingconvention.stdcall, charset = charset.ansi, entrypoint = "wf_createthumbnail")]         public static extern int createthumbnail([marshalas(unmanagedtype.lpstr)]string filename, intptr ptr);

[dllimport("wffmpeg64.dll", callingconvention = callingconvention.stdcall, entrypoint = "wf_freeintptr")]         public static extern int free(ref intptr ptr);

#endregion

由於模擬較多,就不在這一一枚舉了。。。

學習使人優越,優越於學習之中。

託管資源和非託管資源

託管資源 net可以自動進行 的資源,主要是指託管堆上分配的記憶體資源。託管資源的 工作不需要人工干預的,有.net執行庫在合適時呼叫垃圾 器進行 非託管資源 是.net不知道如何 的資源,最常見的一類非託管資源是包裝作業系統資源的物件,如檔案,視窗,網路連線,資料庫連線,畫刷,圖示等。這類資源,垃...

託管呼叫非託管的DLL

dllimport createnewprocess.dll charset charset.unicode public static extern bool createprocess marshalas unmanagedtype.lpwstr string fullpath 以上是定義入口,...

C 託管資源和非託管資源

託管資源指的是.net可以自動進行 的資源,主要是指託管堆上分配的記憶體資源。託管資源的 工作是不需要人工干預的,有.net執行庫在合適呼叫垃圾 器進行 非託管資源指的是.net不知道如何 的資源,最常見的一類非託管資源是包裝作業系統資源的物件,例如檔案,視窗,網路連線,資料庫連線,畫刷,圖示等。這...