Return text from an external program

Return text from an external program

OceanaPolynom
Advocate Advocate
2,011 Views
8 Replies
Message 1 of 9

Return text from an external program

OceanaPolynom
Advocate
Advocate

Hello

I have written some vba code that depends on some text to operate.  I would like to place this text in a compiled vb6 exe file.  I want to run this exe file from the vba code so that the text will be returned to the vba program.  Is this even possible?

 

Thanks

John

0 Likes
2,012 Views
8 Replies
Replies (8)
Message 2 of 9

Ed__Jobe
Mentor
Mentor

Not with VBA. Why does the text have to be in an exe? Can you use the registry to store data?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 9

OceanaPolynom
Advocate
Advocate

Hello

I want to put the exe file on a dongle and use it as protection against copying the program.  As of now the text is in the vba code.  I don't know what you mean by the registry.  I am trying to find out now.

 

Thank you very much

 

John

0 Likes
Message 4 of 9

OceanaPolynom
Advocate
Advocate

Hello

What I found so far is this:

 

Public Function ShellRun(sCmd As String) As String
'https://stackoverflow.com/questions/2784367/capture-output-value-from-a-shell-command-in-vba
'Run a shell command, returning the output as a string' Dim oShell As Object Set oShell = CreateObject("WScript.Shell") 'run command' Dim oExec As Object Dim oOutput As Object 'ShellRun("cmd.exe /c dir c:\") Set oExec = oShell.Exec(sCmd) Set oOutput = oExec.StdOut 'handle the results as they are written to and read from the StdOut object' Dim s As String Dim sLine As String While Not oOutput.AtEndOfStream sLine = oOutput.ReadLine If sLine <> "" Then s = s & sLine & vbCrLf Wend ShellRun = s End Function Private Sub CommandButton1_Click() MsgBox ShellRun("cmd.exe /c dir c:\") a$ = ShellRun("cmd.exe /c dir c:\") 'a$ = ShellRun("cmd.exe /c C:\John\David\JHL-DIM64\exe\exe.exe") bozo = somebozo End Sub

When you click on CommandButton1 the results of dir c:\ are shown in the message box.  I thought if the exe program could redirect output to StdOut, then then results could be returned to the vba program.  It didn't work.

 

Thanks again

John

0 Likes
Message 5 of 9

OceanaPolynom
Advocate
Advocate

Hello

In VB6 I tried to adapt some code I found, it was actually for some thing else.

' Declare the needed API functions
'http://www.vbforums.com/showthread.php?141501-stdout-or-stderr-from-vb
Private Declare Function GetStdHandle Lib "kernel32" _
(ByVal nStdHandle As Long) As Long

Private Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, _
lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long

Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long

Private Const STD_ERROR_HANDLE As Long = -12&
Private Const STD_INPUT_HANDLE As Long = -10&
Private Const STD_OUTPUT_HANDLE As Long = -11&

Sub Main()

Dim a$(1 To 66)
a(1) = "APP_4r$0$180 drykip,AL-0@313^m-@203"
a(2) = "APP_4r_M$0$180 drykip,AL-0@313^m-@203"
a(3) = "APP_4r_M_UP$0$180 drykip,AL-0@313^m-@203"
.
.
.
a(63) = "BLKLSS005,m-@227"
a(64) = "BLKLSS006,m-@226"
a(65) = "BLKLSS007,AL-0@227"
a(66) = "stairs_in$0$DV110020,m-@207"
For i% = 1 To 66
b$ = b$ & a(i%)
Next i%

Call Send(b$)
End Sub


Sub Send(s As String)
Dim STDOUT As Long 'handle to standard output
Dim strHTTP As String 'data buffer for HTTP header output

'get the handle to standard output
STDOUT = GetStdHandle(STD_OUTPUT_HANDLE)

'write the string to STDOUT, just like writing to a file
retval = WriteFile(STDOUT, ByVal strHTTP, Len(strHTTP), lngBytesWritten, ByVal 0&)

'close the handle to standard out
retval = CloseHandle(STDOUT)
End Sub


This code doesn't cause an error, but it doesn't return any text

 

Thanks

John

0 Likes
Message 6 of 9

Anonymous
Not applicable

May be I'm missing something, but here are some thoughts

 

I'd say in your VBA program you need to:

- insert the ShellRun() function

- insert following code lines:

Dim a As String
a = ShellRun(commandToRunYourExe) '<--| change commandToRunYourExe to the command needed to actually run your exe

then you have the output of your exe stored in your string variable a, ready for any subsequent match test

 

 

should that work for you, I'm afraid you're back where you started: anybody could still read your VBA code and see both what exe is to be called and what return string is needed to match the test and then write herself an exe to get around your security check

Message 7 of 9

OceanaPolynom
Advocate
Advocate

After thinking about this I realized that you are right.  However not anyone could do what you said, but rather a very small group of people.  So what I would like to know, is what do I have to do at the VB6 (.exe) side so that it will return text to the vba program, after I do what you suggested.

that is

a = ShellRun(commandToRunYourExe)

Thanks very much

John

0 Likes
Message 8 of 9

Anonymous
Not applicable

I'm not a VB6er, so couldn't help you for that

but I guess writing to the console should be one of the basic write function, easy to be found in any VB6 

 

but you could also have your VB6 exe write to a file and then your VBA macro open that file and read it

 

 

BTW, the "hacker" get around I wrote about in my last post can be even simpler than I explained it: if anybody can enter your VBA code, she can simply comment out the lines that take care of the string match validation, so no need to write an exe to properly communicate with VBA even!

0 Likes
Message 9 of 9

OceanaPolynom
Advocate
Advocate

Thanks very much for all your help

0 Likes