首页 > 代码库 > exe中嵌入dll和exe中释放dll

exe中嵌入dll和exe中释放dll

BinRes.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef _waxie_binary_resource_h_
#define _waxie_binary_resource_h_
 
#include <string>
 
class BinRes 
{
public:
    BinRes();
    virtual ~BinRes();
 
public:
    static void ExtractBinResource( std::string strCustomResName, int nResourceId, std::string strOutputName);
 
private:
    static std::string getAppLocation();
 
};
 
#endif

  BinRes.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "stdafx.h"
#include "testHarness.h"
#include "BinRes.h"
#include <string>
#include <fstream>
#include <iostream>
 
 
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
 
 
BinRes::BinRes()
{
 
}
 
BinRes::~BinRes()
{
 
}
 
void BinRes::ExtractBinResource( std::string strCustomResName, int nResourceId, std::string strOutputName )
{
    HGLOBAL hResourceLoaded;        // handle to loaded resource
    HRSRC hRes;                     // handle/ptr. to res. info.
    char *lpResLock;                // pointer to resource data
    DWORD dwSizeRes;
    std::string strOutputLocation;
    std::string strAppLocation;
 
    // lets get the app location
    strAppLocation = getAppLocation();
    strOutputLocation = strAppLocation += "\\";
    strOutputLocation += strOutputName;
     
    // find location of the resource and get handle to it
    hRes = FindResource( NULL, MAKEINTRESOURCE(nResourceId), strCustomResName.c_str() );
     
    // loads the specified resource into global memory.
    hResourceLoaded = LoadResource( NULL, hRes );
 
    // get a pointer to the loaded resource!
    lpResLock = (char*)LockResource( hResourceLoaded );
 
    // determine the size of the resource, so we know how much to write out to file! 
    dwSizeRes = SizeofResource( NULL, hRes );
 
    std::ofstream outputFile(strOutputLocation.c_str(), std::ios::binary);
 
    outputFile.write((const char*)lpResLock, dwSizeRes);
    outputFile.close();
}
 
 
// retrieves the full path and file name for our executable file
std::string BinRes::getAppLocation()
{
    TCHAR szPathName[128];
    std::string strPath;
 
    GetModuleFileName(NULL, szPathName, 128);
 
    strPath = szPathName;
     
    int slashPos = strPath.rfind(‘\\‘);
 
    if(slashPos == strPath.npos)
        throw "Unable to get exe location";
 
    strPath = strPath.substr(0, slashPos);
     
    return strPath;
}

  

关键操作步骤:

在资源视图-》右键import-》文件类型选择所有类型-》找到对应的dll文件,编译,dll就在exe中了。

在保存的时候,提示资源类型,就写个BIN就可以(其实随意写就好),但是这个名称在释放的时候有用啊。

释放dll的方法是:

BinRes::ExtractBinResource( "BIN", 132, "debugViewer123.exe" )

注释:

1> "BIN"          就是import保存的 时候写的资源类型。

2> 132          是资源ID。

3> "debugViewer123.exe"  是导出的dll文件名称。