Home All Groups Group Topic Archive Search About

Creating a DLL and calling it from an app

Author
31 May 2005 8:03 PM
Faraz Azhar
Hello

I want to store my most common code into a DLL. Creating DLLs is easy
enough, but I have a problem. I dont want it to be viewable in the Object
Browser. That is, once my whole application is installed on a user's
computer... the DLL will get registered and the user will be able to browse
through all the functions available in the DLL through VB. I dont want that
to happen.

Plus, I cant seem to create DLLs similar to the ones provided in Windows.
ie. I cant seem to create the 'Declare Function ...' call for them.

I tried creating an ActiveX DLL. Added a class module to it. And added a
function called ShowMsgBox to it. Now in my normal project, I created a
declaration:

  Private Declare Function ShowMsgBox Lib "dll_project" () As Long

When I called this function like this it gives an 'entry point' error:

Private Sub Command1_Click()
    ShowMsgForm
End Sub


My aim is:
* I dont want the DLL to be viewable in the object browser; so the end-user
cant mess around with my private functions.
* I want to access my DLL in a way similar to accessing APIs. ie. using the
Declare Function.. statements
* I think this can be achieved without registering the DLL even.
* Finally, I think all this can be achieved without creating a license key
and setting up a security system. Can it?

Please thoroughly guide me through the creation process. Thank you very
much!

Faraz Azhar

Author
31 May 2005 8:32 PM
Ken Halter
Show quote Hide quote
"Faraz Azhar" <itz_farazSPAM@hotSPAMmail.com> wrote in message
news:O5eNwxhZFHA.3712@TK2MSFTNGP09.phx.gbl...
> Hello
>
> I want to store my most common code into a DLL. Creating DLLs is easy
> enough, but I have a problem. I dont want it to be viewable in the Object
> Browser. That is, once my whole application is installed on a user's
> computer... the DLL will get registered and the user will be able to
> browse
> through all the functions available in the DLL through VB. I dont want
> that
> to happen.
>
> Plus, I cant seem to create DLLs similar to the ones provided in Windows.
> ie. I cant seem to create the 'Declare Function ...' call for them.
>
> I tried creating an ActiveX DLL. Added a class module to it. And added a
> function called ShowMsgBox to it. Now in my normal project, I created a
> declaration:
>
>  Private Declare Function ShowMsgBox Lib "dll_project" () As Long
>
> When I called this function like this it gives an 'entry point' error:
>
> Private Sub Command1_Click()
>    ShowMsgForm
> End Sub
>
>
> My aim is:
> * I dont want the DLL to be viewable in the object browser; so the
> end-user
> cant mess around with my private functions.
> * I want to access my DLL in a way similar to accessing APIs. ie. using
> the
> Declare Function.. statements
> * I think this can be achieved without registering the DLL even.
> * Finally, I think all this can be achieved without creating a license key
> and setting up a security system. Can it?
>
> Please thoroughly guide me through the creation process. Thank you very
> much!
>
> Faraz Azhar

"Plain vanilla" VB will create only ActiveX components. While you can mark
your methods as "Hidden" so they don't normally show in the object browser,
nothing stops the end developer from selecting the "Show Hidden Members" box
in the object browser.

Using the add-in below, you can create dlls that require Declares, etc and
that contain functionality that won't show in the object browser. fwiw, I've
never tried doing this kind of thing. I like intellisense way too much to
get rid of it <g>

vbAdvance
http://www.vbadvance.com/

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..
Author
31 May 2005 8:33 PM
Mike D Sutton
> I want to store my most common code into a DLL. Creating DLLs is easy
> enough, but I have a problem. I dont want it to be viewable in the Object
> Browser. That is, once my whole application is installed on a user's
> computer... the DLL will get registered and the user will be able to browse
> through all the functions available in the DLL through VB. I dont want that
> to happen.

Why?  You can even do this to a degree with standard DLL files (by using the dependency walker among other things), so
I'm not sure why you're trying to hide it..

> Plus, I cant seem to create DLLs similar to the ones provided in Windows.
> ie. I cant seem to create the 'Declare Function ...' call for them.

These are often known a "Standard DLL"'s and VB cannot create them (at least on its own, I believe there are add-ins
that allow for this though.)  If you simply don't want the user to be able to access the functionality of the DLL then
set the "Instancing" of the class to 1-Private which means that it can only be used within the DLL.

> I tried creating an ActiveX DLL. Added a class module to it. And added a
> function called ShowMsgBox to it. Now in my normal project, I created a
> declaration:
>
>   Private Declare Function ShowMsgBox Lib "dll_project" () As Long
>
> When I called this function like this it gives an 'entry point' error:

