從一個程式中產生另一個程式(資源法)

來源:互聯網
上載者:User

檔標題:從一個程式中產生另一個程式(資源法)  作者: 佚名

前不久大家都在討論怎樣通過一個程式來產生另一個程式,我提出了將另一個程式當資源檔包含在程式中一起編譯,然後在程式運行時根據需要再重建出來,下面是我的一個例子, 在這裡,我將windows內建的寫字板(writer.exe)當資源.

下面是原始碼

//資源檔writer.exe長度為204800,在資源檔中定義為"CUSTOM"資源,編號為1111

  HRSRC   hMyRes;     //resource handle
  HGLOBAL   hgpt;       //resource pointer
  LPVOID    lpBuff;     //resource buffer pointer
  DWORD     rcSize=204800;    //resource size
  HANDLE    hFile;//file to write
  LPDWORD  dwByte;//byte size had been write
  dwByte=&rcSize; //locate the resource and load the resource to memory and lock it
  hMyRes=FindResource((HMODULE)GetWindowLong(Handle,GWL_HINSTANCE),MAKEINTRE SOURCE(1111),"CUSTOM");
  if(hMyRes==NULL)
    ShowMessage(SysErrorMessage(GetLastError()));
  hgpt=LoadResource(NULL,hMyRes);
  if(hgpt==NULL)
    ShowMessage(SysErrorMessage(GetLastError()));
  lpBuff=LockResource(hgpt);

//now i will read the resource and write it to an file

  try
    {
    hFile=CreateFile("e:\\mywriter.exe",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    WriteFile(hFile,lpBuff,rcSize,dwByte,NULL);
    if(*dwByte!=204800)
      ShowMessage("Failed to write to file");
    }
   __finally
    {
    CloseHandle(hFile);
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.