Creating and using a DLL file using VB6

Creating and using a DLL file using VB6

Anonymous
Not applicable
369 Views
7 Replies
Message 1 of 8

Creating and using a DLL file using VB6

Anonymous
Not applicable
Hi,

As an experiment a I created a function in VB6

Public Function LaurieTest (sPar as String) as Boolean

Debug.Print "Hello World"
Debug.Print sPar
LaurieTest = True

End Function

I compiled this into a DLL file.

I then registered the DLL file with regsvr32 "LaurieTest.DLL" and was
rewarded with a message saying the file was successfully registered.

I then opened a VBA project in Land Desktop r2i, used Tools References to
attach the DLL to my project

and tried to run the following code

Sub Test (0
Dim bTmp as Boolean
bTmp = LaurieTest ("FOO WAS HERE")
End Sub

I was rewarded with a message that LaurieTest was an unknown function.

Interestingly, I typed it as laurietest and the system knew enough about it
to change the text to LaurieTest, but it would not recognise that I was
trying to add a parameter.

What have I missed in this process ?


--




Laurie Comerford
CADApps
www.cadapps.com.au
0 Likes
370 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
You have to create an object provided by your DLL then use it's
LaurieTest method.

--
Good judgment comes from experience.
Experience comes from bad judgment.

http://www.acadx.com
0 Likes
Message 3 of 8

Anonymous
Not applicable
Hi Frank,

That makes sense, but how do I do it ? Is there some piece of code which
should be inside the DLL which I reference ?

At the time of creating the Dim statement I can see LaurieTest in the pull
down list of features I can dimension as;

eg "Dim MyObject as LaurieTest "

However VBA rejects that line when you try to run the code.

If I try
"Dim MyObject as Object"
it is accepted, but then what do I place in the line below ?
Set MyObject = ??

These steps to me seem to be the core of creating your own DLLs and yet I
can't find any guidelines in any of my VB and VBA books on how to proceed.

The Object browser for the LaurieTest shows: that under the list of Classes
it has:
"Class1" and the one Member of Class1 is "LaurieTest."

Should I have made an effort to provide "Class1" with my own unique name in
the VB environment.

I've tried
Set MyObject = LaurieTest
Set MyObject = Class1
Set MyObject = LaurieTest.Class1

VBA rejects all of them when you try to run the code.

--




Laurie Comerford
CADApps
www.cadapps.com.au


"Frank Oquendo" wrote in message
news:4E48D6A58282FF9F60D87D6E3AEFF520@in.WebX.maYIadrTaRb...
> You have to create an object provided by your DLL then use it's
> LaurieTest method.
>
> --
> Good judgment comes from experience.
> Experience comes from bad judgment.
>
> http://www.acadx.com
>
>
0 Likes
Message 4 of 8

Anonymous
Not applicable
The programmatic id (ProgId) for your object is the name of the
.. Assuming you didn't change anything from the
defaults when you created your ActiveX DLL, the ProgId should
Project1.Class1:

' Project name isn't required but
' it's still a good idea to include it
Dim myObject As Project1.Class1

Set myObject = New Class1
myObject.LaurieTest

--
Good judgment comes from experience.
Experience comes from bad judgment.

http://www.acadx.com
0 Likes
Message 5 of 8

Anonymous
Not applicable
Hi Frank,

For my records and to help others who will almost certainly run into this
problem I'm written the following process menu.

1 Open VB (VB 6 in my case)
From the opening dialog box choose the New tab and choose ActiveX.DLL
This opens Project1 - Class1 (Code) interface

2 Create a suitable Function as though doing it in conventional VBA or
VB. Make it simple, we don't want code complexities to stuff us up at this
stage.
(Down the track this code will almost certainly become more complex as new
features are added. For the moment concentrate on Processs)

3 Right click on Project1 (Project 1) in the Project Explorer interface
and choose Project Properties. Change any values which appear relevant, but
particularly for the exercise, change the Project Name

4 Select Class1 and rename it also.

5 From the File menu choose Make .DLL and save it to a
suitable directory.

6 Close VB

7 From the Windows Start menu Select Run and type regsvr32 "Full FileSpec
of the DLL file". This can be done by typing regsvr32, then using Windows
Explorer to drag the file into the text box. You should get a message
indicating the file was successfully registered.

7 Start AutoCAD and type VBAMAN at the command line. Unload any open
Projects, Select New, then VBA Editor

8 Choose References from the Tools Menu, browse to your DLL file and
select it, being sure to toggle it on in the references list

9 Choose Insert Module from the insert menu.

10 Create a new SUB and then follow Frank's guidelines with:

Dim MyObject as ProjectName.ClassName as you assigned ProjectName and
ClassName in VB
Set MyObject = New ClassName

11 Call the function in the class with
myobject.FunctionName where FunctionName is the name of the
function you created in the DLL
The above line will act as you would expect for the function. If it uses
parameters, then they must be supplied, and if it is defined as a Data type,
this can return the data value to a suitable parameter.
eg MyParameter = myObject.FunctionName

As a general cleanup you may care to remove the DLL from the registry which
can be done with

regsvr32 -U

Thanks again Frank. Having been highly frustrated for 24 hours, I now feel
able to get on with life. I can also see the joy of using the DLLs as a
source of reusable code.

--




Laurie Comerford
CADApps
www.cadapps.com.au





"Frank Oquendo" wrote in message
news:AB9084606B98D0D925EE62058DB09BFB@in.WebX.maYIadrTaRb...
> The programmatic id (ProgId) for your object is the name of the
> .. Assuming you didn't change anything from the
> defaults when you created your ActiveX DLL, the ProgId should
> Project1.Class1:
>
> ' Project name isn't required but
> ' it's still a good idea to include it
> Dim myObject As Project1.Class1
>
> Set myObject = New Class1
> myObject.LaurieTest
>
> --
> Good judgment comes from experience.
> Experience comes from bad judgment.
>
> http://www.acadx.com
>
>
0 Likes
Message 6 of 8

Anonymous
Not applicable
Laurie,
Thank you, thank you, thank you for that clear, step by step explanation.
It's so rare to see these concepts laid out, step by step, all in one place,
for the beginner who may not realize all the accessory processes that need
to be done which the savvy take for granted!

Thank you again,
Mark
0 Likes
Message 7 of 8

Anonymous
Not applicable
Hi Mark,

I'm glowing with embarassment. 🙂

I've just been browsing around Frank Oquendo's web site and found an article
by Frank called "Creating ActiveX DLLs".

It's a must read to expand on my information. One thing though about
sorting out how to do it yourself, is that you tend to remember.

--




Laurie Comerford
CADApps
www.cadapps.com.au


"MP" wrote in message
news:9002B49C5DE73C6B4FEDE9C23D533610@in.WebX.maYIadrTaRb...
> Laurie,
> Thank you, thank you, thank you for that clear, step by step explanation.
> It's so rare to see these concepts laid out, step by step, all in one
place,
> for the beginner who may not realize all the accessory processes that need
> to be done which the savvy take for granted!
>
> Thank you again,
> Mark
>
>
0 Likes
Message 8 of 8

Anonymous
Not applicable
Laurie Comerford wrote in message
> One thing though about sorting out how to do it yourself, is that you tend
to remember.

I absolutely agree and I try really really hard to figure it out myself
before posting questions here.
But after a couple days of many hours of helpfiles(which in most cases I'm
just not smart enough to figure out - even when they don't give
misinformation) and searching the archive of this group and maybe others and
trying many variations as I can think of with the scanty info the help
provided I get to the point where I just cant afford to waste any more time
cause I know someone here could answer in one minute just one little piece
that I don't understand about the interconnections of loading or referencing
or syntax or punctuation or etc etc....cause I'm a bare beginner and don't
know the basics and didn't come from a computer background.
Finally, with great humiliation, i ask my naive question and then, voila`
the floodgates of generosity open and out pour the solutions...thank you
newsgroups everywhere!!!
And thank you again, good luck with your projects.
Mark
0 Likes