直接执行二进制资源 [英] directly execute binary resource

查看:127
本文介绍了直接执行二进制资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lpBuffer是一个指针(二进制)资源的第一个字节。我该如何执行它马上没有它倾倒到一个临时文件?

  HMODULE hLibrary;
HRSRC hResource;
HGLOBAL hResourceLoaded;
LPBYTE lpBuffer;hLibrary =调用LoadLibrary(C:\\\\ xyz.exe);
如果(NULL!= hLibrary)
{
    hResource = FindResource(hLibrary,MAKEINTRESOURCE(104),RT_RCDATA);
    如果(NULL!= hResource)
    {
        hResourceLoaded = LoadResource(hLibrary,hResource);
        如果(NULL!= hResourceLoaded)
        {
            lpBuffer =(LPBYTE)LockResource(hResourceLoaded);
            如果(NULL!= lpBuffer)
            {
                //做一些与lpBuffer这里
            }
        }
    }
    FreeLibrary则(hLibrary);
}


解决方案

有没有内置于Windows此功能;你唯一的选择就是的CreateProcess ,这需要一个EXE文件。

这是可能的解析自己的可执行文件格式。你会有效地重新创建调用LoadLibrary 函数做什么。

下面是一个如何在其中加载DLL和通话功能的解释:的 http://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory/ 。要为您的EXE适应这一点,你会遵循同样的搬迁和导入步骤。一旦你做你会调用EXE的入口点。 (本教程介绍了如何调用DLL的导出函数)。

根据什么的EXE你可能有直接载入它的问题在现有的过程。例如,你自己的EXE进行各种的Win32和C初始化code和嵌入的EXE可能尝试再次执行相同的初始化。如果这成为一个问题,你的选择是把EXE嵌入在自己的进程;那么,你回来了创建一个临时文件并调用的CreateProcess

lpBuffer is a pointer to the first byte of a (binary)resource. How can I execute it straight away without dumping it to a temporary file?

HMODULE hLibrary;
HRSRC hResource;
HGLOBAL hResourceLoaded;
LPBYTE lpBuffer;

hLibrary = LoadLibrary("C:\\xyz.exe");
if (NULL != hLibrary)
{
    hResource = FindResource(hLibrary, MAKEINTRESOURCE(104), RT_RCDATA);
    if (NULL != hResource)
    {
        hResourceLoaded = LoadResource(hLibrary, hResource);
        if (NULL != hResourceLoaded)        
        {
            lpBuffer = (LPBYTE) LockResource(hResourceLoaded);            
            if (NULL != lpBuffer)            
            {                
                // do something with lpBuffer here            
            }
        }    
    }
    FreeLibrary(hLibrary);
}

解决方案

There isn't a function built into Windows for this; your only option is CreateProcess, which takes an EXE file.

It's possible to parse the executable file format yourself. You'd effectively be recreating what the LoadLibrary function does.

Here's an explanation of how to load a DLL and call functions within it: http://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory/. To adapt this for your EXE, you'd follow the same relocation and import steps. Once you're done you'd call the EXE's entry point. (The tutorial explains how to call a DLL's exported function.)

Depending on what's in the EXE you might have problems loading it directly into an existing process. For instance, your own EXE performs various Win32 and C initialization code, and the embedded EXE is likely to attempt to perform the same initialization again. If this becomes a problem, your alternative is to put the embedded EXE in its own process; then, you're back to creating a temp file and calling CreateProcess.

这篇关于直接执行二进制资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