FindResource 如何獲取自定義資源

2022-07-07 17:57:18 字數 1596 閱讀 8278

來自:

在.exe中可以這樣新增和訪問:

1,新增資料/資源:

在resource(資源)中 通過 「add resource -> import...」選擇需要新增的 資料/資源,其中的 resource type 可以自己命名,

需要注意的是需要利用字串命名,譬如可以為"myrestype",資源id可以為字串,譬如為"idr_data",也可以使id譬如為idr_data,這兩種方式在使用findresource函式是有所區別。

2,訪問資料/資源:

在.exe中當前load的resource即為.exe中的resource,因此在使用findresource,loadresource時,引數hmodule可以為null。具體使用如下:

// string 方式

hrsrc hr = ::findresource(null,l"idr_haarcascade",l"myrestype");

if (null == hr)

ulong nressize = ::sizeofresource(null,hr); // data size/length

hglobal hg= ::loadresource(null, hr);

if (null == hg || nressize <= 0)

lpbyte pdata = (lpbyte)lockresource(hg); // data ptr

// id方式

cstring stritem = makeintresource(idr_haarcascade);

hrsrc hr = ::findresource(null,stritem,l"myrestype");

if (null == hr)

ulong nressize = ::sizeofresource(null,hr); // data size/length

hglobal hg= ::loadresource(null, hr);

if (null == hg || nressize <= 0)

lpbyte pdata = (lpbyte)lockresource(hg); // data ptr

上述方法在dll中會出現錯誤,通過getlasterror會得到錯誤碼0x00000715 ,通過error lookup 可以發現是 「找不到映像檔案中指定的型別」,這是因為此時defaultresource是load此dll的.exe中的resource,需要設定為dll中的resource方可訪問。

具體為:

hmodule ghmodule = getmodulehandle(l"test.dll");

hrsrc hr = ::findresource(ghmodule,l"idr_data",l"myrestype");

if (null == hr)

ulong nressize = ::sizeofresource(ghmodule,hr);

hglobal hg= ::loadresource(ghmodule, hr);

if (null == hg || nressize <= 0)

lpbyte pdata = (lpbyte)lockresource(hg);

MFC中FindResource如何獲取自定義資源

在.exe中可以這樣新增和訪問 1,新增資料 資源 在resource 資源 中 通過 add resource import.選擇需要新增的 資料 資源,其中的 resource type 可以自己命名,需要注意的是需要利用字串命名,譬如可以為 myrestype 資源id可以為字串,譬如為 id...

android TextView 如何動態獲取寬度

1.當textview的屬性是wrap content時,在介面還沒顯示時,是無法獲取其顯示後的長和寬的,那麼怎麼獲取呢?可以通過測量的方法預先測量出來。很簡單。textview tv findviewbyid r.id.tv name tv name settext str int spec vi...

python程式設計星期幾 python如何獲取星期幾

以2019年9月19日為例 import time import datetime if name main today int time.strftime w anyday datetime.datetime 2019,9,19 strftime w print anyday 執行效果圖如下 下面...