0

There are lots of answer about simply save it as const data or say converting it to binary data without details.

I have seen this: How to embed a file into an executable?

I do not prefer converting config file into const data.

How could I embed the config file inside the exe when compiled but not next to the exe.

Detailed steps are needed.

Thank You.

Community
  • 1
  • 1
Pika
  • 507
  • 1
  • 6
  • 16
  • config file? key->value, why don't you just hard code those value instead of embed the config file? – Danh Nov 04 '16 at 03:48
  • As I stated, I do not prefer hard-code this solution. – Pika Nov 04 '16 at 03:51
  • for hard code I meant defining some MACRO for those values, I don't see any benefit of embedding config file over using MACRO – Danh Nov 04 '16 at 03:53
  • If you're okay with a Windows-specific solution, maybe you could embed the config file as a RT_RCDATA resource? – nomadictype Nov 04 '16 at 05:44
  • @nomadictype I am totally okay with Windows-specific solution. It would be greatly appreciated if you could provide more details. – Pika Nov 04 '16 at 07:00
  • @AnsonWong I've added an answer with details. – nomadictype Nov 04 '16 at 07:26
  • Implicit in a config file is that there is a need to change it. The worst possible place you can think of for data that needs to change is the EXE file. An EXE file is locked for write access while the EXE is running, a side-effect of the memory-mapped view that maps the EXE content into RAM. So you'd have to terminate the process and have *another* program that modifies the EXE. Beyond giving anti-malware's a major wedgie, that completely does not solve the desire to deploy only one file. – Hans Passant Nov 04 '16 at 08:43

3 Answers3

1

You have to manually add the settings because visual studio doesn't know about handling config files automatically. If we do not do it manually, the config files won't be copied at the output directory.

Go to project properties -> configuration properties -> build events -> post build event and change the command line with:

copy app.config"$(TargetPath).config"

After that you have to add a new reference from system.configuration. Find that in project properties -> common properties -> references -> add new reference -> .net -> system.configuraion

Then create your config file from project -> new -> configuration file

wr93_
  • 130
  • 1
  • 1
  • 13
  • Thank you for your answer. It do help me a bit. However, I want the exe itself containing that config instead of having the config next to it – Pika Nov 04 '16 at 08:23
  • @AnsonWong You simply can't compile a .exe file with .config altogether. The sole purpose of having a config file is to do manual configurations to the application (.exe) externally. You comment has a duplicate here: – wr93_ Nov 04 '16 at 08:31
  • @AnsonWong Check this: http://stackoverflow.com/questions/4586210/how-do-i-compile-my-app-config-into-my-exe-in-a-vs2010-c-sharp-console-app – wr93_ Nov 04 '16 at 08:31
1

A Windows-specific method is to embed the config file into the executable as a resource:

  • Define an identifier for the resource in some header file, say resource.h:

    #define CONFIG_FILE_RESOURCE_ID 1
    
  • Add a .rc file to your project with the following lines in it:

    #include "resource.h"
    CONFIG_FILE_RESOURCE_ID RCDATA "config.txt"
    

    where config.txt is the name of the file that will be embedded. RCDATA is the type of resource (custom binary data).

    Alternatively, if your version of Visual Studio comes with a resource editor, you could use it.

  • To read the resource from within your program, you must:

    1. Call FindResource(NULL, MAKEINTRESOURCE(CONFIG_FILE_RESOURCE_ID), RT_RCDATA) to get a resource handle.
    2. Call LoadResource(NULL, hrsrc) to convert the resource handle hrsrc to a HGLOBAL.
    3. Use the LockResource and SizeofResource functions to obtain a pointer to the data and the size of the embedded file.

    You may have to supply actual module/instance handles instead of NULL, especially if your code is inside a DLL.

nomadictype
  • 700
  • 4
  • 11
  • I have tested that the config really could embed into EXE. However, I found some difficulties in reading the data from config. Can you provide an example of how to read that embedded data? Thank you. – Pika Nov 04 '16 at 08:21
  • What exactly are you having difficulties with? Did you manage to get the FindResource -> LoadResource -> LockResource -> SizeofResource dance working? – nomadictype Nov 04 '16 at 08:35
  • "To read the resource from within your program, you must:..." – Pika Nov 04 '16 at 08:36
  • There's some example code in [this question](http://stackoverflow.com/questions/1134050/is-there-a-way-to-preserve-the-bitmapfileheader-when-loading-a-bitmap-as-a-windo), just replace RT_BITMAP with RT_RCDATA. If it doesn't work, use a debugger or print out intermediate values to see which step fails. – nomadictype Nov 04 '16 at 08:37
  • Finally, the problem is solved with help of [this question](http://stackoverflow.com/questions/9240188/how-to-load-a-custom-binary-resource-in-a-vc-static-library-as-part-of-a-dll). Thank You. – Pika Nov 04 '16 at 09:46
0

I understood that you want to create self modifying exe file that will store config. As a simple answer you can't achieve that without hacking page protection.

Here and here you can find some useful information.

Community
  • 1
  • 1
nopasara
  • 538
  • 3
  • 10