Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unmanaged calls to managed code

Status
Not open for further replies.

calderon

Technical User
Jan 18, 2010
9
ES
Hi All,

I have a driver for external hardware that is in unmanaged code and I want to wrote a wrapper for this driver. Programs that use this driver will call the wrapper instead of the original driver. The wrapper will pass these calls to the original driver. Call to the driver are made by using the Declare statement. My problem is that I don't know how to expose my methods so that that can be called in the same way as the methods of the original driver e.g. by using the Declare statement.
Hope that anyone can help me with this one. Thanks.
 
It really all depends but basically you would declare your object or declare it WithEvents.

Code:
Public Class clIM
    Private aDllObject As aDll

    Public Sub New()
        Initilizecompenets()
    End Sub

    Private Sub Initilizecompenets()
        aDllObject = New aDll

        AddHandler aDllObject.OnDoSomething, AddressOf aDllObject_OnDoSomething
        AddHandler aDllObject.OnDoThisThing, AddressOf aDllObject_OnDoThisThing
    End Sub

    Public Property SetAProperty() As String
        Get
            Return aDllObject.aProperty
        End Get
        Set(ByVal value As String)
            aDllObject.aProperty = value
        End Set
    End Property

    Private Sub aDllObject_OnDoSomething(ByVal sender As Object, ByVal e As EventArgs)
        'Do something

        RaiseEvent ThisOnDoSomething(sender, e)
    End Sub

    Private Sub aDllObject_OnDoThisThing()
        'Do this thing

        RaiseEvent ThisOnDoThisThing()
    End Sub

    Public Event ThisOnDoSomething(ByVal sender As Object, ByVal e As EventArgs)
    Public Event ThisOnDoThisThing()
End Class


Public Class clIM2
    Private WithEvents aDllObject As aDll

    Public Sub New()
        Initilizecompenets()
    End Sub

    Private Sub Initilizecompenets()
        aDllObject = New aDll
    End Sub

    Public Property SetAProperty() As String
        Get
            Return aDllObject.aProperty
        End Get
        Set(ByVal value As String)
            aDllObject.aProperty = value
        End Set
    End Property

    Private Sub aDllObject_OnDoSomething(ByVal sender As Object, ByVal e As EventArgs) Handles aDllObject.OnDoSomething
        'Do something

        RaiseEvent ThisOnDoSomething(sender, e)
    End Sub

    Private Sub aDllObject_OnDoThisThing() Handles aDllObject.OnDoThisThing
        'Do this thing

        RaiseEvent ThisOnDoThisThing()
    End Sub

    Public Event ThisOnDoSomething(ByVal sender As Object, ByVal e As EventArgs)
    Public Event ThisOnDoThisThing()
End Class
You might use a combination of both. It really depends on how the object allows you to interact with its events. If the object is a control then you might Inherit from Control then add the object to it. It really just all depends.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks for your answer, but I think I wasn´t clear enough. I have a driver in a DLL that comes with an USB Device. This driver acceps calle in the form of:

Public Declare Function aFunction Lib "MyDriver.dll" _
(ByVal aLong As Int32, ByRef aInt As Int16) _
As Int32

I want to create a wrapper to change some functions of my USB device without changing the programs that use this device. Therefore I want to write e DLL that acts just like the original driver (eg. accepts the same function calls) so that the program calls my DLL instead of the original one. My DLL will pass these calls after processing to the original driver and will pass the results back to the calling program. My problem is to write a DLL that accepts unmanaged function calls.

If I try to make a call to the DLL I compiled I get an error message indicating that the enterence pointer was not found. This propably has to do with my code being managed code, but I don´t know how to solve it.

Thanks again
Aart
 
Ah, yes I thought you wanted to create something to allow a program to interact with a device. I can't help with making a device think one dll is another as I've never tried to emulate another dll. Generally speaking it is usually best to see if the device has any SDK/Documentation that would give you a better idea of what is required by the device.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorry, I said just Device where I should have said Device and Program think one dll is another.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I'm affraid that it isn´t possible with VB and I'll have to write my wrapper in C and compile it to unmanaged code. Studying on that one now, but that'll be for another forum...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top