DosTips.com

A Forum all about DOS Batch
It is currently 19 Aug 2017 22:01

All times are UTC-06:00




Post new topic  Reply to topic  [ 22 posts ]  Go to page Previous 1 2
Author Message
PostPosted: 24 Jun 2015 08:03 
Offline

Joined: 24 Jun 2013 17:10
Posts: 437
Location: Bulgaria
Without redundant output:



Code: Select all
// 2>nul||@goto :batch
/*
:batch
@echo off

setlocal
:: find csc.exe
set "frm=%SystemRoot%\Microsoft.NET\Framework\"
for /f "tokens=* delims=" %%v in ('dir /b /a:d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*.*"') do (
   set netver=%%v
   goto :break_loop
)
:break_loop
set csc=%frm%%netver%\csc.exe
:: csc.exe found
if not exist %~n0.exe (
   %csc% /nologo /out:"%~n0.exe" "%~dpsfnx0" >nul
)
%~n0.exe
echo -- redundant output has been cleared --

endlocal
exit /b 0
*/
public class Hello
{
   public static void Main() {
      ClearC();
      System.Console.WriteLine("Hello, C# World!");
   }

   public static void ClearC() {
      try {
         System.Console.CursorTop = System.Console.CursorTop - 1;
         System.Console.Write(new string(' ', System.Console.BufferWidth));
         System.Console.CursorTop = System.Console.CursorTop - 1;
      } catch (System.IO.IOException e) {
      }
   }
}


though if there are `echo`-es before the c# part you'll need to change the cursor position.And wont work if the file is redirected to a file.Also I'm not sure if Console.CursorTop is available in .net 2 and 3


Top
   
PostPosted: 24 Jun 2015 14:37 
Offline

Joined: 03 May 2015 10:51
Posts: 22
npocmaka_ wrote:

You can do that trick with "cls" too :-)
Code: Select all
//>nul 2>nul||@goto :batch
/*
:batch
@echo off
setlocal
cls
:: find csc.exe
set "frm=%SystemRoot%\Microsoft.NET\Framework\"
for /f "tokens=* delims=" %%v in ('dir /b /a:d  /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
   set netver=%%v
   goto :break_loop
)
:break_loop
set csc=%frm%%netver%\csc.exe
:: csc.exe found

call %csc% /nologo /out:"%~n0.exe" "%~dpsfnx0"
%~n0.exe
endlocal
exit /b 0
*/
public class Hello
{
   public static void Main()
   {
      System.Console.WriteLine("Hello, C# World!");
   }
}


Top
   
PostPosted: 21 Mar 2016 10:56 
Offline

Joined: 24 Jun 2013 17:10
Posts: 437
Location: Bulgaria
I've just found this:

Code: Select all
https://gist.github.com/subTee/28b7439d3dfa07053b61


Looks like something that will allow platform invoke from JScript.net.
Not tested yet.
It will be good it works because in some cases will be a good replacement of C# and its redundant output.
Though it will be not so powerful as jscript does not allow passing by reference.


Top
   
PostPosted: 07 Oct 2016 06:15 
Offline

Joined: 24 Jun 2013 17:10
Posts: 437
Location: Bulgaria
using msbuild (without redundant output and without temp files ! ) :

Code: Select all
<!-- :
    @echo off


        echo -^- FROM BATCH

        set "CMD_ARGS=%*"
        ::::::  Starting C# code :::::::
        :: searching for msbuild location
        for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*msbuild.exe") do  set "msb=%%#"

        if not defined  msb (
           echo no .net framework installed
           exit /b 10
        )

        rem ::::::::::  calling msbuid :::::::::
        call %msb% /nologo  /noconsolelogger "%~dpsfnx0"  /property:"H=From C#"
        rem ::::::::::::::::::::::::::::::::::::
        exit /b %errorlevel%
-->


<Project ToolsVersion="$(MSBuildToolsVersion)" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="_">
    <_/>
  </Target>
  <UsingTask
    TaskName="_"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll" >

    <ParameterGroup  >
         <Z ParameterType="System.String">$(H)</Z>
    </ParameterGroup>

    <Task>
      <Using Namespace="System" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[
            String CMD_ARGS=Environment.GetEnvironmentVariable("CMD_ARGS");
            System.Console.WriteLine("-- "+"$(H)"); 
        ]]>
      </Code>
    </Task>
  </UsingTask>
</Project>



