C + +: Silently block model files into dll__c++ by adding resources

Source: Internet
Author: User

This Bo records for the use of halogen cooking records, if there are omissions, please correct me.

Halogen Boiling: Non-literary Xiao Yan er

This blog address: To add resources to the model file quietly sealed into the DLL


For companies engaged in neural networks and deep learning, model files are one of the core technical documents.

If you give someone the SDK, the model file to the original look directly to others, is undoubtedly the core of their own technology to give people, it is estimated that many companies still do not have the courage.

So either encrypt the model or simply block it into the API's DLL file. The second is certainly more insurance.

Next, I'll explain how to block a file into a DLL.

In short, this is done by adding a resource file.

First, your API project generates a target file that is a DLL.


Project right--add--resource, select your model file, and give the file a corresponding name according to the prompts.


After the addition is complete, a resource.h file is generated, and the macro definition in this file is the index of the name of the file you just added, which will then be used to correlate your files. So do not change the value of yo.


In the CPP where you want to use these file data, add two header files:

#include "resource.h"
#include <Windows.h>


Add two more functions

One is the function that loads the current module

Hmodule getcurrentmodule (BOOL bref = FALSE)
{
	hmodule hmodule = NULL;
	if (Getmodulehandleex) (bref? Get_module_handle_ex_flag_from_address: (get_module_handle_ex_flag_from_address
		| Get_module_handle_ex_flag_unchanged_refcount), (LPCSTR) Getcurrentmodule, &hmodule))
	{return
		hModule;
	} return

	NULL;
}

The other is the function of how to use the data in the resource file

BOOL load_fromresource (const int Source_type, const string data_class, Layer *dst) {hmodule g_hinstance;
	G_hinstance = Getcurrentmodule ();

	HRSRC hres = FindResource (g_hinstance, Makeintresource (Source_type), (LPCTSTR) data_class.c_str ());
		if (hres = = NULL) {cout << "Error:findresource ERROR." << Endl;
	return FALSE;
	} hglobal hresload = LoadResource (g_hinstance, hres);
		if (hresload = = NULL) {cout << "Error:loadresource ERROR." << Endl;
	return FALSE;
	const int cbsize = Sizeofresource (g_hinstance, hres);

	cout << "cbsize:" << cbsize << Endl; void *lpreslock = Lockresource (hresload);//lockresource (hresload) is equivalent to loading your model file into a piece of memory with the first address of memory as Lpreslock if (Lpreslock =
		NULL) {cout << "Error:lockresource ERROR." << Endl;
	return FALSE; }//Decrypt & uncompress/*read Start Here, how to read it, just look at what your data is like. * * Const int * Psource = (const int *) Lpreslock; Convert the pointer to the format of your data, such as int int w = ppsource[0];//This sentence is similar to Fread (W,sizeof (int), 1, file) function.
return TRUE; }


Well, it's time to have a rest this weekend, just write these first, and if there are omissions, follow-up corrections.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.