Yes, because you're using an ActiveX DLL rather than a standard DLL - Remember that your function is actually inside a
class so you must first start with creating an instance of that class then you can call the method(s) within it.  You
can also set the instancing to 6-GlobalMultiUse which allows you to call the functions directly without having to create
an instance of the class first, this does add additional overhead to your application though (every call you make to it
has to check to see if a class already exists and if not, create it) so use sparingly..

> My aim is:
> * I dont want the DLL to be viewable in the object browser; so the end-user
> cant mess around with my private functions.

The combination of a Private class and/or hidden procedures (tools -> Procedure attributes -> Advanced -> Attributes ->
Hide this member) may be enough for you here, you can't fully hide them from the object browser with ActiveX DLL's
though AFAIK.

> * I want to access my DLL in a way similar to accessing APIs. ie. using the
> Declare Function.. statements

Then you'll need to look into standard DLL's

> * I think this can be achieved without registering the DLL even.

Correct, only ActiveX DLL's need to be registered with the system.

> * Finally, I think all this can be achieved without creating a license key
> and setting up a security system. Can it?

Yup

> Please thoroughly guide me through the creation process. Thank you very
> much!

Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
31 May 2005 9:03 PM
mr_unreliable
hi Mike,

Here's a wild idea.

Compile your ActX DLL into a resource file, using the resource
compiler, then add it to your app.

At execution time, before you access the dll, load the dll into
memory from it's place among the resources (LoadResData), then
copy it to a temporary file, and register it.

Then use it directly from your app.

When you are finished, un-register the dll, and delete it.

Then, any pesky snoopers will have difficulty looking at your
valuable code (unless they just happen to be around to look
when you are running).

cheers, jw
____________________________________________________________

You got questions?  WE GOT ANSWERS!!!  ..(but,
    no guarantee the answers will be applicable to the questions)
Author
31 May 2005 9:18 PM
Mike D Sutton
Show quote Hide quote
> Compile your ActX DLL into a resource file, using the resource
> compiler, then add it to your app.
>
> At execution time, before you access the dll, load the dll into
> memory from it's place among the resources (LoadResData), then
> copy it to a temporary file, and register it.
>
> Then use it directly from your app.
>
> When you are finished, un-register the dll, and delete it.
>
> Then, any pesky snoopers will have difficulty looking at your
> valuable code (unless they just happen to be around to look
> when you are running).

It's feasible, however since the DLL is registered with the system during runtime then any other application can 'snoop'
(or even copy) the DLL whilst it's registered which somewhat defeats the point :)
Cheers,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
31 May 2005 9:34 PM
Faraz Azhar
> It's feasible, however since the DLL is registered with the system during
runtime then any other application can 'snoop'
> (or even copy) the DLL whilst it's registered which somewhat defeats the
point :)
> Cheers,
>
>     Mike
>

and what if by any reason program or windows crash, or theres a power
failure or anything.. the dll would keep registered until next time the
program is ran and exited cleanly.
Author
31 May 2005 9:15 PM
Faraz Azhar
>
> Why?  You can even do this to a degree with standard DLL files (by using
the dependency walker among other things), so
> I'm not sure why you're trying to hide it..

thats different.. i can accept that degree level.

>
> > Plus, I cant seem to create DLLs similar to the ones provided in
Windows.
> > ie. I cant seem to create the 'Declare Function ...' call for them.
>
> These are often known a "Standard DLL"'s and VB cannot create them (at
least on its own, I believe there are add-ins
> that allow for this though.)  If you simply don't want the user to be able
to access the functionality of the DLL then
> set the "Instancing" of the class to 1-Private which means that it can
only be used within the DLL.
>

uh.. question : VB cannot create standard DLLs.. but an add-in for VB (eg.
vbAdvance) can make VB create standard DLL ?

how? why ? (just curious)

Cant this thing without purchasing add-ins, etc? Can VC++ be of any help in
this?


Faraz Azhar
Author
31 May 2005 9:45 PM
Mike D Sutton
> uh.. question : VB cannot create standard DLLs.. but an add-in for VB (eg.
> vbAdvance) can make VB create standard DLL ?

Correct.

> how? why ? (just curious)

If everyone knew that vbAdvance would be out of a job ;)
I would imagine it probably plugs into the linker or something low level like that and simply spits out the correct DLL
signatures, there's no reason that VB shouldn't be able to create standard DLL's (there's nothing particularly special
about them) it was just never added as a feature.
Be aware though that even though it will spit out standard DLL files, they are not quite as portable as those created in
other languages as they still require the VB runtime (and indeed will require the engine starting before calls are made
to the DLL.)

> Cant this thing without purchasing add-ins, etc? Can VC++ be of any help in
> this?

