?
Solved

Calling COM interface without type library

Posted on 2003-03-27
5
Medium Priority
?
418 Views
Last Modified: 2008-02-01
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.

Regards

Ryan
0
Comment
Question by:Waylander1970
[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
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 8218006
>>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):
http://www.domaindlx.com/e_morcillo/scripts/type/default.asp

This article also discusses the creation of type libraries, not directly from a C++ interface, but the information should be useful:
http://www.vbbyjc.com/articles/vbtoidl.htm
0
 

Author Comment

by:Waylander1970
ID: 8219531
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?

Ryan
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 290 total points
ID: 8219678
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/midl/midl/using_the_midl_compiler_2.asp
0
 

Author Comment

by:Waylander1970
ID: 8226658
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.
0
 

Author Comment

by:Waylander1970
ID: 8233494
Although accurate and very knowledgable, I was after an example.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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