将COM服务导入VB.net

时间:2019-05-09 19:35:33

标签: vb.net

我有一个COM服务接口,可以提升程序的运行速度,使其可以实时运行。我在C ++中有调用此服务的代码并且可以工作。我现在正在尝试在VB.net中实现类似的功能

这是有效的C ++代码:

bool SetPriority()
{
HRESULT                    hr(S_OK);
CString                    errStr;
CComPtr<IIncreasePriority> pPriorityService;

  hr = pPriorityService.CoCreateInstance(__uuidof(IncreasePriority));
  if (FAILED(hr))
  {
    errStr.Format(_T("IIncreasePriority.CoCreateInstance failed %X"), hr);
    OutputDebugString(errStr);
    MessageBox(NULL, errStr, _T("Error"), MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
    goto Exit;
  }

  hr = pPriorityService->SetPriority(GetCurrentProcessId(), REALTIME_PRIORITY_CLASS);
  if (FAILED(hr))
  {
    errStr.Format(_T("pPriorityService->SetPriority failed %d"), hr);
    OutputDebugString(errStr);
    MessageBox(NULL, errStr, _T("Error"), MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
    goto Exit;
  }

Exit:
  return SUCCEEDED(hr);
}

这是我到目前为止在VB.net中拥有的

Public Const REALTIME_PRIORITY_CLASS As Long = &H100&
Public Const CLSCTX_INPROC_SERVER As Integer = 1
Public Const CLSCTX_INPROC_HANDLER As Integer = 2
Public Const CLSCTX_LOCAL_SERVER As Integer = 4
Public Const CLSCTX_REMOTE_SERVER As Integer = 16

Public Const CLSCTX_ALL As Integer = CLSCTX_INPROC_SERVER + CLSCTX_INPROC_HANDLER + CLSCTX_LOCAL_SERVER + CLSCTX_REMOTE_SERVER

Declare Function GetCurrentProcessId Lib "Kernel32.dll" () As Long
Declare Function CoCreateInstance Lib "Ole32.dll" (ByRef rclsid As Guid, ByVal pUnkOuter As Long, ByVal dwClsContext As Long, ByRef riid As Guid, ByRef ppv As Object) As Long


Private Sub frmMenuBar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim pPriorityService As IIncreasePriority
  Dim CLASS_IncreasePriority As Guid = New Guid("1E540BCE-C431-11E5-81F6-002215AB8805")
  Dim IID_IIncreasePriority As Guid = New Guid("13E6855E-C431-11E5-81F6-002215AB8805")

  On Error GoTo frmMenuBar_Load_Exit

  ' Set the Priority of the main process/thread
  CoCreateInstance(CLASS_IncreasePriority, 0, CLSCTX_ALL, IID_IIncreasePriority, pPriorityService)
  pPriorityService.SetPriority(GetCurrentProcessId(), REALTIME_PRIORITY_CLASS)

frmMenuBar_Load_Exit:
End Sub

虽然编译,但在我调用CoCreateInstance时失败。我真的不知道下一步该怎么做。

1 个答案:

答案 0 :(得分:2)

尝试使用编组属性装饰函数,如下所示:

Declare Function CoCreateInstance Lib "Ole32.dll" (
        <[In], MarshalAs(UnmanagedType.LPStruct)>
        ByRef rclsid As Guid,
        <MarshalAs(UnmanagedType.IUnknown)>
        ByVal pUnkOuter As Object,
        ByVal dwClsContext As UInteger,
         <[In], MarshalAs(UnmanagedType.LPStruct)>
        ByRef riid As Guid,
        <MarshalAs(UnmanagedType.[Interface])>
        ByRef ppv As Object) As UInt32

或者,您可以使用DllImportAttribute;这里用CoCreateInstanceEx(具有几种参数类型)进行了演示:

    <StructLayout(LayoutKind.Sequential)>
    Friend Structure MULTI_QI
        <MarshalAs(UnmanagedType.LPStruct)>
        Public pIID As Guid
        <MarshalAs(UnmanagedType.[Interface])>
        Public pItf As Object
        Public hr As Integer
    End Structure

    <Flags>
    Friend Enum CLSCTX As UInteger
        CLSCTX_INPROC_SERVER = &H1
        CLSCTX_INPROC_HANDLER = &H2
        CLSCTX_LOCAL_SERVER = &H4
        CLSCTX_INPROC_SERVER16 = &H8
        CLSCTX_REMOTE_SERVER = &H10
        CLSCTX_INPROC_HANDLER16 = &H20
        CLSCTX_RESERVED1 = &H40
        CLSCTX_RESERVED2 = &H80
        CLSCTX_RESERVED3 = &H100
        CLSCTX_RESERVED4 = &H200
        CLSCTX_NO_CODE_DOWNLOAD = &H400
        CLSCTX_RESERVED5 = &H800
        CLSCTX_NO_CUSTOM_MARSHAL = &H1000
        CLSCTX_ENABLE_CODE_DOWNLOAD = &H2000
        CLSCTX_NO_FAILURE_LOG = &H4000
        CLSCTX_DISABLE_AAA = &H8000
        CLSCTX_ENABLE_AAA = &H10000
        CLSCTX_FROM_DEFAULT_CONTEXT = &H20000
        CLSCTX_ACTIVATE_32_BIT_SERVER = &H40000
        CLSCTX_ACTIVATE_64_BIT_SERVER = &H80000
        CLSCTX_INPROC = CLSCTX_INPROC_SERVER Or CLSCTX_INPROC_HANDLER
        CLSCTX_SERVER = CLSCTX_INPROC_SERVER Or CLSCTX_LOCAL_SERVER Or CLSCTX_REMOTE_SERVER
        CLSCTX_ALL = CLSCTX_SERVER Or CLSCTX_INPROC_HANDLER
    End Enum

Public Module NativeMethods
    <DllImport("ole32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True, PreserveSig:=False)>
    Private Sub CoCreateInstanceEx(
        <[In], MarshalAs(UnmanagedType.LPStruct)> ByVal rclsid As Guid,
        <MarshalAs(UnmanagedType.IUnknown)> ByVal pUnkOuter As Object,
        ByVal dwClsCtx As CLSCTX,
        ByVal pServerInfo As IntPtr,
        ByVal cmq As UInteger,
        <[In], Out> ByVal pResults As MULTI_QI())
    End Sub
End Module