Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / Objective C

DCOM D-Mystified: Questions and Answers

Rate me:
Please Sign up or sign in to vote.
4.75/5 (17 votes)
11 Aug 2000CPOL 545.6K   4.9K   50   199
Brian's detailed answers to your most frequently asked questions about his DCOM tutorial.

Introduction

Welcome to the Questions and Answers page for the DCOM D-Mystified tutorial. I have created this extra page so I can more richly (such as with screenshots and following my source code conventions) provide you, my readers, with answers to your frequently asked questions. These are questions that you either ask me directly by e-mail, or that you post to The Code Project's message boards at the ends of each of the steps of the tutorial.

Source Code Archive

If you follow along through the tutorial as it goes, there is source code provided with each step, and all the instructions in the tutorial allow you to follow along and end up with the same source code (we hope) as is provided for download. However, it's also nice to be able to just have a central place where the source code for all steps can be found, regardless of which one you're on.

To get the source code for a particular step, simply click the link in the list below to download the corresponding files.

 

 

Questions and Answers

A step of the tutorial mentions that there are these "Back" and "Next" buttons at the bottom of the page. I see "<< Back" and "Next >>" *links*, but no buttons. Do these work? What's up?

The "Back" and "Next" buttons which the question asked about used JavaScript; Chris Maunder (this site's administrator) and I decided to turn the buttons into links because not all browsers support or allow JavaScript. The links work just like the buttons and step you through the tutorial.

Why do you stop at generating the project with AppWizard in Step 1 and make us go on to Step 2?

I do this so if you choose to download the sample source code for Step 1, you get a freshly-started project which hasn't been modified yet, and having just generated the project is a great stopping place if it's late in the evening. Scroll down to the bottom of the article page and click the Next button to move on to Step 2, where we actually begin to modify the source files.

Notes From the Rear

This page is here because DCOM is such a complex and intricate subject that many people have lots of questions, even with the most well-documented tutorial. Also, sometimes it helps to be able to have the ability to answer your questions with figures, charts, diagrams, and screenshots. The message boards, great though they are, just don't provide that functionality. I strongly encourage you to pose questions to the message boards, and I'll either reply to you on the board or put up a "Hey everybody, the answer to this is on the Questions and Answers page!" if a lot of people ask about it.

Each time I've added a new question to this page, Chris Maunder's excellent scripts will mark it with a little 'Updated' graphic, and the home page will let you know it's been updated. Then simply go to this page to see what's new!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
Dr. Brian Hart obtained his Ph.D. in Astrophysics from the University of California, Irvine in 2008. Under Professor David Buote, Dr. Heart researched the structure and evolution of the universe. Dr. Hart is currently employed as a Astrodynamicist / Space Data Scientist with Point Solutions Group in Colorado Springs, CO supporting Space Operations Command, United States Space Force. Dr. Hart is a Veteran of the U.S. Army and the U.S. Navy, having most recently served at Fort George G. Meade, MD as a Naval Officer with a Cyber Warfare Engineer designator. Dr. Hart has previously held positions at Jacobs Engineering supporting Cheyenne Mountain/Space Force supporting tests, with USSPACECOM/J58 supporting operators using predictive AI/ML with Rhombus Power, and with SAIC supporting the Horizon 2 program at STARCOM. Dr. Hart is well known to the community due to his over 150 technical publications and public speaking events. Originally from Minneapolis/Saint Paul, Minnesota, Dr. Hart lives in Colorado Springs with his Black Lab, Bruce, and likes bowling, winter sports, exploring, and swimming. Dr. Hart has a new movie coming out soon, which is a documentary called "Galaxy Clusters: Giants of the Universe," about his outer space research. The movie showcases the Chandra X-ray Observatory, one of NASA’s four great observatories and the world’s most powerful telescopes for detecting X-rays. The movie has been accepted for screening at the USAFA Planetarium and will highlight the need of updating and maintaining X-ray telescopes for scientific advancement.

Comments and Discussions

 
QuestionHow to raise events from DCOM Server Pin
Nguyen Duc Thang4-Jan-04 17:45
Nguyen Duc Thang4-Jan-04 17:45 
AnswerRe: How to raise events from DCOM Server Pin
Brian C Hart5-Jan-04 8:56
professionalBrian C Hart5-Jan-04 8:56 
GeneralBSTR problem Pin
Member 7293491-Dec-03 2:28
Member 7293491-Dec-03 2:28 
GeneralError starting server AGAIN Pin
tkp29-Jul-03 2:39
tkp29-Jul-03 2:39 
GeneralRe: Error starting server AGAIN Pin
Brian C Hart29-Jul-03 7:16
professionalBrian C Hart29-Jul-03 7:16 
GeneralCalling multiple DCOMs Pin
sdaymond21-Jul-03 20:24
sdaymond21-Jul-03 20:24 
GeneralRe: Calling multiple DCOMs Pin
Brian C Hart22-Jul-03 6:29
professionalBrian C Hart22-Jul-03 6:29 
GeneralRe: Calling multiple DCOMs Pin
sdaymond22-Jul-03 8:36
sdaymond22-Jul-03 8:36 
Hello Brian,

Thank you for your comment - that put me in the right direction. There is no doubt I have to create object dynamically in VB. For that purpose VB has CreateObject but with CreateObject you cannot specify remote machine. So there must be some other way.
I have done some search on the Web and found an article that directly appears to solve my problem:
"Creating Remote DCOM Objects on Specific Servers" (http://www.avdf.com/apr98/art_vb005.html)
The author - Jim Karabatsos - provides VB code for a function: CreateRemoteObject (that calls a couple of API functions, including CoCreateInstanceEx):
The code is below.

Best regards,
Stan Daymond
London, UK


Private Type SERVER_STRUCTURE
reserved1 As Long
pServer As Long
AuthInfo As Long
reserved2 As Long
End Type

Private Type MULTI_QI
pIID As Long
pInterface As Object
hResult As Long
End Type

Private Declare Function CLSIDFromProgID Lib "ole32.dll" _
(Progid As Any, Clsid As Any) As Long

Private Declare Function OleInitialize Lib "ole32.dll" _
(ByVal Nullptr As Long) As Long

Private Declare Function CoCreateInstanceEx Lib "ole32.dll" _
(Clsid As Any, ByVal pUnkOuter As Long, _
ByVal Context As Long, Server As SERVER_STRUCTURE, _
ByVal nElems As Long, mqi As MULTI_QI) As Long

Private Declare Function GetComputerName Lib "kernel32" _
Alias "GetComputerNameA" (ByVal lpBuffer As String, _
nSize As Long) As Long

' The trick is to call CoCreateInstanceEx to do the dirty work - and get
' an iDispatch interface pointer in one step. This is very efficient,
' You get the IDISPATCH pointer by passing the 'well-known' REFIID of
' IDISPATCH. Unfortunately, not being able to do this as a constant,
' we can hard-code the REFIID into a little routine.

Public LastError As String

Public Function CreateRemoteObject(ObjectName As String, _
Optional ServerName As String) As Object

Dim clsid(256) As Byte
Dim progid() As Byte
Dim server() As Byte
Dim QI As MULTI_QI
Dim SS As SERVER_STRUCTURE
Dim refiid(16) As Byte
Dim lrc As Long

LastError = ""

' We only need to create the object remotely if the server name is not
' the same as our machine (or if it is empty, allowing this function to
' act as a general replacement for CreateObject)

If (Trim$(ServerName) = "") Or (UCase$(ServerName) = UCase$(GetCompName())) Then

On Error Resume Next

Err = 0

Set CreateRemoteObject = CreateObject(ObjectName)

If Err <> 0 Then
LastError = Err.Description 'record last error
End If

On Error GoTo 0

Exit Function

End If

'otherwise, it is genuinely remote.

GetIIDforIDispatch refiid() 'set an IID for IDispatch
QI.pIID = VarPtr(refiid(0)) 'point to the IID
progid = ObjectName & Chr$(0) 'specify the object to be launched
server = ServerName & Chr$(0) 'specify the server
OleInitialize 0 'initialise OLE
lrc = CLSIDFromProgID(progid(0), clsid(0)) 'get the CLSID for the object

If lrc <> 0 Then
LastError = "Unable to obtain CLSID from progid " & ObjectName & vbCrLf _
& "Possibly it is not registered on both this server and server " & ServerName
Exit Function
End If

' point to server name and
' invoke a remote instance of the desired object

SS.pServer = VarPtr(server(0))
lrc = CoCreateInstanceEx(clsid(0), 0, 16, SS, 1, QI)

If lrc <> 0 Then
LastError = "CoCreateInstanceEx failed with error code " & Hex$(lrc)
Exit Function
End If

Set CreateRemoteObject = QI.pInterface ' pass back object ref.

End Function


Public Sub GetIIDforIDispatch(p() As Byte)

' fills in the well-known IID for IDispatch into the byte array p.

p(1) = 4
p(2) = 2
p(8) = &HC0
p(15) = &H46

End Sub

Function GetCompName() As String

' return the computer name

Dim buf As String
Dim rc As Long

buf = String$(256, 0)
rc = GetComputerName(buf, Len(buf))

If InStr(buf, Chr$(0)) > 1 Then
GetCompName = UCase$(Left$(buf, InStr(buf, Chr$(0)) - 1))
End If

End Function


That's it. Just include this module in your project, then create your remote object using this syntax:

Dim O as MyServer.MyObject ' NB - you can use early binding !!!

Set O = CreateRemoteObject("MyServer.MyObject","MyServer")

CreateRemoteObject is pretty much a drop-in replacement for CreateObject.
GeneralRe: Calling multiple DCOMs Pin
Brian C Hart22-Jul-03 10:48
professionalBrian C Hart22-Jul-03 10:48 
Generalerror starting server Pin
Riyadh Tanveer23-Jun-03 4:00
Riyadh Tanveer23-Jun-03 4:00 
GeneralRe: error starting server Pin
luenii24-Jul-03 7:35
luenii24-Jul-03 7:35 
GeneralInstalling the server Pin
Anonymous9-Jun-03 23:08
Anonymous9-Jun-03 23:08 
GeneralRe: Installing the server Pin
luenii24-Jul-03 7:37
luenii24-Jul-03 7:37 
GeneralRe: Installing the server Pin
Brian C Hart24-Jul-03 11:29
professionalBrian C Hart24-Jul-03 11:29 
GeneralDCOM server and proxy-stub registration Pin
Manoj Chaudhari25-Apr-03 7:26
sussManoj Chaudhari25-Apr-03 7:26 
GeneralRe: DCOM server and proxy-stub registration Pin
awael17-Nov-03 1:11
awael17-Nov-03 1:11 
GeneralRe: DCOM server and proxy-stub registration Pin
Brian C Hart17-Nov-03 6:20
professionalBrian C Hart17-Nov-03 6:20 
GeneralRe: DCOM server and proxy-stub registration Pin
Brian C Hart17-Nov-03 6:19
professionalBrian C Hart17-Nov-03 6:19 
Generalcan't get both dcom.dll and dcom.exe as outputs when i compile my dcom server code Pin
problemcracker17-Dec-02 13:13
problemcracker17-Dec-02 13:13 
GeneralRe: can't get both dcom.dll and dcom.exe as outputs when i compile my dcom server code Pin
Brian C Hart17-Nov-03 6:22
professionalBrian C Hart17-Nov-03 6:22 
GeneralATL client Pin
Vld12-Dec-02 12:20
Vld12-Dec-02 12:20 
GeneralPorting the current client to ActiveX component Pin
dcom_dummy31-Oct-02 8:36
sussdcom_dummy31-Oct-02 8:36 
GeneralRe: Porting the current client to ActiveX component Pin
zilberd8-Nov-02 4:00
zilberd8-Nov-02 4:00 
GeneralProblem in OnAdviseServer Pin
Ivan Marmol25-Oct-02 1:40
Ivan Marmol25-Oct-02 1:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.