[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1065
  • Last Modified:

Embed a batch file in Visual Basic 6

I have a VB program which runs a batch file. How can I embed this batch file into VB itself, so no one can steal/look at the code?


Thanks again
0
Shank1
Asked:
Shank1
  • 7
  • 6
  • 5
  • +2
2 Solutions
 
bobbit31Commented:
off the top of my head...

store the contents of batch file as a constant string.
at runtime, create a file with the content of that string.
run the file (using Shell(...))
delete the file after complete

need to know how?
0
 
RogueSolutionsCommented:
If you look at this request ...

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21183010.html

One of the solutions was to build and execute a BAT file.  Sounds like what you want.
0
 
Shank1Author Commented:
bobbit31, that would be great. It's alot of code, maybe 2 pages worth, so  otherwise I could use Roguesolutions's solution, but thats alot of code to copy over into VB and add quotes into etc..  unless thats the only way to do it


0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
RogueSolutionsCommented:
Shank1

What I do for transferring SQL strings into VB so I can build them into a string (similar I think) is to copy the text into Excel so that each line of text is in a row.

Then build formulae in the column to the right that adds the necessary characters to create a string, so ...

If A1 is ECHO My Batch File
and A2 is ECHO Line two of my file

Then B1 is ="    strBat = strBat & """ & A1 & """ & vbCrLf "
and B2 is ="    strBat = strBat & """ & A2 & """ & vbCrLf "

With a bit of filling down and copying you can get the VB code mostly defined for you.
0
 
bobbit31Commented:
Add the file to Resources...

hold on, i'm making an example now


0
 
Shank1Author Commented:
Rougesolutions : So each line of my batch file needs to be declared?
0
 
RogueSolutionsCommented:

Not sure what you mean by declared.  Here's a fuller example that might help.

Here's a chunk of batch file from my PC, no idea what it does just found it.

@echo off

set CLASSPATH0=%CLASSPATH%
set VC_PATH=%1
if "%VC_PATH%"=="NONE" goto noVC
set NTTOOLS=%VC_PATH%
call %VC_PATH%\bin\vcvars32.bat
goto msEnv

:noVC
rem echo Visual C++ product directory could not be found.
set NTTOOLS=
set LIB=
set INCLUDE=

:msEnv
set MS_DISK=%2
set MS_MSTN=%3
set MS=%MS_DISK%%MS_MSTN%

---

Here's the code to build and run it.

Private Sub WriteMe()
   Open App.Path + "\Delself.bat" For Append As #1
   Print #1, strGetBatchContent()
   Close
   Shell App.Path + "\Delself.bat", vbHide
End Sub

Private Function strGetBatchContent() As String

    Dim strBat  As String
   
    strBat = ""
    strBat = strBat & "@ECHO OFF" & vbCrLf
    strBat = strBat & vbCrLf

    strBat = strBat & "set CLASSPATH0=%CLASSPATH%" & vbCrLf
    strBat = strBat & "set VC_PATH=%1" & vbCrLf
    strBat = strBat & "if ""%VC_PATH%""==""NONE"" goto noVC" & vbCrLf
    strBat = strBat & "set NTTOOLS=%VC_PATH%" & vbCrLf
    strBat = strBat & "call %VC_PATH%\bin\vcvars32.bat" & vbCrLf
    strBat = strBat & "GoTo msEnv" & vbCrLf
    strBat = strBat & vbCrLf

    strBat = strBat & ": noVC" & vbCrLf
    strBat = strBat & "Rem echo Visual C++ product directory could not be found." & vbCrLf
    strBat = strBat & "set NTTOOLS=" & vbCrLf
    strBat = strBat & "set LIB=" & vbCrLf
    strBat = strBat & "set INCLUDE=" & vbCrLf
    strBat = strBat & vbCrLf

    strBat = strBat & ": msEnv" & vbCrLf
    strBat = strBat & "set MS_DISK=%2" & vbCrLf
    strBat = strBat & "set MS_MSTN=%3" & vbCrLf
    strBat = strBat & "set MS=%MS_DISK%%MS_MSTN%" & vbCrLf
    strBat = strBat & vbCrLf

    strGetBatchContent = strBat
   
End Function

Note the vbCrLf's that are on each line and between the 'blocks' of the batch file to ensure the gaps are maintained (just for checking purposes really)

Also any " that were in the original file are repeated to ensure the string builds properly.  You could do this as part of the Excel stage by using the SUBSTITUTE worksheet function, e.g.
="    strBat = strBat & """ & SUBSTITUTE(A1,CHAR(34), CHAR(34) & CHAR(34)) & """ & vbCrLf "

It will be fiddly but secure, no text file exists as part of your install.  You canget the Batch file to delete itself as it's last command.

Perhaps the Resource method from Bobbit will suit your needs better.
0
 
bobbit31Commented:
simply click tools->Resource Editor (if you don't see it... add it via Add-Ins->VB6 Resource Editor)

click "add custom" and select your batch file.
The file will now be stored along w/ the compiled .exe (you don't have to have the actual .bat anywhere on system)

To get the contents of the file from resources:

Dim strFile As String
strFile = StrConv(LoadResData(101, "CUSTOM"), vbUnicode) '' strFile now has contents of batch file (assuming you left the default properties for the resource)

now all you have to do is:

Open App.Path + "\RunMe.bat" For Output As #1
Print #1, strFile
Close (1)
Shell App.Path + "\RunMe.bat", vbHide


and then delete the file on unload.
0
 
RogueSolutionsCommented:

Bobbit.  

I realise I could go read the help ;) but doesn't a resource file 'travel' as a .res when you do your installation?  I remember something about using a Resource file to create language changes (i.e. you substitute the .res of the language the user wants).
0
 
bobbit31Commented:
afaik, no that's not how it works.

The resource file is used when compiling the exe and the contents of the resource file are included in the build.

Try it out and test... do the above steps, make the .exe, delete the .res and run the .exe.  The info will still be there.
0
 
RogueSolutionsCommented:

Fair enough, I've never used one (and no VB on this PC to play with just at the mo).  I expect Shank will feedback, it's definitely less painful than my approach!



0
 
BWarmuskerkenCommented:
Why not encrypt the batch.  Decypt it, run it, deletete he decrypted file.

here's a link to an encryption routine.
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=56449&lngWId=1
0
 
Erick37Commented:
I seem to remember (back in the MSDOS days) there was a utility which converted batch files into executables.  I think it was called bat2exe.  Perhaps if you can compile the .bat file into a .com or .exe file, it would be unreadable.

But is there any reason you could not just convert the functionality of the batch file into real VB code and just run the VB version?
0
 
Shank1Author Commented:
bobbit31: How do I delete the batch file after the exe has been run?
0
 
bobbit31Commented:
Private Sub Form_Unload(Cancel as Integer)
   Kill <path to file>
End Sub
0
 
RogueSolutionsCommented:

You can also have a DEL command in the batch file as the last line and it will delete itself (based on the solution I pasted a link to from another question)
0
 
Shank1Author Commented:
That worked great. Thanks. but the problem is if the program closes unexpectedly, such as you do a shutdown, then the unload call doesn't execute right? If that is the case, I put the kill command as the last command in the form load procedure, right after the shell App.path _ " " code. And if you close the program then, it gives you an error file not found too. can we prevent that from happening?
0
 
bobbit31Commented:
remove the kill statement from form unload?
0
 
bobbit31Commented:
or put del command as last line in batch file as roguesolutions suggested
0
 
Shank1Author Commented:
Perfect, works like a charm. Thanks guys
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 7
  • 6
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now