Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing COM though VB.NET

Status
Not open for further replies.

russgreen

Programmer
Dec 7, 2002
86
GB
I am currently developing an application in VB.NET that uses the Microstation's object model via COM Interop.

I have a problem regarding multiple instances of the Microstation application (ustation.exe) and calling the COM object in multiple forms in my application.

The Microstation Application object is demnsioned in Common.mod

Public oMSTN As MicroStationDGN.Application


My application is entered in StartUp.mod and there is a routine that determines if Microstation is already running or not. Different forms load depending on if the user already has an instance if Microstation running or not.


##############################################
# CODE EXECUTED IN STARTUP.MOD #
# TO START MICROSTATION IF IT ISN'T #
# ALREADY RUNNING #
# #
# oMSTN DIMENSIONED IN COMMON.MOD #
# Public oMSTN As Application #
##############################################
Private Sub CheckForProject()
If IsProcessExecuting("ustation") = False Then
projectForm = New frmProj
projectForm.ShowDialog()
Else
'process is executing so get the project
'configuration file currently selected
'and set project ID from database

oMSTN = New MicrostationDGN.Application
sConfigName = oMSTN.ActiveWorkspace.ConfigurationVariableValue("_USTN_PROJECTNAME")

lProj_ID = GetProjIDFromDb(sConfigName)
End If
End Sub

If Microstation is running then a new instance of oMSTN must be started so that my application can read a microstation configuration variable. That works fine, howver, when other forms load I don't know how to reference the same instance of oMSTN. Starting new instances of oMSTN in each form simply starts and extra instance of ustation.exe running in task manager.

How do I start a call this code once "oMSTN = New MicrostationDGN.Application" in my startup module and then use oMSTN in every form that loads after that?

Thanks in advance

Russ

Regards,
Russ

 
Put your code in a class then you can reference the class like you do a form.

BTW I think you could even reference the microstation that is already running, wich you don't seem to do right now.


Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Thanks,

I'm starting to understand that I need to build a singlton class. But what might that class look like if it were to be added to the following simple app?

Russ



Public Class Form1
Inherits System.Windows.Forms.Form

Private oWinWord As Word.Application

#Region " Windows Form Designer generated code "

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Word isn't running so start it
oWinWord = New Word.Application
oWinWord.Visible = True

Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & " documents open"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
oWinWord = New Word.Application

oWinWord.Documents.Add()

Me.Label1.Text = "There are " & CStr(oWinWord.Documents.Count) & " documents open"
End Sub

End Class

Regards,
Russ

 
I don'tunderstand what you really want with this example but if I take you previous one then I would do this

make a new class file

Code:
namespace myclasses
Public class Common
  Public shared oMSTN As MicroStationDGN.Application
end class

Public class Startup
public Sub CheckForProject()
    If IsProcessExecuting("ustation") = False Then
        projectForm = New frmProj
        projectForm.ShowDialog()
    Else
        'process is executing so get the project
        'configuration file currently selected
        'and set project ID from database

        common.oMSTN = New MicrostationDGN.Application
        sConfigName = common.oMSTN.ActiveWorkspace.ConfigurationVariableValue("_USTN_PROJECTNAME")

        lProj_ID = GetProjIDFromDb(sConfigName)
    End If
End Sub
end class
end namespace

and you do the following in the form that calls it

Code:
imports projectname.myclasses

.....

common.oMSTN. ...

I made oMSTN shared.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Don't forget...

Classes typically require an object to execute the methods from...

You could also just use a Module in place of the class...
Code:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckForProject()
        '....
    End Sub
    '....
End Class

Public [b]Module[/b] STARTUP
Public Sub CheckForProject()
    If IsProcessExecuting("ustation") = False Then
        projectForm = New frmProj
        projectForm.ShowDialog()
    Else
        'process is executing so get the project
        'configuration file currently selected
        'and set project ID from database

        oMSTN = New MicrostationDGN.Application
        sConfigName = oMSTN.ActiveWorkspace.ConfigurationVariableValue("_USTN_PROJECTNAME")

        lProj_ID = GetProjIDFromDb(sConfigName)
    End If
End Sub
End [b]Module[/b]

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Classes typically require an object to execute the methods from...
not if they are shared.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
But... if you do not plan to use it as an object, why would you make it a Class instead of a Module?

BTW... you would still want to place...
Public oMSTN As MicroStationDGN.Application
Above the sub in the module...
Code:
Public Module STARTUP
[b]Public oMSTN As MicroStationDGN.Application[/b]
Public Sub CheckForProject()
    If IsProcessExecuting("ustation") = False Then
        projectForm = New frmProj
        ...

In a public Module, all public variables are shared anyways, without having to use the Shared keyword

And the Namespace really isn't needed either, unless you are planning to have mulitple sub or variables named CheckForProject() or oMSTN

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
think OOP, think big, think future.
I know it sounds a bit overkill at this instance but what if he wants more. More modules. Namespace, class it sounds more .net. Module is vb6.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
yeah,
I understand that...

BUT...

If you you want to do that, I would go the whole mile...

Go ahead and use the Class instead of the module, make a few public methods (Sub/Function) that deal specifically to the interface, set up the properties that point to private variables and/or private sub/functions from external libraries, then you can create the class object in the form's constructor, and delete it in the destructor... (or form Load/Leave in vb)

I have set up several things that use legacy external libraries and such, and it works great...

But if you are used to Modules in VB6 and not ready to take the full leap to .Net & all the bells and whistles of OOP then the Modules are still available, if you want to keep it simple...

But still... I do agree with you on the moving to OOP and .Net stuff, It looks like it has much potential in the long run...

I still use Modules from time to time if I have several classes in a project that need shared variables and such, but that is only because I am not 100% comfortable with inherits and implements and all the other "New in .Net" words...

I did start using inherits the other day, and I'm Luvin it ;-)
...At least so far :p

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top