Solved

Using a DLL in VB

Posted on 2007-03-22
11
2,429 Views
Last Modified: 2013-11-27
I am new to using DLLs in Vb and am having a problem accessing a DLL function.  I have a DLL with the following IDL file:

//**********************************
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: prxySayHi.DLL
[  uuid(1D5A64A8-6705-4156-B4B6-0354E3A96A3F),  version(1.0)]
library prxySayHi
{
    // TLib :     // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("stdole2.tlb");
    // Forward declare all types defined in this typelib
    dispinterface _intfcSayHi;
    [
      uuid(2D02AD08-4CB8-4857-8CAB-85828BFC8CFD),
      helpstring("Generated on Tuesday, March 20, 2007 03:43:39 PM")
    ]
    dispinterface _intfcSayHi {
        properties:
        methods:
            [id(0x00000001)]
            long methdSayHi(
                            [in, out,
                custom({F0D491D0-0DF9-4ECA-AFB0-5C573ABE9325}, "100")            
           
] BSTR* vDataBuf,
                            [out,
                custom({F0D491D0-0DF9-4ECA-AFB0-5C573ABE9325}, "0")            
           
] long* tpurcode);
    };
    [
      uuid(2E2E8AFE-DEBD-4C51-952C-105C89C98627),
      appobject
    ]
    coclass intfcSayHi {
        [default] dispinterface _intfcSayHi;
    };
};
//**********************************
The purpose of the DLL is just to pass a "Hello" message back to the calling program.

I am using VB 2005 Express to write the calling program.  I have creaded a button to execute the call and a text field to display the "Hello" response.
When I view the dll in the object browser, I see two options available: prxySayHi.intfcSayHi and prxySayHi._intfcSayHi both showing my methdSayHi with appropriate variables.

If i call it using  prxySayHi.intfcSayHi I get the following error:
'Unable to cast COM object of type 'prxySayHi.intfcSayHiClass' to interface type 'prxySayHi._intfcSayHi'. This operation failed because the QueryInterface call on the COM component for the interface with IID  "
This is the code I'm using:
'**********************-Start-***************************
Public Class GetHi
    Private Sub cmdGetHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetHello.Click
        Dim intRetVal As Integer
        Dim strDataBuf As String
        Dim intUrCode As Int32
        Dim objCSayHi As prxySayHi.intfcSayHi
        objCSayHi = New prxySayHi.intfcSayHi
        strDataBuf = "Hi"
        intRetVal = objCSayHi.methdSayHi(strDataBuf, intUrCode)
        Me.txtHelloMsg.Text = strDataBuf
    End Sub
End Class
'**********************-End-***************************

If I try to use the interface type 'prxySayHi._intfcSayHi' usning the following code I get other errors.

'**********************-Start-***************************
Public Class GetHi
    Private Sub cmdGetHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetHello.Click
        Dim intRetVal As Integer
        Dim strDataBuf As String
        Dim intUrCode As Int32
        Dim objISayHi As prxySayHi._intfcSayHi
        objISayHi = New prxySayHi._intfcSayHi
        strDataBuf = "Hi"
        intRetVal = objISayHi.methdSayHi(strDataBuf, intUrCode)
        Me.txtHelloMsg.Text = strDataBuf
    End Sub
End Class
'**********************-End-***************************
With this code on the 'objISayHi = New prxySayHi._intfcSayHi" line I get an error : "'New' cannot be used on an interface.      "
If I remove this line I get the following warning: "Variable 'objISayHi' is used before it has been assigned a value. A null reference exception could result at runtime."
If I ignore the warning and run the app I get - amazingly - a "NullReferenceException was unhandled" error.

I know I must be donig something stupid but I'm missing it.  Any suggestions???

Thanks,  Diane

0
Comment
Question by:diane903
11 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 18772491
Did you make the dll in VB.NET using ComInterop?  (a "tlb" is a TypeLibrary used with COM - VB6, etc) If you are making the dll in VB.NET to work with a .NET application, then you don't have to use ComInterop, so uncheck it in the Project Properties, if you have it checked.  That way it will only create a "dll", which you will be able to add as a reference and instantiate using the "New" keyword.

Hope that helps.
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18772522
Just another comment:  Although you can create a dll/tlb in .NET using ComInterop for backwards compatibility with VB6, etc., you cannot use this dll with a .NET application, which may be why you are getting the error.  If you create the dll without using ComInterop, then it should work fine with your .NET application.

0
 

Author Comment

by:diane903
ID: 18772644
The dll is created by a proprietary development environment that provides linkage to an application my company wants to use.  We have no visibility into the dll other than the idl file.  We can specify the method parameters and return values but other than that we have no control over the coding of the dll.  It's all done internal to their environment and includes code that links into their proprietary appplication.  Regardless, I should be able to get to the DLL and I'm not even getting to that point.
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18772647
Just one more comment:  When you create your dll to work with .NET, do not add a "Com Class", but just add a regular "Class".

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 18772705
Ok.  I guess I don't know then.  Sorry.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:diane903
ID: 18795450
Please close this.  I found a solution myself
0
 
LVL 1

Expert Comment

by:zgilboa
ID: 19657336
The better and more intuitive way for using a COM dll is via VC++ or VB 6.

In addition, all errors you have received have something to do with the COM interface not being able to create the class object (QueryInterface is a method that has to be implements whenever an IUnknown object is implemented, which is the case for all COM objects).

Possible reasons include the following:

1. Incorrect settings in your IDE / incorrect project settings.
2. Internal problem with the COM dll.
3. Registry problem (the COM dll is fine but is not properly registered)
4. Dependency problem - the COM dll requires some intermediate libraries which are not available (or are not up-to-date) on the target machine  (all dlls created b y VB6, for example, require that the target machine has an updated version of msvbvm60.dll)
0
 
LVL 1

Expert Comment

by:wish756
ID: 19882491
diane903,

Could you please post the solution too for the benefit of others looking at this discussion.

Thank You.
0
 

Author Comment

by:diane903
ID: 19892041
The solution is as follows:
1. make sure the dll is registered.  OLE View is a good check.
2. Add the dll as a reference.

Following is similar to the code I used for the button click.   The ouput "Hello" comes from the dll in vDataBuf and is displayed  in a text box txtHi.  The tpurcode is return code used by the program that generates the dll.

        Dim tpurcode As Integer
        Dim objGetData As Object
        Dim strHi As String = ""

        Try
            objGetData = CreateObject("prxySayHi.intfcSayHi.1")
            Call objGetData.methdSayHi(strHi, tpurcode)
        Catch ex As Exception When ex.Message = "Cannot create ActiveX component."
            strHi = "DLL Failure"
        Catch ex As Exception
            MsgBox("Err: " & ex.Message)
        End Try
        If Len(strHi) > 1 Then
            Me.txtHi.Text = strHi
        Else
            Me.txtHi.Text = "Error!"
        End If
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 20633787
PAQed with points refunded (250)

Computer101
EE Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now