Sumit Rao
Sumit Rao

Reputation: 1

PostPublish.bat file works when called using Command Prompt, Throwing error when called as a Post Publish Event

I created a batch file to obfuscate a dll. When I run that dll using command prompt it is obfuscating dll & giving desired output. When I configure the same batch file to be run after publish of same project it is throwing error & not obfuscating the dll.

Please find below content of all the files:

Content of CSProj file:

 <Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <OutputType>Exe</OutputType>
     <TargetFramework>net5.0</TargetFramework>
   </PropertyGroup>
 
   <Target Name="DllObfuscating" AfterTargets="Publish">
     <Exec Command="D:\Obfuscation_Script\PostBuild.bat $(ProjectDir) $(PublishDir) $(TargetName) $(TargetExt) &quot;$D:\BitMono.CLI\&quot; output\ _Original" />
   </Target>
 
 </Project>

My application has only one class Program.cs, Content of Program.cs:

namespace Program
{
    class Program
    {
        static int Main(string[] args)
        {
            return 0;
        }
    }
}

Content of PostBuild.bat file

echo Starting DLL Obfuscation...

::Dll absolute path
set ProjectDir=%1

::Publish directory path
set PublishDir=%2

::Project Name
set TargetName=%3

::DLL Extension
set TargetExt=%4

::Obfuscator Directory Path
set ObfuscatorPath=%5

::obfuscated dll Output folder
set OutputFolder=%6

::Append Sub-String to original dll
set DllNewNameSubString=%7

set TargetFileName=%TargetName%%TargetExt%

echo Obfuscating %TargetFileName%

::Change Directory to Obfuscator Directory
chdir %obfuscatorPath%

::Run Obfuscatore CLI
::-f dll to obfuscate
::-l location of all dependencies
::-o Path to store obfuscated dll
echo | BitMono.CLI -f %ProjectDir%%PublishDir%%TargetFileName% -l %ProjectDir%%PublishDir% -o %ProjectDir%%PublishDir%%OutputFolder%

::Change directory to Project Publish Directory
chdir %ProjectDir%%PublishDir%

::Update name of Original dll
rename %TargetFileName% %TargetName%%DllNewNameSubString%%TargetExt%

::Move Obfuscate dll out of Output directory
move /Y %OutputFolder%%TargetFileName% .

::Remove Output Directory only if it's empty(after moving obfuscate dll out, it should be empty)
rmdir %OutputFolder%

echo %TargetFileName% Obfuscation Complete.

Getting error from BitMono.CLI only. When triggered as PostBuild Event.

Error:

Something went wrong! System.IO.IOException: The handle is invalid.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
   at System.Console.Clear()
   at BitMono.CLI.Program.<Main>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at BitMono.CLI.Program.<Main>d__2.MoveNext()
Enter anything to exit!

Stuck on this problem since long, any hints or way forward will be very helpful.

Thanks in Advance

I am trying to obfuscate the dll. I tried obfuscating using BitMono CLI it's working. I tried calling BitMono CLI from Command Prompt, It's also working. I tried obfuscating the dll after publish, It's not working.

Upvotes: 0

Views: 80

Answers (1)

Sumit Rao
Sumit Rao

Reputation: 1

This issue is resolved now, I don't know the root cause though.

Explanation will be appreciated.

Earlier:

echo | BitMono.CLI -f "dll-to-obfuscate" -l "dependencies" -o "output-directory"

Fix:

echo | Start BitMono.CLI -f "dll-to-obfuscate" -l "dependencies" -o "output-directory"

Start will run BitMono.CLI in new command prompt & that resolves my issue.

Upvotes: 0

Related Questions