欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

VC集成dll到资源中(也可是其他文件) 博客分类: VC VC

程序员文章站 2024-02-23 15:40:04
...
g_MakeResourceFromExe用于释放资源DLL与SYS中ID为IDR_DLL1的WinIo32.dll
和ID为IDR_SYS1的WinIo32.sys.ReleaseExe用于将文件属性置为只读,隐藏,归档
主要代码如下:
bool CtestDlg::g_MakeResourceFromExe(CString szExPathName, CString szResName, int nResID)
{
	HRSRC hSrc = FindResource(NULL,MAKEINTRESOURCE(nResID),szResName);
	if(hSrc == NULL) return false;
	HGLOBAL hGlobal = LoadResource(NULL,hSrc);
	if(hGlobal == NULL) return false;
	LPVOID lp = LockResource(hGlobal);
	DWORD dwSize = SizeofResource(NULL,hSrc);
	CFile file;
	if(file.Open(szExPathName,CFile::modeCreate|CFile::modeWrite))
	{
		file.Write(lp,dwSize);
		file.Close();
	}
	FreeResource(hGlobal);
	return true;
}

void CtestDlg::ReleaseDLL()
{//从资源DLL中自释放WinIo32.dll自定义资源类型,ID=IDR_DLL1
	CString rc_dll = _T("WinIo32.dll");
	CString rc_sys = _T("WinIo32.sys");
	CFileFind find;
	if(!find.FindFile(rc_dll))
	{
		g_MakeResourceFromExe(rc_dll,_T("DLL"),IDR_DLL1);
		//改变WinIo32.dll的属性:只读,隐藏,归档
		DWORD dwAttributes = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY;
		SetFileAttributes(rc_dll,dwAttributes);  
	}

	if(!find.FindFile(rc_sys))
	{
		g_MakeResourceFromExe(rc_sys,_T("SYS"),IDR_SYS1);
		//改变WinIo32.dll的属性:只读,隐藏,归档
		DWORD dwAttributes = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY;
		SetFileAttributes(rc_sys,dwAttributes);  
	}
	find.Close();
}
代码中再动态加载WinIo32.dll.并且找到dll中的方法进行调用.主要方法如下:
typedef int (_stdcall *InitializeWinIoFun)();
InitializeWinIoFun InitializeWinIo = 0;

typedef int (_stdcall *GetPortValFun)(WORD wPortAddr, PDWORD pdwPortVal, BYTE bSize);
//typedef int (__cdecl *MYPROC)(short int* , short int* , short );  
GetPortValFun GetPortVal = 0;

typedef int (_stdcall *ShutdownWinIoFun)();
ShutdownWinIoFun ShutdownWinIo = 0;

HMODULE hDllInst = ::LoadLibrary(_T("WinIo32.dll"));

if (hDllInst)
	{
		InitializeWinIo = (InitializeWinIoFun)GetProcAddress(hDllInst, "InitializeWinIo");
		GetPortVal = (GetPortValFun)GetProcAddress(hDllInst, "GetPortVal");
		ShutdownWinIo = (ShutdownWinIoFun)GetProcAddress(hDllInst, "ShutdownWinIo");
	}
 
	int   bResult   =   InitializeWinIo(); 

 

相关标签: VC