zoukankan      html  css  js  c++  java
  • DlgResToDlgTemplate 的代码,提取EXE中的资源,然后转化成C的字符串数组

    代码来源:https://www.codeproject.com/Articles/13330/Using-Dialog-Templates-to-create-an-InputBox-in-C

    #include <windows.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      printf("DlgResToDlgTemplate v1.0 (c) <lallousx86@yahoo.com>
    
    ");
    
      if (argc < 4)
      {
        printf("usage: %s MODULE_FILE DLG_ID FILE.H
    ", argv[0]);
        return -1;
      }
    
      int dlgID = atoi(argv[2]);
    
      HMODULE hModule;
      hModule = ::LoadLibraryEx(argv[1], 0, LOAD_LIBRARY_AS_DATAFILE);
      if (hModule == NULL)
      {
        printf("Could not load module '%s'
    ", argv[1]);
        return -1;
      }
    
      HRSRC hrsrc = ::FindResource(hModule, (LPCTSTR) dlgID, RT_DIALOG);
      if (hrsrc == NULL)
      {
        printf("Cannot find dialog resource with id '%d'
    ", dlgID);
        return -2;
      }
    
      HGLOBAL hglobalRsrc = ::LoadResource(hModule, hrsrc);
      if (hglobalRsrc == NULL)
      {
        printf("Cannot load resource!
    ");
        return -3;
      }
    
      char *buf = (char *)::LockResource(hglobalRsrc);
      if (buf == NULL)
      {
        printf("Cannot lock resource!
    ");
        return -4;
      }
    
      DWORD sz = ::SizeofResource(hModule, hrsrc);
    
      FILE *fp = fopen(argv[3], "wb");
      if (fp == NULL)
      {
        printf("Could not open '%s' for writing
    ", argv[3]);
        return -5;
      }
    
      
      char temp[100];
    
      sprintf(temp, "static unsigned char dlg_%d[] = 
    {
    	", dlgID);
      fwrite(temp, 1, strlen(temp), fp);
    
      sz--;
      for (DWORD i=0;i<=sz;i++)
      {
        sprintf(temp, "0x%02x", (unsigned char)(buf[i] & 0xFF));
        fwrite(temp, 1, strlen(temp), fp);
    
        // don't append a comma if it was last byte
        if (i != sz)
        {
          fwrite(", ", 1, 1, fp);
        }
    
        if (i && (i % 16 == 0))
          fwrite("
    	", 1, 2, fp);
      }
    
      strcpy(temp, "
    };
    ");
      fwrite(temp, 1, strlen(temp), fp);
    
      fclose(fp);
      ::FreeLibrary(hModule);
    
      printf("generated %s (%d bytes) successfully!
    ", argv[3], sz);
    
      return 0;
    }
  • 相关阅读:
    react篇章-React State(状态)
    react篇章-React 组件-复合组件
    react篇章-React 组件-向组件传递参数
    react篇章-React 组件-ES6 class 来定义一个组件
    React篇章-React 组件
    复习常用算法_冒泡算法
    项目管理小结(如何做好一个百万级项目甚至千万级别的项目)
    AOP切点切面内容
    Spring 框架
    Spring MVC 框架
  • 原文地址:https://www.cnblogs.com/eaglexmw/p/11404328.html
Copyright © 2011-2022 走看看