?
Solved

CreateObject&COM+

Posted on 2003-03-17
20
Medium Priority
?
892 Views
Last Modified: 2012-05-04
hi,
i'm using VB6.0,COM+,SQL2k,WinAdvSrv SP3.
Previously i was creating all my DLL's with the NEW Keyword,it was working fine but when i came to change my COM server to another machine i got the error in loading DLL,i have read that New is not creating the objects through the SCM that's why i may face a frustricated error.
then i my way of creating the Configured Components by using the CreateObject("MyCls.class")but here i started getting an error which was not available by using the New Keyword here is the error:
Run-Time error '-2147023170' Automation error
The Remote Procedure Call failed.
by searching in Microsoft article they said to install win2k SP3,which is already installed in my machine.
shall i go back to New!!!???
rgrds
Meer.
0
Comment
Question by:mirghani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 3
20 Comments
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8151431
No. It si true: you cannot use new with com+, you have to use createobject.
Reason why it failed (I will ook first for the second one):
1) you installed something that overrided the sp3
2) you did not set adeguate priviledges (your com+ components need to run as Administrator).
Look in control panel->Administrative tools->Componet services->my Comuter(right clicking here and look for properties will let you set somethinmg about security)->Com+ components->yourcomponent(right click and look properties)
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8151437
Remember also you cannot use secondary interfaces...
0
 

Author Comment

by:mirghani
ID: 8151582
hi Cimperiali,
>>you installed something that overrided the sp3
my system shows that SP3 is installed in my pc.

i adhusted the previlages and that reuires me to Reboot my machine,i'm administrator in my PC and the Owner beyond that i added my account but still i'm getting the error.
Meer.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:fluglash
ID: 8151812
do you get this error not on your development machine??
0
 

Author Comment

by:mirghani
ID: 8152461
first i got this error at my Dev Machine, then i installed the Application Proxy into another Client,and i'm getting the same error.
something more could help experts:
the first time i installed win2k SP3 it was from the internet(express installation),then i installed it again the full package from my another source,does this make any sense?
Meer.
0
 

Author Comment

by:mirghani
ID: 8152613
here is my framework:
The Client Create Obj1, Obj1 Create Obj2, Obj2 Create Obj3..then Obj3 execute SQL Sp and Retrieve RecordSet ByRef.
ActiveX Dll Properties:
MultiUse
NotAnMTSObject
Meer.
0
 

Author Comment

by:mirghani
ID: 8152664
here is the way i'm creating all Objects:
Dim MyCls as Object
Set MyCls=CreateObject("Foreign.cForeign")
and also there is a Refrence in my DLL Project for Foreign.cForeign.
how if i use this way:
Dim MyCls as Foreign.cForeign (adding Refrence in the Dll Project)
Set MyCls=CreateObject("Foreign.cForeign")

Meer.
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8152735
Yes, you can...
CreateObject does not means you do not have to use early binding.
You can add references in projects, and declare variable as ...yourObiectClass.
Then you can instantiate using the createobject function.
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8152836
can you show the code in your Class_Initialize sub&
0
 

Author Comment

by:mirghani
ID: 8153396
i changed the design in all my Dll's from Late Binding Dim MyCls As Object to early Binding Dim MyCls As Foreign.cForeign then i recompile my DLL(Binary Compatability)now the Automation message is gone but appears new message:
Run-time error '3704'
Operation is not allowed when the Object is Closed.
in my client project i perform RS.RecordCount and the recordSet Retrieved is Empty!!!!!.
Meer.
0
 

Author Comment

by:mirghani
ID: 8153482
here is my obj1 code:
Dim AttachedParameters() As PrjDataAccess.paraList

Private Const mVarChar = adVarChar
Private Const mInt = adInteger
Private Const mTinyInt = adTinyInt
Private Const mSmallInt = adSmallInt
Private Const mDateTime = adDBTimeStamp
Private Const mChar = adChar
Private Const mMoney = adCurrency
Private Const mGUID = adGUID
'Select Data

Dim obj_GMBO As GMBO.cGMBO 'Global Methods Business Object

Dim Count As Integer

Dim Meer_RS As ADODB.Recordset
Public Function GetLookupData(BO_RS As ADODB.Recordset, ByVal LookUpID As Integer) As Boolean


Count = 1

GetLookupData = False

' Execute stored procedure and return recordset
Set obj_GMBO = CreateObject("GMBO.cGMBO")
AssignParameters Count, LookUpID, mInt, 4, adParamInput
    If obj_GMBO.GetLookupData(Meer_RS, AttachedParameters()) Then
     Set BO_RS = Meer_RS
     
     GetLookupData = True
    End If
Release_Object obj_GMBO
End Function
Private Sub AssignParameters(ByRef Counter As Integer, ByVal VarValue As Variant, _
                            ByVal DataType As Variant, ByVal Size As Integer, ByVal Direction As Integer)
ReDim Preserve AttachedParameters(Counter)
AttachedParameters()(Counter).VarValue = VarValue
AttachedParameters()(Counter).DataType = DataType
AttachedParameters()(Counter).Size = Size
AttachedParameters()(Counter).Direction = Direction
Counter = Counter + 1
End Sub
*******************************
Class GMBO Code:
Private Const scGET_LOOKUP = "Get_Lookup"
Dim obj_Da As PrjDataAccess.cDataAccess

Public Function GetLookupData(BO_RS As ADODB.Recordset, ByRef AttachedParameters() As paraList) As Boolean
Dim DO_RS As ADODB.Recordset

GetLookupData = False

' Execute stored procedure and return recordset
'Set obj_Da = CreateObject("PrjDataAccess.cDataAccess")
Set obj_Da = CreateObject("PrjDataAccess.cDataAccess")
    If obj_Da.ExecuteSp(DO_RS, scGET_LOOKUP, Remote_Conn, AttachedParameters()) Then
     Set BO_RS = DO_RS
     Set DO_RS = Nothing
     GetLookupData = True
    End If
Set obj_Da = Nothing
End Function

here comes Obj3 Code(DataAccess Component):
Option Explicit
Implements ObjectControl
Implements IObjectConstruct

Public Type paraList
  VarValue As Variant
  DataType As Variant
  Size As Integer
  Direction As Integer
End Type

Public Enum QUERYOPTIONS
    QO_UseTable = ADODB.adCmdTable
    QO_UseText = ADODB.adCmdText
    QO_UseStoredProc = ADODB.adCmdStoredProc
    QO_UseUnknown = ADODB.adCmdUnknown
    QO_UseDefault = -1
End Enum

Public Enum LOCKTYPES
    LT_ReadOnly = ADODB.adLockReadOnly
    LT_Optimistic = ADODB.adLockOptimistic
    LT_Pessimistic = ADODB.adLockPessimistic
    LT_BatchOptimistic = ADODB.adLockBatchOptimistic
End Enum

Public Enum CURSORLOCATION
    CURL_Client = ADODB.adUseClient
    CURL_Server = ADODB.adUseServer
End Enum

Public Enum CURSORTYPE
    CURT_OpenKeySet = ADODB.adOpenKeyset
    CURT_OpenForwardOnly = ADODB.adOpenForwardOnly
End Enum

Public Enum E_ConnType
     Local_Conn = 0
     Remote_Conn = 1
End Enum

Private Const OBJNAME = "DataAccess Component"
Private Cnn As ADODB.Connection
Private Cmd As ADODB.Command
Private Outernalconnstr As String

'Connection String that Contains the Lookups Data Source
'Private Const Internalconnstr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SBOS EH;Data Source=SBOS_SERVER"
Private Const Internalconnstr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SBOS EH;Data Source=SBOS_SERVER"

Private Sub IObjectConstruct_Construct(ByVal pCtorObj As Object)
Dim mConstructString As IObjectConstructString
Set mConstructString = pCtorObj
Outernalconnstr = mConstructString.ConstructString
End Sub

'=======================================================================
'Synopsis:              This function closes a previously opened connection
'Function input:        none
'Function output:       Returns TRUE if the function succeeds
'=======================================================================
Public Function CloseConnection() As Boolean
    If Not Cnn Is Nothing Then
'        Cnn.Close
        Set Cnn = Nothing
    End If
End Function

'=======================================================================
'Synopsis:              This function creates and opens a new connection
'Function input:        Connect string
'Function output:       Returns TRUE if the function succeeds
'=======================================================================
Public Function OpenConnection(ByVal Connect As String) As Boolean
Dim lErrNo      As Long
Dim sErrDesc    As String

    'assume failure
    OpenConnection = False
   
    'enable error handler
    On Error GoTo ErrorHandler
   
    'establish the transaction if Connection String specified
    If Connect <> vbNullString Then
        CloseConnection
        Set Cnn = CreateObject("ADODB.Connection")
        Cnn.Open Connect
        'MsgBox "Connected"
    End If
   
    OpenConnection = True
    Exit Function
   
ErrorHandler:

    'store incoming values and raise error
    lErrNo = Err.Number
    sErrDesc = Err.Description
    On Error GoTo 0
    Err.Raise lErrNo, OBJNAME, sErrDesc

End Function

'Private Sub ObjectControl_Activate()
''Nothing Occur here
'End Sub

'This Function Executes a StoredProcedure and Return RS ByRefrence and Boolean
'if Done Successfully.

Public Function ExecuteSp(ByRef rs As ADODB.Recordset, ByVal spName As String, ByVal ConnType As Integer, ByRef AttachedParameters() As paraList) As Boolean
Dim i As Long
Dim lErrNo      As Long
Dim sErrDesc    As String

On Error GoTo ErrorHandler
   ' If RS Is Nothing Then
        Set rs = CreateObject("ADODB.Recordset")
   ' End If
 
    Set Cmd = CreateObject("ADODB.Command")

    If rs.State = adStateOpen Then
        rs.Close
    End If
With rs
    .CURSORTYPE = CURT_OpenForwardOnly
    .LockType = LT_ReadOnly
    .CURSORLOCATION = CURL_Client
End With
     
        ExecuteSp = True
       
 
    'Is used for DataBase Connection String
    If ConnType = Remote_Conn Then
      OpenConnection (Outernalconnstr)
    Else
    'Is used for Cashing Connection String
      OpenConnection (Internalconnstr)
    End If
     Cmd.CommandTimeout = 3600
     Cmd.ActiveConnection = Cnn
     Cmd.CommandType = QO_UseStoredProc
     Cmd.CommandText = Trim(spName)
        If (UBound(AttachedParameters) > 0) Then
           'Cmd.Parameters.Refresh
            For i = 1 To UBound(AttachedParameters)
                If AttachedParameters(i).VarValue = "" Then
                    Cmd.Parameters(i).Value = Null
                Else
                    Cmd.Parameters(i).Type = AttachedParameters(i).DataType
                    Cmd.Parameters(i).Direction = AttachedParameters(i).Direction
                    Cmd.Parameters(i).Size = AttachedParameters(i).Size
                    Cmd.Parameters(i).Value = AttachedParameters(i).VarValue
                End If
            Next i
        End If
        ReDim AttachedParameters(0)
     rs.Open Cmd
     
     Exit Function
ErrorHandler:
    'store incoming values
    lErrNo = Err.Number
    sErrDesc = Err.Description
   
    'close down what we can and raise error
    On Error Resume Next
        Set rs = Nothing
    On Error GoTo 0
        ExecuteSp = False
        Set rs.ActiveConnection = Nothing
        Err.Raise lErrNo, OBJNAME, sErrDesc
End Function

Private Sub ObjectControl_Activate()

End Sub

Private Sub ObjectControl_Deactivate()
    'clean up any outstanding connections
    CloseConnection
    Set Cmd = Nothing
End Sub

Private Function ObjectControl_CanBePooled() As Boolean
    ObjectControl_CanBePooled = False
End Function
*************************************
the Client Code:
Dim MyCls as Obj1.cObj1
Set MyCls=CreateObject("Obj1.cObj1")
Set Result=Obj1.GetLookupData(RS,12)

that's all about my Project.
rgrds
Meer.
0
 

Author Comment

by:mirghani
ID: 8153500
obj2 contains a refrence for obj3(DataAccess).
Obj1 contains a refrence for Obj2(GMBO)+Obj3 so as to use the ParaList DataType.
Client the have a refrence for Obj1.

i hope that will help.
Meer.
0
 
LVL 5

Accepted Solution

by:
Cimperiali earned 400 total points
ID: 8153838
Now, when you want to pass recordset through objetcs, you should use disconnected recordsets (that is: you must create a clientside recordset, populate, set connection to nothing while leaving it opened (=simply, do not close it), and you should pass them byVal

'---------------------------------
From msdn:
'------------------------
Out Of Process
When you pass an ADO recordset out of process, the ADO libraries are loaded in both processes. The ADO libraries pass the information stored in the recordset from the one process to the other. Each process now has its own copy of the recordset object rather than a reference to the initial object.

The bookmark, filter, and sort information are not used in determining what records to pass across process boundaries. All rows of the recordset are passed, regardless of the current setting of the Filter property. In the resulting recordset, the first record is the current record regardless of the bookmark set in the other process. The resulting recordset does not receive the value of the Sort property from the other process.

The MarshalOptions property can be used to control whether the whole Recordset or only the modified rows are marshalled.

If you pass the variable ByRef, the data for the recordset is passed back to the calling procedure. This can cause an unnecessary performance hit and should be avoided whenever possible. Unless you want the changes made within the procedure to be returned to the calling procedure, do not pass the recordset ByRef. Keep in mind that if you set MarshalOptions to adMarshalModifiedOnly and pass the recordset ByRef, that at the end of the call to the server the client has a recordset that contains only the modified rows.

If you pass the variable ByVal, then the recordset is not returned from the procedure. This means that your recordset remains in the same state as it was before the call to the procedure. The modified rows are still marked as having been modified.

You can only pass an ADO recordset out of process if it's CursorLocation is adUseClient. You should also set the recordset's ActiveConnection property to Nothing before passing it out of process.

'-------------------------------------
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset

' Create instance of connection object and then open the
' connection.
Set Conn = New ADODB.Connection
Conn.Open "DSN=SQLServer", "sa", ""

' Create instance of recordset object and open the
' recordset object against a table.
Set Rs = New ADODB.Recordset

' Setting the cursor location to client side is important
' to get a disconnected recordset.
Rs.CursorLocation = adUseClient
Rs.Open "Select * from Table1", _
         Conn, _
         ADODB.adOpenForwardOnly, _
         ADODB.adLockBatchOptimistic

' Disconnect the recordset.
Set Rs.ActiveConnection = Nothing

' Get the value of one of the fields from the recordset
' after disconnection.
Dim v
v = Rs.Fields(0).Value
MsgBox v

Conn.Close

' Get the value of one of the fields from the recordset
' after closing the connection to ensure that you have a
' disconnected recordset.
v = Rs.Fields(0).Value
MsgBox (v)

' Now edit the value and save it.
Rs.Fields("au_lname").Value = "NewValue"

' Now reopen the connection and attach it to the recordset.  Update
Set Conn = New ADODB.Connection
Conn.Open "DSN=DBSql", "sa", ""
Rs.ActiveConnection = Conn
Rs.UpdateBatch

Rs.Close
Conn.Close
Set Rs = Nothing
Set Conn = Nothing
0
 
LVL 7

Expert Comment

by:fluglash
ID: 8153887
as i understand your function GetLookUpData must return recordset. is it right?
0
 

Author Comment

by:mirghani
ID: 8154012
yep it return RecordSet ByRef to the Client.
but there is something strange:
if i use the components which are created by New and Debug my DataAccess exactly at the Line:
Rs.Open i find that the SQL SP execute and return RecordSet successfully, and if i use my components created by CreateObject and do the same Debugging i find that after executing the Line:
Rs.Open that the SP didn't return any RecordSet and by using the Watch tool in VB most of the RecordSet values are "Operation is not allowed....."
!!!!!
0
 

Author Comment

by:mirghani
ID: 8154069
To Cimperiali:
Is there any way rather than returning RecordSet ByVal??? we r working as a team and our company restriction that our function should return True/False.
Meer.
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8157643
>our function should return True/False
all right...then, if it is byref, I am afraid you should not set to nothing the component as long as you need the recordset...(I may be wrong here, as I did not tried. Make a try to wait to set to naothing the component to see if recordset is still alive...And of course, do not close it inside the component...)
0
 

Author Comment

by:mirghani
ID: 8158081
hi,
Now CreateObject works Fine after i Debug my code and found the error at the AssignParameters Sub,i corrected it.

To Cimperiali:
As u see the Client passes me an empty RecordSet and my DataAccess Fill this RS with Records and return to the Client ByRef.
How if i want to avoid ByRef,if i change it to ByVal the recordset appears an empty,do u have any Idea?
Meer.
0
 
LVL 5

Expert Comment

by:Cimperiali
ID: 8158260
Yes: byval means you pass a copy of recordset, not the original one to the function. Thus function works with a local copy, and no matter if you fill the local copy: it will not return to the original calling....
Make a try to see what happens if you pass byref and then destroy the called object...
0
 

Author Comment

by:mirghani
ID: 8158781
i changed all my methods in all my Tiers to ByVal but unfortunately the Client catch an Empty RecordSet!!!
Meer.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question