首页
社区
课程
招聘
[求助]关于UpdateResource的问题
发表于: 2010-8-20 10:05 6496

[求助]关于UpdateResource的问题

2010-8-20 10:05
6496
关于修改程序中资源:
源代码如下,就是得不到正确的字符,全是乱码
我先新建一个基于MFC的控制台程序中,1111111.cpp代码如下:
// 11111111.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "11111111.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

#define IDR_RT_STRING1 1000
#define IDR_RT_STRING2 1001
char *pUrl1;
char *pUrl2;

BOOL GetConfig()
{
        HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_RT_STRING1), RT_STRING);
        if(!hRsrc)
        {
                return FALSE;
        }
        pUrl1 = (char *)(LoadResource(NULL, hRsrc));
       
        hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_RT_STRING2), RT_STRING);
        if(!hRsrc)
        {
                return FALSE;
        }
        pUrl2 = (char *)(LoadResource(NULL, hRsrc));
        return TRUE;
}

int main(int argc,char* argv[])
{

        MessageBox(NULL,pUrl1,"网址:",MB_OK);
        MessageBox(NULL,pUrl2,"备用网址:",MB_OK);
}
上面的程序的资源是空的,我再写一个程序去向这个程序添加字符串资源,然后用这个程序打印出那两个字符串,但是却得到是空字符串,用资源查看工具可以看到程序确实添加了两个字符串资源
添加程序如下:
记得要把这个程序拷贝到添加程序的目录下!
WriteDlg.cpp源代码如下(是一个MFC的程序):
// WriteDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Write.h"
#include "WriteDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define IDR_RT_STRING1 1000
#define IDR_RT_STRING2 1001
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
        CAboutDlg();

// Dialog Data
        //{{AFX_DATA(CAboutDlg)
        enum { IDD = IDD_ABOUTBOX };
        //}}AFX_DATA

        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CAboutDlg)
        protected:
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
        //}}AFX_VIRTUAL

// Implementation
protected:
        //{{AFX_MSG(CAboutDlg)
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
        //{{AFX_DATA_INIT(CAboutDlg)
        //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CAboutDlg)
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
        //{{AFX_MSG_MAP(CAboutDlg)
                // No message handlers
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWriteDlg dialog

CWriteDlg::CWriteDlg(CWnd* pParent /*=NULL*/)
        : CDialog(CWriteDlg::IDD, pParent)
{
        //{{AFX_DATA_INIT(CWriteDlg)
                // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT
        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWriteDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CWriteDlg)
                // NOTE: the ClassWizard will add DDX and DDV calls here
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWriteDlg, CDialog)
        //{{AFX_MSG_MAP(CWriteDlg)
        ON_WM_SYSCOMMAND()
        ON_WM_PAINT()
        ON_WM_QUERYDRAGICON()
        ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
        ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWriteDlg message handlers

BOOL CWriteDlg::OnInitDialog()
{
        CDialog::OnInitDialog();

        // Add "About..." menu item to system menu.

        // IDM_ABOUTBOX must be in the system command range.
        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
        ASSERT(IDM_ABOUTBOX < 0xF000);

        CMenu* pSysMenu = GetSystemMenu(FALSE);
        if (pSysMenu != NULL)
        {
                CString strAboutMenu;
                strAboutMenu.LoadString(IDS_ABOUTBOX);
                if (!strAboutMenu.IsEmpty())
                {
                        pSysMenu->AppendMenu(MF_SEPARATOR);
                        pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
                }
        }

        // Set the icon for this dialog.  The framework does this automatically
        //  when the application's main window is not a dialog
        SetIcon(m_hIcon, TRUE);                        // Set big icon
        SetIcon(m_hIcon, FALSE);                // Set small icon
       
        // TODO: Add extra initialization here
       
        return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWriteDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
        if ((nID & 0xFFF0) == IDM_ABOUTBOX)
        {
                CAboutDlg dlgAbout;
                dlgAbout.DoModal();
        }
        else
        {
                CDialog::OnSysCommand(nID, lParam);
        }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWriteDlg::OnPaint()
{
        if (IsIconic())
        {
                CPaintDC dc(this); // device context for painting

                SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

                // Center icon in client rectangle
                int cxIcon = GetSystemMetrics(SM_CXICON);
                int cyIcon = GetSystemMetrics(SM_CYICON);
                CRect rect;
                GetClientRect(&rect);
                int x = (rect.Width() - cxIcon + 1) / 2;
                int y = (rect.Height() - cyIcon + 1) / 2;

                // Draw the icon
                dc.DrawIcon(x, y, m_hIcon);
        }
        else
        {
                CDialog::OnPaint();
        }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWriteDlg::OnQueryDragIcon()
{
        return (HCURSOR) m_hIcon;
}

void CWriteDlg::OnButton1()
{
        // TODO: Add your control notification handler code here
        char szFileName[MAX_PATH]="11111111.exe";
        char NewFileName[MAX_PATH]="\\hehe.exe";
        char WinDir[MAX_PATH];
        char* szUrl1="http://192.168.0.19/temp5.txt";
        char* szUrl2="http://192.168.0.18/temp5.txt";
        GetWindowsDirectory(WinDir,MAX_PATH);
        strcat(WinDir,NewFileName);
        CopyFile(szFileName,WinDir,FALSE);
        HANDLE hUpdateRes=BeginUpdateResource(WinDir,FALSE);
        if(hUpdateRes == NULL)
        {
                return ;
        }
        if(!UpdateResource((HMODULE)hUpdateRes,RT_STRING,MAKEINTRESOURCE(IDR_RT_STRING1),1033,szUrl1,strlen(szUrl1)))
        {
                return ;
        }
        if(!UpdateResource((HMODULE)hUpdateRes,RT_STRING,MAKEINTRESOURCE(IDR_RT_STRING2),1033,szUrl2,strlen(szUrl2)))
        {
                return ;
        }
        if(!EndUpdateResource(hUpdateRes, FALSE))
        {
                return ;
        }
        MessageBox("资源成功载入!");
}

void CWriteDlg::OnButton2()
{
        // TODO: Add your control notification handler code here
        DestroyWindow();
}
我是先将那个程序拷贝到另外一个地方,然后在进行的操作,以防破坏程序!
但就是得不到我想要的那两个字符串,不知道为什么?用资源工具去查看的时候,显示为乱码,希望有以前遇到同样问题的人能给个解答,谢谢!

[课程]Linux pwn 探索篇!

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 369
活跃值: (233)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
LZ请参考一下这个吧!

VC++中修改PE文件字符串的方法
2010-8-20 10:17
0
雪    币: 2323
活跃值: (4113)
能力值: ( LV12,RANK:530 )
在线值:
发帖
回帖
粉丝
3
这效率也太高了,这么快就有人回答,非常感谢!!
2010-8-20 10:31
0
雪    币: 2323
活跃值: (4113)
能力值: ( LV12,RANK:530 )
在线值:
发帖
回帖
粉丝
4
刚才看了一下楼上兄弟说的代码,是通过文件定位,然后写入字符串,我想问一下,如果用我上面的那种方法,直接UpdateResource能实现吗?为什么不显示呢?谢谢!但是我用这种方法,加载DLL到程序中是可以的,就是字符串,不能正确显示?
2010-8-20 10:52
0
游客
登录 | 注册 方可回帖
返回
//