.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Call vb.net dll from VBA?

18 REPLIES 18
Reply
Message 1 of 19
Anonymous
1775 Views, 18 Replies

Call vb.net dll from VBA?

So I have this VB.net app (exe) that I have compiled to a DLL. I need to try
and call it from within AutoCAD in order keep it "in process". VBA Mgr won't
let me add it as a reference, so is there some other way to call it from
VBA? I tried this statement: Declare Sub frmDLPlot_Load Lib "HCYU_DLP"
(ByVal N As Long) where HCYU_DLP is the name of the DLL and frmDLPlot_Load
is the name of the startup function in my app... I've also copied the dll to
my system32 folder...

It's entirely possible I'm doing something wrong as I am a poor hack that
has never tried anything like this before. Any help would be greatly
appreciated!

Thanks!
18 REPLIES 18
Message 2 of 19
Anonymous
in reply to: Anonymous

You need to set up your .NET assembly as a COM server.
--
Bobby C. Jones

"pkirill" wrote in message
news:5178116@discussion.autodesk.com...
So I have this VB.net app (exe) that I have compiled to a DLL. I need to try
and call it from within AutoCAD in order keep it "in process". VBA Mgr won't
let me add it as a reference, so is there some other way to call it from
VBA? I tried this statement: Declare Sub frmDLPlot_Load Lib "HCYU_DLP"
(ByVal N As Long) where HCYU_DLP is the name of the DLL and frmDLPlot_Load
is the name of the startup function in my app... I've also copied the dll to
my system32 folder...

It's entirely possible I'm doing something wrong as I am a poor hack that
has never tried anything like this before. Any help would be greatly
appreciated!

Thanks!
Message 3 of 19
Anonymous
in reply to: Anonymous

Does that mean I have to start over?


"Bobby C. Jones" wrote in message
news:5178089@discussion.autodesk.com...
You need to set up your .NET assembly as a COM server.
--
Bobby C. Jones

"pkirill" wrote in message
news:5178116@discussion.autodesk.com...
So I have this VB.net app (exe) that I have compiled to a DLL. I need to try
and call it from within AutoCAD in order keep it "in process". VBA Mgr won't
let me add it as a reference, so is there some other way to call it from
VBA? I tried this statement: Declare Sub frmDLPlot_Load Lib "HCYU_DLP"
(ByVal N As Long) where HCYU_DLP is the name of the DLL and frmDLPlot_Load
is the name of the startup function in my app... I've also copied the dll to
my system32 folder...

It's entirely possible I'm doing something wrong as I am a poor hack that
has never tried anything like this before. Any help would be greatly
appreciated!

Thanks!
Message 4 of 19
Anonymous
in reply to: Anonymous

pkirill wrote:

> Does that mean I have to start over?

I have a limited knowledge of this was able to pull it off.

In the Compile section of your projects properties turn on 'Register for
COM interop'.

Terry
Message 5 of 19
Anonymous
in reply to: Anonymous

Probably not. Do a search for "Interop" in this group. There are some
posts that go into detail.

Have fun 🙂
--
Bobby C. Jones


"pkirill" wrote in message
news:5178161@discussion.autodesk.com...
Does that mean I have to start over?


"Bobby C. Jones" wrote in message
news:5178089@discussion.autodesk.com...
You need to set up your .NET assembly as a COM server.
--
Bobby C. Jones

"pkirill" wrote in message
news:5178116@discussion.autodesk.com...
So I have this VB.net app (exe) that I have compiled to a DLL. I need to try
and call it from within AutoCAD in order keep it "in process". VBA Mgr won't
let me add it as a reference, so is there some other way to call it from
VBA? I tried this statement: Declare Sub frmDLPlot_Load Lib "HCYU_DLP"
(ByVal N As Long) where HCYU_DLP is the name of the DLL and frmDLPlot_Load
is the name of the startup function in my app... I've also copied the dll to
my system32 folder...

It's entirely possible I'm doing something wrong as I am a poor hack that
has never tried anything like this before. Any help would be greatly
appreciated!

Thanks!
Message 6 of 19
Anonymous
in reply to: Anonymous

Thanks, Terry. Not that I had anything to do with it, but it was already
turned on... Hmm...


"Terry W. Dotson" wrote in message
news:5178177@discussion.autodesk.com...
pkirill wrote:

> Does that mean I have to start over?

I have a limited knowledge of this was able to pull it off.

In the Compile section of your projects properties turn on 'Register for
COM interop'.

Terry
Message 7 of 19
Anonymous
in reply to: Anonymous

Well I searched and didn't find anything the helpful to me. Probably because
I didn't understand something I was reading...

I was able to add HCYU_DLP.tlb as a reference and now at least I can
display my forms from within AutoCAD. However the class and modules in my
app aren't running... The class that I have reads an INI file and
reorganizes the data and puts it into a list box for selection.

Any tips?


"Bobby C. Jones" wrote in message
news:5178211@discussion.autodesk.com...
Probably not. Do a search for "Interop" in this group. There are some
posts that go into detail.

Have fun 🙂
--
Bobby C. Jones


"pkirill" wrote in message
news:5178161@discussion.autodesk.com...
Does that mean I have to start over?


"Bobby C. Jones" wrote in message
news:5178089@discussion.autodesk.com...
You need to set up your .NET assembly as a COM server.
--
Bobby C. Jones

"pkirill" wrote in message
news:5178116@discussion.autodesk.com...
So I have this VB.net app (exe) that I have compiled to a DLL. I need to try
and call it from within AutoCAD in order keep it "in process". VBA Mgr won't
let me add it as a reference, so is there some other way to call it from
VBA? I tried this statement: Declare Sub frmDLPlot_Load Lib "HCYU_DLP"
(ByVal N As Long) where HCYU_DLP is the name of the DLL and frmDLPlot_Load
is the name of the startup function in my app... I've also copied the dll to
my system32 folder...

It's entirely possible I'm doing something wrong as I am a poor hack that
has never tried anything like this before. Any help would be greatly
appreciated!

Thanks!
Message 8 of 19
Anonymous
in reply to: Anonymous

not that this solves the problem, but MS offers a free book on the subject of using .net dlls with VB6/VBA.
Its actually very well written and is worth reading, even though its free.
http://msdn.microsoft.com/vbrun/staythepath/additionalresources/upgradingvb6/

pkirill
|>So I have this VB.net app (exe) that I have compiled to a DLL. I need to try
|>and call it from within AutoCAD in order keep it "in process". VBA Mgr won't
|>let me add it as a reference, so is there some other way to call it from
|>VBA? I tried this statement: Declare Sub frmDLPlot_Load Lib "HCYU_DLP"
|>(ByVal N As Long) where HCYU_DLP is the name of the DLL and frmDLPlot_Load
|>is the name of the startup function in my app... I've also copied the dll to
|>my system32 folder...
|>
|>It's entirely possible I'm doing something wrong as I am a poor hack that
|>has never tried anything like this before. Any help would be greatly
|>appreciated!
|>
|>Thanks!
James Maeding
Civil Engineer and Programmer
jmaeding - athunsaker - com
Message 9 of 19
saluki
in reply to: Anonymous

Have you thought about converting your vba app to vb .net?

I migrated a few vba apps to .net relatively painlessly.

Another option would be to netload your vb.net dll then just issue the command to run the .net app to the command line through vba.
(If you don't want to netload it you could autoload it by modifying the registry).

good luck
Message 10 of 19
Chumplybum
in reply to: Anonymous

apart from checking 'register for COM interop' you probably need to make sure that you're using a COM Class, as opposed to just a class... if you add a new COM Class to your project vs.net will automatically create GUID's which provide a COM identity.
this should then allow you to add it as a reference in VBA. From my experience, this works fine on the machine that you're compiling the DLL from, however, if you are using the dll on other computers you'll need to Strong Name your app and also load it into the GAC... the easiest way i've found to do this is to add a new setup/deployment project to your current project. the setup/deployment msi then does all the strong naming and loading / registrying your app (DLL file)
Message 11 of 19
Anonymous
in reply to: Anonymous

To all - thanks for your help with this. I really appreciate the lessons!
I'm still stuck, though...

Okay, so after researching it, I *think* I added a COM Class - or rather
converted my existing class to a COM Class. Like this:

clsLoadDwgList.EventsId)> _
Public Class clsLoadDwgList
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "E7606F7A-8B38-4C5B-8115-B1B608EE9346"
Public Const InterfaceId As String = "BBBF7781-2CC6-457B-B069-47650FC39225"
Public Const EventsId As String = "1DB37C24-9477-48E3-873D-9CEA3737510B"
#End Region

Public Sub New()
MyBase.New()
End Sub

Public Shared blnDullesSpecial As Boolean = False 'Is this a Dulles Project
File
Public Shared blnDullesSpecialAutoCreate As Boolean = False
Public Shared strDrawingName...

Public Sub LoadIni()
intNumber = InStrLast(strDwgListini, "\") 'looks at path to _dwglist.ini and
finds last "\"
strFilePath =.......

And now, I can see clsLoadDwgList as an object(?) in VBA (Dim loadini as New
... gives me a option to select clsLoadDwgList.) But I guess I thought I'd
see another option in the Add References Dialog. Am I still doing something
wrong?

I also found some good information here:
http://msdn2.microsoft.com/en-us/library/57kx88c8.aspx



wrote in message news:5179931@discussion.autodesk.com...
apart from checking 'register for COM interop' you probably need to make
sure that you're using a COM Class, as opposed to just a class... if you add
a new COM Class to your project vs.net will automatically create GUID's
which provide a COM identity.
this should then allow you to add it as a reference in VBA. From my
experience, this works fine on the machine that you're compiling the DLL
from, however, if you are using the dll on other computers you'll need to
Strong Name your app and also load it into the GAC... the easiest way i've
found to do this is to add a new setup/deployment project to your current
project. the setup/deployment msi then does all the strong naming and
loading / registrying your app (DLL file)
Message 12 of 19
jbooth
in reply to: Anonymous

What version of Autocad are you trying to use? Are you using the .NET ObjectArx object library?

Autocad 2006 can use the ObjectArx wrappers to declare commands that are defined in a seperate .NET library (like something you write on your own). Older versions require you to write a VBA macro that communicates to your dll via COM interop.

To expose something to com:
- The "Register for COM Interop" checkbox must be enabled
- You must have at least one ComClass with at least one public method that is not a constructor (this can be done by adding a ComClass to your project).

To call it from AutoCad all you should need to do is reference the project and use the following code:

Dim myObject as new MyComClass()
call myObject.RunMyComClassMethod()

There are no special options required when adding your dll as a reference.


I hope this helps, but if not it may be more helpful to us to know exactly what errors you are getting while trying to run the COM class.

Regards,
JB
Message 13 of 19
Anonymous
in reply to: Anonymous

Well, I think that did it. I have some more work to do in getting things to
clear out once I'm done - like now if I run the app, it load the drawings
into a list, then if I run it again, it loads the list again without
clearing out the first. (Restarting AutoCAD, of course, clears it out. ) But
at least I know it's seeing whole thing...

And I get an unhandled exception: "Cannot access a disposed object. Object
name: 'frmArchive'" This is a form that is called if more than 3 drawings
are selected asking you if you want to archive this set... Do I need to make
a VBA reference to all my forms?


wrote in message news:5180314@discussion.autodesk.com...
What version of Autocad are you trying to use? Are you using the .NET
ObjectArx object library?

Autocad 2006 can use the ObjectArx wrappers to declare commands that are
defined in a seperate .NET library (like something you write on your own).
Older versions require you to write a VBA macro that communicates to your
dll via COM interop.

To expose something to com:
- The "Register for COM Interop" checkbox must be enabled
- You must have at least one ComClass with at least one public method that
is not a constructor (this can be done by adding a ComClass to your
project).

To call it from AutoCad all you should need to do is reference the project
and use the following code:

Dim myObject as new MyComClass()
call myObject.RunMyComClassMethod()

There are no special options required when adding your dll as a reference.


I hope this helps, but if not it may be more helpful to us to know exactly
what errors you are getting while trying to run the COM class.

Regards,
JB
Message 14 of 19
jbooth
in reply to: Anonymous

You are trying to call a method on your frmArchive object after it was disposed. Try creating a new one instead, or try not closing it until you know you won't need it any more.

Are you specifically calling the Dispose method in your code somewhere? .The NET framework doesn't really dispose of any objects you create unless there are no references to it. The only way I know of to get this kind of error is to call dispose manually, which usually isn't done unless it's a database connection or file stream.
Message 15 of 19
cgay
in reply to: Anonymous

pkirill,

What is it that you are trying to accomplish?
What Version of AutoCAD are you using?
What Version of Visual Studio are you using?
If this is Acad 2005/06/07 why are you not using some managed solution (this may negate the need to set up a com component)?
Why use VBA at all?
Will you post some example code or a sample project?

You are asking very specific questions that are dependant on what versions of AutoCAD/VisualStudio you are using. Unfortunately I don't believe that anyone can help you more than they have if you can't give us some direction.

My point is that we want to help, but it is up to you to accurately describe your problems/questions so we have enough to go on, not just guess.

Regards,
C
Message 16 of 19
Anonymous
in reply to: Anonymous

Much thanks for your input. Responses to your questions are below.

>What is it that you are trying to accomplish?
I'm trying to run an external vb.net app inside AutoCAD in order to keep it
"in-process" and prevent AutoCAD from bogging down when processing more than
20 or so drawings. This app essentially opens AutoCAD, opens a drawing from
list, and performs one or more actions on that drawing, closes it and then
moves on to the next drawing in the list.

>What Version of AutoCAD are you using?
AutoCAD 2005

>What Version of Visual Studio are you using?
VS 2003

>If this is Acad 2005/06/07 why are you not using some managed solution
>(this may negate the need to set up a com component)?
I'm a .NET newbie and hack and don't know what a "managed solution" means.
And the VB.net app I have already built "works" outside of AutoCAD, but just
bogs down (meaning it goes from taking 10-15 seconds per drawing to 2-3
minutes per drawing) after about 20 drawings.

>Why use VBA at all?
Because otherwise, the app runs "out of process" and starts to bog down. I'm
not 100% sure the "in-process/out-of-process" is the real issue, but it
seems to be the most likely candidate.

>Will you post some example code or a sample project?
It's a big project with several forms, modules, etc. But I'm happy to email
it to anyone who wants to take a real look at it... I'd love for someone to
take a look at it just to tell me how screwed up component organization is!

>You are asking very specific questions that are dependant on what versions
>of AutoCAD/VisualStudio you are using. >Unfortunately I don't believe that
>anyone can help you more than they have if you can't give us some
>direction.

>My point is that we want to help, but it is up to you to accurately
>describe your problems/questions so we have enough to go >on, not just
>guess.

>Regards,
>C
Message 17 of 19
cgay
in reply to: Anonymous

pkirill,

I am willing to take a look at your project. Please email it to CougerAC(at)hotmail.com. Replace (at) with the @ symbol. Also, send revelant drawings and some info on what your programs does to them. Hopefully I'll be able to advise on your issue(s).

C
Message 18 of 19
Anonymous
in reply to: Anonymous

Thanks for your assist - your previous post was the bomb. I reverted back to
a non-hacked up version of the app, added the com class and used your
suggestion int he vba app and - bingo - it worked. Still have a small issue
of the data not clearing after the first use, but I'm working that out...


wrote in message news:5181177@discussion.autodesk.com...
You are trying to call a method on your frmArchive object after it was
disposed. Try creating a new one instead, or try not closing it until you
know you won't need it any more.

Are you specifically calling the Dispose method in your code somewhere? .The
NET framework doesn't really dispose of any objects you create unless there
are no references to it. The only way I know of to get this kind of error is
to call dispose manually, which usually isn't done unless it's a database
connection or file stream.
Message 19 of 19
Anonymous
in reply to: Anonymous

Thanks for your assist - your previous post was the bomb. I reverted back to
a non-hacked up version of the app, added the com class and used your
suggestion int he vba app and - bingo - it worked. Still have a small issue
of the data not clearing after the first use, but I'm working that out...


wrote in message news:5181177@discussion.autodesk.com...
You are trying to call a method on your frmArchive object after it was
disposed. Try creating a new one instead, or try not closing it until you
know you won't need it any more.

Are you specifically calling the Dispose method in your code somewhere? .The
NET framework doesn't really dispose of any objects you create unless there
are no references to it. The only way I know of to get this kind of error is
to call dispose manually, which usually isn't done unless it's a database
connection or file stream.



--
----------------------------------------
I am using the free version of SPAMfighter for private users.
It has removed 95 spam emails to date.
Paying users do not have this message in their emails.
Get the free SPAMfighter here: http://www.spamfighter.com/len

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost