改写dll字符串资源

建一个dll文件。
打开vc,
   一  File->New->Win32 Dynamic-link Library
         Project name 输入dlltest ->ok->A simple DLL project->Finish
   二  在dlltest.cpp中添加如下代码 1 (见后)
   三  添加资源,Insert->Resource  属性框中选择String Table->New,双击出现的ID Value Caption
       下方条框,在String Properties中输入 this is a string for test!(这就是要读取的资源字符串)
   四  保存
   五  在Header Files中右键添加resource.h;在Resource Files中右键添加Script1.rc
   六  在dlltest.cpp文件中添加头文件resource.h
  
   至此,读取字符串资源,并把字符串写入一个文件中的dll做好了。
  
修改dll文件的程序:
    编译执行代码 2 (见后)

 

  
  
 代码 1
 /***************************************************************************
 dll文件
    功能:读取资源字符串,写入c:/dlltest.txt
     

 
 ****************************************************************************/ 
 #include "stdafx.h"
 #include "stdio.h"
 #include "resource.h"
 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
 {
      switch(fwdreason)
      {
           case DLL_PROCESS_ATTACH:
                    int i;
                    FILE *fw;
     char filename[200];
                    fw = fopen("c://dlltest.txt", "w");
                    char str[200];
                    LoadString(hinstDLL,1,str,MAX_PATH);  
                    fprintf(fw,"%s/n", str); 
                break;
           default:
                break;
       }
       return(TRUE);
 }
 

 

-------------------------------------------

代码 2
/*****************************************************************************************
 dll文件
    功能:改写资源字符串
        
******************************************************************************************/
 
#include "windows.h"
#include "stdio.h"

void   main()
{

   
 BOOL result;
 int i,len=0;
 char data[100];
 char source[128];          //新的资源字符串
 sprintf(source,"
www.msn.com");
 char *sNewString;
 sNewString = source;

 HINSTANCE hdll; 
 char sss[600];

 hdll = LoadLibraryEx("dlltest.dll",NULL, LOAD_LIBRARY_AS_DATAFILE);  //获得dll文件句柄
 LoadString(hdll, 1, sss, 600 );                    //根据id读取资源字符串
 FreeLibrary(hdll);                          //释放dll
 printf("the old string:/n/t%s/n",sss);
 
  HANDLE hUpdateRes;
 hUpdateRes=BeginUpdateResource("dlltest.dll",false);                 //返回一个可被UpdateResource函数使用的句柄
 if(hUpdateRes==NULL)
 {
  printf("open dll error/n");
  return;
 }

 int iCharCount=strlen(sNewString)+1;
 LPWSTR pUnicodeString =new WCHAR[iCharCount];
 DWORD dwUnicodeCharCount=                                            //变换成宽字节
  MultiByteToWideChar(CP_ACP,NULL,sNewString,-1,pUnicodeString,iCharCount);


 
 HGLOBAL hglob=GlobalAlloc(GHND,(dwUnicodeCharCount+4) * sizeof(WCHAR));  //分配内存
 if(!hglob)
 {
  delete pUnicodeString;
 }
 LPWSTR pBufStart=(LPWSTR)GlobalLock(hglob);                              //锁定内存段
 if(!pBufStart)
 {
  GlobalFree(hglob);
  delete pUnicodeString;
 }
 LPWSTR pBuf=pBufStart;
 pBuf++;
 *(pBuf++)=(WCHAR)dwUnicodeCharCount-1;
 for(i=0;i<(int)dwUnicodeCharCount-1;i++)
  *(pBuf++)=pUnicodeString[i];
 delete pUnicodeString;
 if(++dwUnicodeCharCount%1)
  dwUnicodeCharCount++;
 BOOL bSccess=TRUE;
 

 result=UpdateResource(hUpdateRes,                              //开始更新     
                    RT_STRING,                     
        MAKEINTRESOURCE(1),
        MAKELANGID(LANG_CHINESE,0x2),                  //根据不同文件选用不同的语言选项,美区英文的为: LANG_ENGLISH,SUBLANG_ENGLISH_US                               
        pBufStart,                     
        dwUnicodeCharCount * sizeof(WCHAR));    
 if (result==FALSE)
 {
  printf("error:can not add resource!/r/n");
  return;
  
 }
 GlobalUnlock(hglob);
 GlobalFree(hglob);

 if (!EndUpdateResource(hUpdateRes,FALSE))
 {
  printf("error:can not write changes to this file!/r/n");
  return;
 
 }

 


 hdll = LoadLibraryEx("dlltest.dll",NULL, LOAD_LIBRARY_AS_DATAFILE);  //获得dll文件句柄
 LoadString(hdll, 1, sss, 600 );                    //根据id读取资源字符串
 FreeLibrary(hdll);                          //释放dll
 printf("the new string:/n/t%s/n",sss);

 return ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值