• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

Calling COM interface without type library

Hi all,

I have a COM interface (the Still Image Aquisition library in sti.dll) that I need to call from VB.

My test code so far is;

Private Const STI_VERSION = &H2

Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" _
    (ByVal lpModuleName As String) As Long

Private Declare Function StiCreateInstance Lib "sti.dll" Alias "StiCreateInstanceA" _
    (ByVal hInst As Long, _
     ByVal dwVer As Long, _
     ByVal ppSti As Long, _
     ByVal punkOuter As Long _
    ) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (lpvDest As Any, _
   lpvSource As Any, _
   ByVal cbCopy As Long)

Private Sub Command2_Click()
    Dim lRet As Long
    Dim lProcessHandle As Long
    Dim dwVer As Long
    Dim oTemp As Object
    Dim ppSti As Object
    Dim lPointerToInterface As Long
    Dim pwszDeviceName As String ' LPWSTR
    Dim pdwEventCode As Long ' DWORD
    Dim pwszEventName As String ' LPWSTR

    ' The following is required for design time.
    lProcessHandle = GetModuleHandle("vb6.exe" & Chr(0))
    If (lProcessHandle = 0) Then
        ' Must be in run time.
        lProcessHandle = GetModuleHandle(App.EXEName & ".exe" & Chr(0))
    End If
    dwVer = STI_VERSION

    lRet = StiCreateInstance(lProcessHandle, dwVer, VarPtr(lPointerToInterface), 0&)

    If (lRet = 0) Then
        ' Turn the pointer into an illegal, uncounted interface
        CopyMemory oTemp, (lPointerToInterface), 4

        ' Call AddRef method, so that ppSti becomes a legal reference.
        Set ppSti = oTemp

        ' Destroy the illegal reference by setting it to Nothing without calling Release.
        CopyMemory oTemp, 0&, 4

        ' We now have an STI object available to call methods on.
        pwszDeviceName = String(1024, Chr(0))
        pwszEventName = String(1024, Chr(0))
        pdwEventCode = 0
        lRet = ppSti.GetSTILaunchInformation(pwszDeviceName, pdwEventCode, pwszEventName)
    End If

End Sub

The first CopyMemory call fails, I am not passing a correct pointer somehow. The StiCreateInstance call required a pointer to a pointer where it would store the COM interface pointer. This call succeeds and I am trying to make use of that pointer by converting it into a generic object.

I am nearly there with this, but get a problem when copying pointers. I think that there may be more problems ahead because the functions are not strongly prototyped.

Is this the way to go or are there better ways of accessing this interface? For example, should I attempt to translate the C++ interface to a VB type library (never done this before) and use that? As far as I can see, no one else has a type library for this.

Hope someone can help.


  • 3
  • 2
1 Solution
>>should I attempt to translate the C++ interface to a VB type library

Yes.  You can get good references on how this might be done by looking at the type library done here (translation of over 100 interfaces):

This article also discusses the creation of type libraries, not directly from a C++ interface, but the information should be useful:
Waylander1970Author Commented:
Thanks Azra,

I'm familiar with Eduardo's work, but I cannot see any help on his page on how to actually do it.

With the other link, it redirects to a missing page.

Do you have any more specific examples or alternative links?

I used to have several links talking about IDL/ODL and creating type libraries from scratch when I was into this sort of thing about a year ago.  Unfortunately, time did not permit me to learn as much as I would have liked.  However, what I did find, was that it was fairly straightforward and I would often look at Eduardo's type library, to see how he converted a particular interface to get pointers on some specific problems I had, be it how to declare a particular function, how its parameters should be implemented, etc.

The link that doesn't work really talks about creating "proper" type libraries from a VB COM dll.  Its at that site vbbyjc.com, under his VB6 Projects and Articles section.  The article is entitled "Creating Type Libraries from VB Code in 4 Easy Steps".  Though its geared towards going in somewhat of an opposite direction to what you are looking, it does provide some potentially helpful information.

I did a quick search at Microsoft for "IDL ODL" and it appears they still have a number of links to articles to teach you about it.  This is one that may get you started:
Waylander1970Author Commented:
You have been very helpful, although I was rather hoping for a small example here. Does anyone else have any examples of this?

I'm willing to close this and award to Azra (for his politeness and helpful attitude more than the answer I needed) unless someone else would like to contribute.
Waylander1970Author Commented:
Although accurate and very knowledgable, I was after an example.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now