How to generate exe file with some settings from my application - windows
Basically, I just wan't to know how can this be achieved.
For example, suppose that I have to exe files, app1.exe and app2.exe. Now, app2.exe does a specific job basing on some settings defined on it's variables. I wan't to know how can I code the app1.exe to generate app2.exe files while defining different settings (variables) for it, without using any config file, registry or similar.
I don't have a specific project with this problem, but I was just wondering how this can be done.
--Inspired by the famous Trojan Horse ProRat. It does the same thing, it generates server.exe file with predefined settings from its server creator (another exe file). Furthermore it can bound with other files such as images, audio, video etc.
After this period of time, I found a solution to this problem by using code injection on file.
Below is my solution on steps:
Since both exe files (app1.exe and app2.exe) are created by same person, you can create the app2.exe with some predefined variable values (like string setting1 = "${MYVAR}"), then compile and save the exe (app2.exe).
Include app2.exe as an embeded resource on app1.exe (the app2.exe generator), and get input (usually from user) that will be used to replace setting1 variables value of app2.exe
Read byte array of app2.exe on app1.exe (since it is embedded on it's resources) and convert it to HEX string
From the string (that is converted from byte array) find value of setting1 variable (by firstly converting it also in byte array then in HEX string), and then replace it with the input you got in step 2 (by also converting in byte array then in HEX string).
Convert the whole string (after replacing the values) back to byte array and save it as a file (app2.exe).
If someone want to see an example, I can put some code as proof of concept.
Related
For the use of the LocalizedResourceName property
I wish to customize my own folder style, I tried to make the folder get remarks by modifying the LocalizedResourceName property in desktop.ini. I try to set LocalizedResourceName to a Chinese string. But it is displayed as garbled characters when it is actually displayed. I noticed the following code in the desktop.ini of the system folder: LocalizedResourceName=#%SystemRoot%\system32\shell32.dll,-21798 So I try to write a .dll file by myself, encapsulate the icon and string, and use it. I already know how to make a resource-only dll file, but I don't know how to get a certain resource in the file. (ie, get the number -21798 in the above example code) How should I do ?
By convention, a positive resource number is an index (0 is the first resource etc.) and negative numbers are resource ids. In this specific case, it is the string resource with the id of abs(-21798) that Windows would pass to LoadString. If you want to create your own .dll, add a string with an id of 2 for example (any number between 2 and 0xffff) and in your .ini you would use #c:\path\mydll.dll,-2. Before you go to all this trouble, just try saving the .ini as UTF-16 LE (Unicode in Notepad) and use Chinese strings directly without the #.
What is the internal format of the binary registry keys under LastVisitedPidlMRU and LastVisitedPidlMRULegacy?
The first part is the app exe name in null-terminated Unicode, which is easy enough. Past that, each level of the path is included, first in non-Unicode, then Unicode (except for the path root/drive, which is just non-Unicode), but separated by unknown data structures. My underlying problem is that the .Net Reportviewer control's UI export button calls the common dialog, but the control doesn't expose any way of setting the initial folder. I'd like to reliably read/write these keys to manage default open/save behavior. Example registry key dump (app is notepad.exe, path is "C:\Stuff\DLTemp\HattoriHanzo"): "10"=hex:6e,00,6f,00,74,00,65,00,70,00,61,00,64,00,2e,00,65,00,78,00,65,00,00,\ 00,14,00,1f,50,e0,4f,d0,20,ea,3a,69,10,a2,d8,08,00,2b,30,30,9d,19,00,2f,43,\ 3a,5c,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,4c,00,31,00,\ 00,00,00,00,87,48,c0,8a,10,00,53,74,75,66,66,00,38,00,08,00,04,00,ef,be,7b,\ 44,5a,69,87,48,c0,8a,2a,00,00,00,1f,d8,01,00,00,00,ca,04,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,53,00,74,00,75,00,66,00,66,00,00,00,14,00,50,00,31,\ 00,00,00,00,00,87,48,b1,95,10,00,44,4c,54,65,6d,70,00,00,3a,00,08,00,04,00,\ ef,be,1c,45,60,7e,87,48,b1,95,2a,00,00,00,61,5b,02,00,00,00,62,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,44,00,4c,00,54,00,65,00,6d,00,70,00,00,00,\ 16,00,5e,00,31,00,00,00,00,00,87,48,ca,9a,10,00,48,41,54,54,4f,52,7e,31,00,\ 00,46,00,08,00,04,00,ef,be,87,48,ab,90,87,48,ca,9a,2a,00,00,00,e3,e3,04,00,\ 00,00,0e,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,48,00,61,00,74,00,74,\ 00,6f,00,72,00,69,00,48,00,61,00,6e,00,7a,00,6f,00,00,00,18,00,00,00
You can decode the remaining bytes using the SHGetPathFromIDListW Windows API call. For example, see the C# implementation of it at https://svcperf.codeplex.com/SourceControl/latest#src/Viewer/UIUtils/MruFileHelper.cs , but that class decodes OpenSavePidlMRU, which doesn't contain the executable name. You can do the same with the remaining bytes that you get from LastVisitedPidlMRU.
Rules for file extensions?
Are there any rules for file extensions? For example, I wrote some code which reads and writes a byte pattern that is only understood by that specific programm. I'm assuming my anti virus programm won't be too happy if I give it the name "pleasetrustme.exe"... Is it gerally allowed to use those extensions? And what about the lesser known ones, like ".arw"?
You can use any file extension you want (or none at all). Using standard extensions that reflect the actual type of the file just makes things more convenient. On Windows, file extensions control stuff like how the files are displayed in Windows Explorer and what happens when you double click on it.
I wrote some code which reads and writes a byte pattern that is only understood by that specific programm. A file extension is only an indication of what type of data will be inside, never a guarantee that certain data formatted in a specific way will be inside the file. For your own specific data structure it is of course always best to choose an extension that is not already in use for other file formats (or use a general extension like .dat or .bin maybe). This also has the advantage of being able to use an own icon without it being overwritten by other software using the same extension - or the other way around. But maybe even more important when creating a custom (binary?) file format, is to provide a magic number as the first bytes of that file, maybe followed by a file header structure containing a version number etc. That way your own software can first check the header data to make sure it's the right type and version (for example: anyone could rename any file type to your extension, so your program needs to have a way to do some checks inside the file before reading the remaining data).
Include pictures while converting ps1 to exe with PowerGUI
I use the PowerGUI editor to convert a ps1 file to an exe file. Reason is that I dont want people to see my source code. The script includes a own little GUI with a picture on it. My problem is that after converting the script to an exe file the picture will only be shown when it exists on a specific path. If I delete or move the picture from that path it wont be shown when starting the exe. How can I include the picture to the exe? I want to have only one file in the end ...
One way you could do this is by converting your image into a Base 64 String, using the following: [convert]::ToBase64String((get-content C:\YourPicture.jpg -encoding byte)) > C:\YourString.txt With the string that is produced in the text file "C:\YourString.txt" you can copy and paste it into your code and load it into a picture object on the form like so: $logo.Image = [System.Convert]::FromBase64String(' iVBORw0KGgoAAAANSUhEUgAAAfQAAACMCAYAAACK0FuSAAAABGdBTUEAALGPC/xhBQAAAAlwSFlz AAAXEQAAFxEByibzPwAAAAd0SU1FB98DFA8VLc5RQx4AANRpSURBVHhe7J0FmBTX0oZ7F1jc3d3d nU+dOtXw4MGDZfft25fmyJEjPu+//76Xqzke8pCHPOQhD3lIQGxxcGSapvHdd9+FF3hHEdjGFHgn ....... Many more lines of string ....... OqelFQzDMAzD/CZoztADbUwhUm0JERoXCNfEQYhyPAQryiBIUQSBiiTwl1sb3skwDMMwzG+KWLVe jTEBH6U7JvJ0CJQXoaGPQMWBm5W+Va27k14MwzDMfwCA/wfUstOLO+nBIAAAAABJRU5Jggg==') Do this will mean that your image is stored within the code already and doesn't need to be loaded from somewhere. Note: Make sure that the pictures size on disk is the smallest you can get it as producing the string can take sometime and could turn out thousands of lines long. So I would recommend that you only use a pic that is less that 75 Kilobytes in size. You could do it with larger one but this will take a long time to process.
Replacing recorded values with Data file - Data driven testing
I have recorded a simple Coded UI test from visual Studio 2013. What it does is; Launches a website Fills in a form with (8 fields) Saves the form and closes it Now, I wish to use a data.csv file to replace those 8 values. All the searching I did, I could only find options where every input field had different method hence was easy to find and replace values. In my case, 1 method and 8 fields. How do I do that? Where do I make the changes since my main file looks like this: Where and what changes should I make to use CSV file instead of manual values that I recorded. My designer file code is as below for couple of input fields: // Type '123456789' in 'i' text box uIIEdit.Text = this.createKundeParams.UIIEditText; // Type '{Tab}' in 'i' text box Keyboard.SendKeys(uIIEdit, this.createKundeParams.UIIEditSendKeys, ModifierKeys.None); // Type 'Jeff Hardy' in 'name_i' text box uIName_iEdit.Text = this.createKundeParams.UIName_iEditText; // Type '{Tab}' in 'name_i' text box Keyboard.SendKeys(uIName_iEdit, this.createKundeParams.UIName_iEditSendKeys, ModifierKeys.None);
you can put all the 8 values in 1 csv row and treat them as one input to be passed to the method that sets the fields, alternatively you can change the method to accept one value and field name and sets the field with the value
Create the CSV file. Add the CSV file to the project. Make sure the CSV file is deployed. Add the CSV file as a data source for an individual test. Read the CSV fields and use them in the test. The detailed steps, with some variations, are explained below. Visual Studio 2010 has a "data source wizard" that does some of these steps. Visual Studio versions 2012 and 2013 do not have the wizard and so all the steps have to be done manually. Create the CSV file One way is to create the file in a spreadsheet then save it as Comma Separated Values. Another way is to use a text editor and just write the file. I use a spreadsheet program for big data source files and a text editor for creating small files. Some editors add a byte order mark (BOM) at the start of a file, that will be added to the first field name of the CSV which appears to make the field unreadable. See this page for more about the BOM. Add the CSV file to the project Use the context menu in solution explorer, select Add -> Existing Item. Then browse to the required file. Note the file filter will probably need to be altered to be . or *.csv. Make sure the CSV file is deployed Open the properties panel for the CSV file from solution explorer. Set "Copy to output directory" to "Copy if newer" or to "Copy always". Some documents recommend "Copy if newer" but I prefer "Copy always" as occasionally a file was not copied as I expected. The difference between the two copy methods is a little disk space and a little time, but disks are normally big and the time to copy is normally small. Any savings are, in my opinion, far outweighed by being sure that the file will be copied correctly. Add the CSV file as a data source for an individual test Replace the [TestMethod] attribute with the correct data source line. This Microsoft blog shows the replacement code for several possible data source file types. For CSV use: [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod] Note that the file name occurs three times and one copy has a # rather than a .. I have not found any useful documentation about the different fields of the Datasource(...) attribute so cannot advise further on how to choose values for non-CSV data sources. The |DataDirectory| part above is replaced by the directory where files are deployed when the tests run. The whole file name within the string quotes could be replaced by a full path name of a file, if required. Read the CSV fields and use them in the test The Coded UI record and generate tool creates classes with fields that hold values entered into text boxes or used in assertions. Each action method has a ...Params class and each assert method has an ...ExpectedValues class, where the ... is the method name. The default values of these fields are the values used when the test was recorded. The recorded values can be overwritten by an assignment before the action or assertion method is called. The fields of the current row of the data source are accessed from TestContext.DataRow[...]. Suppose a Coded UI test has an EnterValue method that writes text into two fields of the screen and it also has a CheckResult method that asserts one field. The test method might then be written as follows. [DataSource... TestMethod] public void CodedUITestMethod1() { this.UIMap.EnterValueParams.UIItem0TextSendKeys = TestContext.DataRow["ValueOne"].ToString(); this.UIMap.EnterValueParams.UIItem1TextSendKeys = TestContext.DataRow["ValueTwo"].ToString(); this.UIMap.EnterValue(); this.UIMap.CheckResultExpectedValues.UIItem0TextDisplayText = TestContext.DataRow["Result"].ToString(); this.UIMap.CheckResult(); } The ...Params and ...ExpectedValues classes allow the test to create values when the test runs. For example, if the EnterValue method also wanted to write tomorrow's date into a field we could add the following line before it is called: