Save batch file on teensy

I own a Teensy 3.2, and I want to store a batch file on it. I found out, that Teensy has a 2kb EEPROM, where I could store it.

The batch file is generating a .exe file with compiled C# inside.

But now I have two problems:

  1. The batch-file is too big (3.37kb), is there any way to make it smaller?
  2. How am I able to transfer that file on the Teensy (if it has the appropriate size) and then retranfser it to computer again, if I want to? I can only find EEPROM.write() as method, but as I want to transfer a file, and not just single bytes, this doesn't seem to work for me?

Here's the code of the batch:

// 2>nul||@goto :batch
/*
:batch
@echo off
setlocal
set "csc="
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do  set "csc=%%#"
if not exist "%~n0.exe" (
   call %csc% /nologo /r:"Microsoft.VisualBasic.dll" /out:"%~n0.exe" "%~dpsfnx0" || (
      exit /b %errorlevel% 
   )
)
%~n0.exe %*
endlocal & exit /b %errorlevel%
*/
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
public class ScreenCapture
{
    private Image CaptureWindow()
    {
        IntPtr handle = User32.GetDesktopWindow();
        IntPtr hdcSrc = User32.GetWindowDC(handle);
        User32.RECT windowRect = new User32.RECT();
        User32.GetWindowRect(handle, ref windowRect);
        int width = windowRect.right - windowRect.left;
        int height = windowRect.bottom - windowRect.top;
        IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
        IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
        IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
        GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
        GDI32.SelectObject(hdcDest, hOld);
        GDI32.DeleteDC(hdcDest);
        User32.ReleaseDC(handle, hdcSrc);
        Image img = Image.FromHbitmap(hBitmap);
        GDI32.DeleteObject(hBitmap);
        return img;
    }
    public void CaptureScreenToFile(string filename)
    {
        Image img = CaptureWindow();
        img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    public static void Main()
    {
        String[] arguments = Environment.GetCommandLineArgs();
        if (arguments.Length == 1)
            Environment.Exit(0);
        String file = arguments[1];
        ScreenCapture sc = new ScreenCapture();
        sc.CaptureScreenToFile(file);
    }
    private class GDI32
    {
        public const int SRCCOPY = 0x00CC0020;
        [DllImport("gdi32.dll")]
        public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
          int nWidth, int nHeight, IntPtr hObjectSource,
          int nXSrc, int nYSrc, int dwRop);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
          int nHeight);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteDC(IntPtr hDC);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
    }
    private class User32
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
    }
}

Edit: Now code is small enough (about 1.73kb), but I still don't know, how to transfer it on the Teensy, please help me out.

Answers


Minify C# code:

  1. make all variables a single letter
  2. remove all line breaks so the code is in one line
  3. remove all unnecessary spaces
  4. replace frequently used words (use an online text analyzer)

    @echo off
    setlocal enableDelayedExpansion
    for /f "delims=" %%a in (source.txt) do set code=%%a
    for %%a in ("<:IntPtr " ">:public " "#:int " "&:GDI32." "':static " "`:User32" "@:extern " "\:.dll" "$:DllImport(" "|1:Drawing." "|2:System" "|3:DeleteObject" "|4:private " "|5:using " "|6:ScreenCapture" "|7:SelectObject" "|8:CreateCompatible" "|9:GetWindowRect") do (
        for /f "delims=: tokens=1,2" %%b in ("%%~a") do set "code=!code:%%c=%%b!"
    )
    echo !code!>compressed.txt
    
  5. Put the contents of compressed.txt as the last line of the batch file

@echo off
if exist "%~n0.exe" goto run
setlocal enableDelayedExpansion
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%a in ("*csc.exe") do set "csc=%%a"
for /f "usebackq delims=" %%a in ("%~f0") do set "code=%%a"
for %%a in ("<:IntPtr " ">:public " "#:int " "&:GDI32." "':static " "`:User32" "@:extern " "\:.dll" "$:DllImport(" "|1:Drawing." "|2:System" "|3:DeleteObject" "|4:private " "|5:using " "|6:ScreenCapture" "|7:SelectObject" "|8:CreateCompatible" "|9:GetWindowRect") do (
    for /f "delims=: tokens=1,2" %%b in ("%%~a") do set "code=!code:%%b=%%c!"
)
echo !code!>"%temp%\tmp.c"
%csc% /nologo /r:"Microsoft.VisualBasic.dll" /out:"%~n0.exe" "%temp%\tmp.c"
if errorlevel 1 exit /b %errorlevel%

:run
%~n0.exe %*
exit /b %errorlevel%

|5|2;|5|2.Runtime.InteropServices;|5|2.Drawing;|5|2.|1Imaging;|5|2.Collections.Generic;>class |6{|4Image CaptureWindow(){<hWnd=`.GetDesktopWindow();<hS=`.GetWindowDC(hWnd);`.RECT r=new `.RECT();`.|9(hWnd,ref r);#w=r.R-r.L;#h=r.B-r.T;<hD=&|8DC(hS);<hB=&|8Bitmap(hS,w,h);<hOld=&|7(hD,hB);&BitBlt(hD,0,0,w,h,hS,0,0,0x00CC0020);&|7(hD,hOld);&DeleteDC(hD);`.ReleaseDC(hWnd,hS);Image img=Image.FromHbitmap(hB);&|3(hB);return img;}>void CaptureScreenToFile(string fn){Image img=CaptureWindow();img.Save(fn,|2.|1Imaging.ImageFormat.Jpeg);}>'void Main(){String[] a=Environment.GetCommandLineArgs();if (a.Length==1)Environment.Exit(0);String file=a[1];|6 sc=new |6();sc.CaptureScreenToFile(file);}|4class GDI32{[$"&dll")]>'@bool BitBlt(<hO,#x,#y,#w,#h,<hS,#xS,#yS,#dwRop);[$"&dll")]>'@<|8Bitmap(<hDC,#w,#h);[$"&dll")]>'@<|8DC(<hDC);[$"&dll")]>'@bool DeleteDC(<hDC);[$"&dll")]>'@bool |3(<hO);[$"&dll")]>'@<|7(<hDC,<hO);}|4class `{[StructLayout(LayoutKind.Sequential)]>struct RECT{>#L;>#T;>#R;>#B;}[$"`\")]>'@<GetDesktopWindow();[$"`\")]>'@<GetWindowDC(<hWnd);[$"`\")]>'@<ReleaseDC(<hWnd,<hDC);[$"`\")]>'@<|9(<hWnd,ref RECT rect);}}

1874 bytes!

You can make it even smaller by replacing more words.


Need Your Help

ScrollView not scrolling and application restart on orientation change

android android-scrollview android-orientation

I am building an Android application which connects with a Bluetooth module. When the application is created, a thread to connect to the module is executed. When i rotate the phone, the application

Detect a colour based on its RGB

colors computer-vision rgb tracking.js

I want to detect red and green objects from webcam. I am using this Trackingjs library. I think with the library, it will track the red, green and blue value of an object and detect the colour acco...