⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmemory.cpp

📁 bt848,bt878 a采集卡的探测
💻 CPP
字号:
#include "CMemory.hpp"
#include "CFile.hpp"
#include "CString.hpp"
#include <assert.h>

CMemory::CMemoryAtom::CMemoryAtom(const UINT thelen) 
	: len(thelen) , refcount(1) 
{ 
	block = (void*) malloc( (thelen) ? thelen : 1 ); 
}

// The memory atom resize fn
bool CMemory::CMemoryAtom::ReSize(const UINT thelen) 
{
	void* newblock = realloc(block, (thelen) ? thelen : 1);
	if (newblock) {
		block = newblock;
		len = thelen;
		return true;
	}
	return false;
}

bool CMemory::CMemoryAtom::Release()
{ 
	refcount--; 
	return (!refcount);
}

CMemory::CMemoryAtom::~CMemoryAtom()
{ 
	assert( refcount == 0);
	if (block) free(block); 
}

CMemory& CMemory::operator=(CMemory& blk) {
	CMemoryAtom* old = atom;
	atom = blk.atom;
	atom->AddRef();
	if (old->Release()) {
		delete old;
	}
	return *this;
}

CMemory::~CMemory()
{ 
	if (atom->Release()) {
		delete atom; 
	}
}

CMemory LoadBinaryResource(HINSTANCE instance,LPCTSTR res,LPCTSTR lpType)
{
	CMemory ret;

	// Find resource
	HRSRC HEXE = FindResource(instance,res,lpType);
	if (HEXE) { // We got it!
		// Load the resource
		HGLOBAL HLEXE = LoadResource(instance,HEXE);
		if (HLEXE) {
			// Lock the resource to be able to access it!
			PVOID Ptr = (PVOID) LockResource(HLEXE);
			if (Ptr) {
				UINT Len = SizeofResource(instance,HEXE);
				// Copy it to our buffer
				ret.ReSize(Len);
				memcpy(ret.Ptr(),Ptr,Len);
				UnlockResource(HLEXE);
			}
			FreeResource(HLEXE);
		}
	} 
	return ret;
}

CMemory LoadBinaryFile(CString& File)
{
	return LoadBinaryFile(File.Ptr());
}

CMemory LoadBinaryFile(LPTSTR File)
{
	CMemory ret;

	// Open file
	CFile ab(File,CFile::Normal,OPEN_EXISTING);

	// Exit on failure
	if (!ab.IsOpen()) {
		return ret;
	}

	// Get File size
	DWORD FSize = ab.Size();

	// Get a pointer to a memory block that will hold all the file. We treat it as Bytes
	if (!ret.ReSize(FSize)) {
		return ret;
	}

	// Read all file into the block of memory
	DWORD BRead = 0;
	ab.Read(ret.Ptr(),FSize,BRead);
	ret.ReSize(BRead);

	// Return memory block
	return ret;
}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -