Compile/Encode Visual Basic code inside a Macro

J

Jorge Muria

Hi.

I've created a Word macro and now I want to hide the Visual Basic
code. Is there any way to encode/compile the code so that it can be
used but anyone could see it?.


I suppose this question has been already posted but I couln't find it.

Thanks a lot.
 
J

Jezebel

In the IDE go to Tools > Properties > Protection and lock the project for
viewing. If you want a more secure approach, put the code in a DLL, called
from an add-in. A lot more work, but not crackable.
 
M

Malcolm Smith

Yes, there are.

There are two ways. The first is to password protect the code. This I
don't do for two reasons. The first is that one can buy password crackers
and the second is that I believe that the client owns the code.

The problem with protecting the code is that it can cheese off the client
somewhat. I was called in to a major firm because their templates were
broken and the person who wrote them password protected them and then went
off back backing around the other side of the world for six months.

After I had the password cracked she never worked for that client again.
If she didn't have the code password protected she would have continued
working for them when she returned.


So, basically, password protecting the code is, in my view, a waste of
time and counter-productive.



Now for really sensitive stuff; such as passwords, remote login-ids and
the like and for stuff which I really can't have open then I place the
code into an Active-X DLL and then through the COM interface I call that
code.

If you do it that way and have things like user-ids, passwords, URLs or
whatever which are sensitive then make sure that you don't just have them
in as a string; a hex editor can pick them out.

I usually construct sensitive strings from picking out letters from
phrases such as 'The Quick Brown Fox...' and so on.


At the end of the day, if you want to make your code secure; shove it into
a DLL otherwise it isn't as secure as you may think...

- Malc
www.dragondrop.com
 
D

DeeJee

At the end of the day, if you want to make your code secure; shove it into
a DLL otherwise it isn't as secure as you may think...

Thats interesting, but how to make a dll from Word VBA code?
I can't do it in VB, because I get errors of the specific Word code then,
and in VBA editor, I can't find anything to compile to dll.
What do I overlook?
 
M

Malcolm Smith

It depends on how you define your interfaces within the DLL.

Do you use stuff like ActiveDocument within the VB code? If so remember
that a DLL is an out-of-process library and, therefore, has no concept
of what an ActiveDocument is. So the best thing to do is to pass into the
DLL a reference to the Word document and then work from there.

What sort of errors are you getting in your VB code? Or could this be it?

- Malc
www.dragondrop.com
 
D

DeeJee

It depends on how you define your interfaces within the DLL.
Do you use stuff like ActiveDocument within the VB code? If so remember
that a DLL is an out-of-process library and, therefore, has no concept
of what an ActiveDocument is. So the best thing to do is to pass into the
DLL a reference to the Word document and then work from there.

What sort of errors are you getting in your VB code? Or could this be it?

Yes, with Application and ActiveDocument in the code I get
variable not defined. Maybe I misunderstand, but by setting a reference
to the doc in the dll, and have the rest of code in the doc, why use a dll
then?
I work a lot with VB and VBA, but never tried to make a dll,
so can you please point me to the right direction.
Thanks a lot.
 
M

Malcolm Smith

The reason why you are using a DLL is that since the code is compiled to
binary the source code isn't open to examination.

The code you want to hide you place in the DLL. Of course you will need
some code within the Word template; but these could just be nothing more
than single lines which call the routines in the DLL.

For example, you may have code which places information on your company's
intranet and you need a login-id and password to do this.

Putting this information within the VBA is silly as anyone can read it.
So the place is to put the code within the DLL.

I have to ask; what sort of information are you trying to secrete? Is it
your code, the client's or what?

- Malc
 
J

Jezebel

Yes, with Application and ActiveDocument in the code I get
variable not defined. Maybe I misunderstand, but by setting a reference
to the doc in the dll, and have the rest of code in the doc, why use a dll
then?


Simplest is to add a reference to the Word library the VB project, then use
code along these lines:

Dim pWordApp as Word.Application
Dim pWordDoc as Word.Document

Set pWordApp = new Word.Application
Set pWordDoc = pWordApp.ActiveDocument

etc

If you want to run as a DLL rather than an EXE, you'll need to add a little
bit of VBA code (eg in an add-in) to instantiate the DLL object.
 
L

Lars-Eric Gisslén

Malcolm,
If so remember
that a DLL is an out-of-process library

Just out of curiosity, what is your definition of 'out-of-process library'
above?

By normal definitions DLL's are in-process libraries as a DLL is mapped into
the calling process adress space. EXE files used as Automation servers are
out-of-process servers as they run as a separate process and therefor in
it's own address space. That's also the reason why Automation Servers are
slower than ActiveX DLL's because of the mashalling between the the
processes.

Regards,
Lars-Eric
 
M

Malcolm Smith

Lars-Eric

Sorry, got my head the wrong way around today. Was messing with Active-X
Exes here at home and then the cat caught fire (honest!) and then with my
head in a tizz answered this question and got this bit 180 degrees wrong.

Just shove the wrong bits into the shredder...

- Malc
 
M

Malcolm Smith

Opened the front door of the wood burner (my central heating source) and
it's high time for me to get rid of the ash and the cat refused to move
away from the front of the stove.

A hot log rolled out onto the cat. The traumatised beast vanishes with
sparks and smoke coming its fur...

- Malc
 
D

DeeJee

Purrfect. The only good cat is a dead one, anyway.
Hihi, my cat says that it is not nice.
But about the question, is there any exemple on-line somewhere?
I have to ask; what sort of information are you trying to secrete? Is it
your code, the client's or what?
No Malc, I don't have client's at all.
But I like to do automation in Word and Excel mostly for friends,
and it happens that they change things in code without the knowledge.
Because it is not nice indeed to set a password, maybe I can hide code by
using dll.
Thats what I was interested for, but it seems to be not that easy.
 
M

Malcolm Smith

Then the answer is easy. Leave the code open and if they mess with it
then, well, it's their look-out. And if they mess with it then just tell
them to 'put it back'.

Also I would keep a copy of everything you send to them and then you work
on those copies only.

Honestly, it will be the best way in the long run.

- Malc
www.dragondrop.com
 
J

Jezebel

If it's rats and mice you're dealing with, snakes are much more efficient.
And they don't kill birds.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top