Yes, you could write the DLL in VC++ or Delphi, indeed there's an article on my site about how to author a DLL in VC++
and use it in VB.
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
31 May 2005 10:01 PM
Faraz Azhar
> Yes, you could write the DLL in VC++ or Delphi, indeed there's an article
on my site about how to author a DLL in VC++
> and use it in VB.

Ok thanks for all that info. Im still a bit shakey at using VC++, not much
aware of the coding techniques in there. Could you also tell if there is any
newsgroup for VC++ 6 just like this newsgroup so I could ask beginner level
questions in it?

Thanks

Faraz Azhar
Author
31 May 2005 10:28 PM
Mike D Sutton
> Ok thanks for all that info. Im still a bit shakey at using VC++, not much
> aware of the coding techniques in there. Could you also tell if there is any
> newsgroup for VC++ 6 just like this newsgroup so I could ask beginner level
> questions in it?

I would imagine the microsoft.public.vc.* groups would be where to go for that, I don't know offhand which you would
want to post to though.
Hope this helps,

    Mike


- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Author
31 May 2005 9:56 PM
Jonathan Wood
Faraz,

> Cant this thing without purchasing add-ins, etc? Can VC++ be of any help
in
> this?

Absolutely. C/C++ works just fine.

I'd be happy to write a C DLL for you but I'm afraid that wouldn't be free
either. :-(

--
Jonathan Wood
SoftCircuits
http://www.softcircuits.com
Available for consulting: http://www.softcircuits.com/jwood/resume.htm
Author
1 Jun 2005 5:47 PM
Faraz Azhar
OK Ive downloaded vbAdvance.. have experimented it. I can create standard
DLLs and call the functions using Declare statements.

New problem: I am trying to place a form inside the DLL. So if I call the
function from my app to the DLL, it would display a form (nothing much in
that form, a simple plain window).

It crashes my app when I try to call the function which is supposed to
display the form. Why? Any alternate for it?

my code
======
I create an activex dll, add a module to it. add a sub to it and call it
ShowMyForm. In that sub the code is:
Sub ShowMyForm()
    Form1.Show vbModal
End Sub

I put a check at this sub in the Exports list of vbAdvance. It creates the
DLL all ok. I declare the sub in my app:

Private Declare Sub ShowForm Lib "MyDLL.dll" ()

And then call the ShowForm sub in a command button event. It crashes my app.
If I dont display a form in the DLL, instead I display a messagebox, it
works ok.


Faraz Azhar
Author
1 Jun 2005 6:16 PM
Ken Halter
"Faraz Azhar" <itz_farazSPAM@hotSPAMmail.com> wrote in message
news:usCyeItZFHA.3780@tk2msftngp13.phx.gbl...
>
> And then call the ShowForm sub in a command button event. It crashes my
> app.
> If I dont display a form in the DLL, instead I display a messagebox, it
> works ok.
>

You might have better luck with the ShowWindow API. This API is required if
you want to show modeless forms from C++ or Delphi so it may help here (note
that I mentioned Modeless).
'===========
Option Explicit

Public Enum ShowWinStates
   SW_HIDE = 0
   SW_SHOWNORMAL = 1
   SW_NORMAL = 1
   SW_SHOWMINIMIZED = 2
   SW_SHOWMAXIMIZED = 3
   SW_MAXIMIZE = 3
   SW_SHOWNOACTIVATE = 4
   SW_SHOW = 5
   SW_MINIMIZE = 6
   SW_SHOWMINNOACTIVE = 7
   SW_SHOWNA = 8
   SW_RESTORE = 9
   SW_SHOWDEFAULT = 10
   SW_FORCEMINIMIZE = 11
   SW_MAX = 11
End Enum

Private Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

Public Sub ShowForm(State As ShowWinStates)
   Call ShowWindow(Me.hWnd, State)
End Sub
'===========


--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Sign up now to help keep VB support alive - http://classicvb.org/petition
Please keep all discussions in the groups..
Author
3 Jun 2005 7:20 PM
Faraz Azhar
> Private Declare Function ShowWindow Lib "user32" _
>  (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
>
> Public Sub ShowForm(State As ShowWinStates)
>    Call ShowWindow(Me.hWnd, State)
> End Sub
> '===========

This would work IF the form is loaded, right. Its not even loading the form.
Take a loot at this code which is in a module:

Public Sub DisplayForm()

    Load Form1
    Form1.ShowWindow( whatever )

End Sub


After compiling the standard DLL and calling this sub from another compiled
exe, it gives error "ActiveX component cant create object" when the flow
reaches 'Load Form1'. Its not even loading it.

Cant forms be added to standard DLLs created in VB (using vbAdvance) ?

Faraz Azhar