Will have to experiment more with this to see how P/Invoke can be used (something I can't do neat with jscript.net)

https://msdn.microsoft.com/en-us/library/dd722601.aspx


Top
   
PostPosted: 06 Jan 2017 16:02 
Offline

Joined: 24 Jun 2013 17:10
Posts: 437
Location: Bulgaria
More notes on the msbuild technique.
If you need a reference to a dll you'll need to include it in the task node in the xml (alike the simple /r:some.dll in csc compiler)

you cant use the "--" directly in batch part because it will broke the xml parsing (I'll have to think how the batch part can be put in CDATA section... may be will be not possible)

The type can be fragment (as in the previous example) or class or method

Using a class needs more effort than a fragment - you need to implement ITask interface . Though it will be easier to inherit Task class which has some pipe work done. These are in Microsoft.Build.Framework and Microsoft.Build.Utilities namespaces. Doing so you'll have to override Execute method.

The class must be public (though you can have more non public classes) and it should have the same name as the task you want to execute.

Here's an example:

Code: Select all
<!-- :
    @echo off


        echo -^- FROM BATCH

        set "CMD_ARGS=%*"
        ::::::  Starting C# code :::::::
        :: searching for msbuild location
        for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*msbuild.exe") do  set "msb=%%#"

        if not defined  msb (
           echo no .net framework installed
           exit /b 10
        )

        rem ::::::::::  calling msbuid :::::::::
        call %msb% /nologo  /noconsolelogger "%~dpsfnx0"  /property:"H=From C#"
        rem ::::::::::::::::::::::::::::::::::::
        exit /b %errorlevel%
      
-->


<Project ToolsVersion="$(MSBuildToolsVersion)" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Program">
    <Program/>
  </Target>
  <UsingTask
    TaskName="Program"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll" >

    <ParameterGroup  >
         <Z ParameterType="System.String">$(H)</Z>
    </ParameterGroup>

    <Task>
     <Reference Include="$(MSBuildToolsPath)\System.Windows.Forms.dll"/>
      <Using Namespace="System" />
 
      <Code Type="Class" Language="cs">
        <![CDATA[
   using Microsoft.Build.Framework;
   using Microsoft.Build.Utilities;
   using System;
   
   public class Program:Task, ITask
    {
      public override bool Execute(){
         Console.WriteLine("Whoa");
         String CMD_ARGS=Environment.GetEnvironmentVariable("CMD_ARGS");
                        System.Console.WriteLine("-- "+"$(MSBuildToolsVersion)");
         return true;
      }
   }
        ]]>
      </Code>
    </Task>
  </UsingTask>
</Project>


Also the MSBuildToolsVersion and MSBuildToolsPath are no more hard coded in the xml.

As a reference I've used this - https://gist.github.com/subTee/6fa2b0cc ... 0e6d93f31a (in the subtee's gists there are pretty interesting things)

Still not tried to implement a method The msdn does not tell much -
If the value of Type is Method, then the code defines an override of the Execute method of the ITask interface.


I feel a little lonely in my excitement about this - but for sure will try and share the method type task too.


EDIT. Example with method (the msdn explanation was more than enough):

Code: Select all
<!-- :
    @echo off


        echo -^- FROM BATCH

        set "CMD_ARGS=%*"
        ::::::  Starting C# code :::::::
        :: searching for msbuild location
        for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*msbuild.exe") do  set "msb=%%#"

        if not defined  msb (
           echo no .net framework installed
           exit /b 10
        )

        rem ::::::::::  calling msbuid :::::::::
        call %msb% /nologo  /noconsolelogger "%~dpsfnx0"
        rem ::::::::::::::::::::::::::::::::::::
        exit /b %errorlevel%
      
-->


<Project ToolsVersion="$(MSBuildToolsVersion)" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="_">
    <_/>
  </Target>
  <UsingTask
    TaskName="_"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll" >

    <Task>
     <Reference Include="$(MSBuildToolsPath)\System.Windows.Forms.dll"/>
      <Using Namespace="System" />
 
      <Code Type="Method" Language="cs">
        <![CDATA[

      public override bool Execute(){
         MyMethod();
         return true;
      }
      
      void MyMethod(){
         Console.WriteLine("Whoa");
         String CMD_ARGS=Environment.GetEnvironmentVariable("CMD_ARGS");
            System.Console.WriteLine("-- "+"$(MSBuildToolsVersion)");
      }
        ]]>
      </Code>
    </Task>
  </UsingTask>
</Project>


Top
   
PostPosted: 07 Jan 2017 08:58 
Offline

Joined: 26 Oct 2016 22:45
Posts: 10
Nice one npocmaka,

two questions
1. why do you call msbuild.exe?
2. are there remnant files from the build, if yes where are they located?

Wish you a happy new year.


Top
   
PostPosted: 07 Jan 2017 09:13 
Offline

Joined: 24 Jun 2013 17:10
Posts: 437
Location: Bulgaria
LotPings wrote:
Nice one npocmaka,

two questions
1. why do you call msbuild.exe?
2. are there remnant files from the build, if yes where are they located?

Wish you a happy new year.


1.MSBuild works with xml files which can be used in batch files for comment lines without redundant output. the "<!-- :" will be executed as ":<!--" i.e. label and there will be nothing displayed in the console.

2.This is even more beautiful - there are no build files , no temp files - everything is loaded in the memory (may be this mean that it will be faster but I'n not sure). Except there's a compilation error - there will be some source files in the %temp% where you can see in more details what msbuild is doing.


WRONG:The only impediment is that you can't exit with custom error codes if the build is successful it returns 0 otherwise 1 , depending on what is returned from Execute() method.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 22 posts ]  Go to page Previous 1 2

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited