我在2个远程服务器上的com应用程序中安装了一个com dll。我已经从其中一台服务器导出了一个非著名代理,并将其安装在客户端计算机上。
我希望能够在运行时在运行时实例化com对象的哪个远程服务器 - 代理安装包含我从中导出的机器的远程服务器名称,因此,只需在客户端上创建代理我将其导出的机器是代理属性的一部分,因为它是代理属性的一部分。
代理未排队,所以我无法使用诸如 queue:ComputerName=Server01/new:ComClass.Class
的路径名调用 GetObject
的方法。
对于将始终调用远程服务器的非流行代理,它们从我只使用CreateObject(objectName)
,它将使用代理属性使用远程服务器名称。
搜索后,我找到了一个解决方案(在下面的自我答复中(,但是在vb6中有一种更简单的方法而无需使用ole32.dll
函数吗?
编辑:测试 @bob77在评论中提出的解决方案后,称为CreateObject
,带有服务器名称参数没有效果。仅使用CreateRemoteObject
方法概述的实际调用指定服务器上的COM组件。
这可能是因为客户端调用来自IIS进程,并且远程服务器COM 应用程序的用户身份不同。
可以使用ole32.dll
库中的CoCreateInstanceEx
函数完成。
首先声明ole32.dll
和相应数据结构所需的功能:
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
i然后使用此功能,该功能输入对象名称和服务器名称并返回对象的实例,这将是所需服务器的代理:
Private Function CreateRemoteObject(ByVal ObjectName As String, _
ByVal ByVal serverName As String) As Object
Dim clsid(256) As Byte
Dim progid() As Byte
Dim server() As Byte
Dim queryInterface As MULTI_QI
Dim serverStructure As SERVER_STRUCTURE
Dim refiid(16) As Byte
Dim longReturnCode As Long
Dim errorString As String
errorString = ""
GetInterfaceIDforIDispatch refiid() ' set an interface ID for IDispatch
queryInterface.pIID = VarPtr(refiid(0)) ' point to the interface ID
progid = ObjectName & Chr$(0) ' specify the object to be launched
server = serverName & Chr$(0) ' specify the server
OleInitialize 0 ' initialise OLE
longReturnCode = CLSIDFromProgID(progid(0), clsid(0)) ' get the CLSID for the object
If longReturnCode <> 0 Then
errorString = "Unable to obtain CLSID from progid " & ObjectName
App.LogEvent errorString, vbLogEventTypeError
Exit Function
End If
' point to server name and invoke a remote instance of the desired object
serverStructure.pServer = VarPtr(server(0))
longReturnCode = CoCreateInstanceEx(clsid(0), 0, 16, serverStructure, 1, queryInterface)
If longReturnCode <> 0 Then
errorString = "CoCreateInstanceEx failed with error code " & Hex$(longReturnCode)
App.LogEvent errorString, vbLogEventTypeError
Exit Function
End If
' Pass back object ref
Set CreateRemoteObject = queryInterface.pInterface
End Function
Private Sub GetInterfaceIDforIDispatch(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