Question GetProcAddress API

Sparda

Member
Joined
Jul 30, 2011
Messages
5
Location
Greece
Programming Experience
1-3
GetProcAddress Function

Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).

Lets say I have something like this:

VB.NET:
Dim moduleHandle As Integer = LoadLibrary("C:\myDLL.dll")
Dim methodName As String = ""
GetProcAddress(moduleHandle, methodName)

DLL should be win32 standard dll or .net dll.

How can I run the exported function if I have the address of it through GetProcAddress().If someone could supply some code it would be great.

Like if dll has this code:

VB.NET:
Function Show()
    Msgbox("It works!")
End Function

I have this code:
Dim moduleHandle As Integer = LoadLibrary("C:\myDLL.dll")
GetProcAddress(moduleHandle, "Show")

I need to add something but I don't know what :/. I may also need to send some parameters but lets leave that for later.

Thx for reading.
 
I have this class:

VB.NET:
NotInheritable Class DynamicAPI


#Region "API"
    <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)> Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
    End Function
    <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Ansi, ExactSpelling:=True)> Private Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal procName As String) As IntPtr
    End Function
    <Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True, EntryPoint:="FreeLibrary")> Private Shared Function FreeLibrary(ByVal hModule As IntPtr) As Boolean
    End Function
#End Region


#Region "Types"
    Public Delegate Function DefaultAPI() As IntPtr
    Public Delegate Sub VoidAPI()
#End Region


#Region "Methods"
    Public Overloads Shared Function Invoke(ByVal Library As String, ByVal [Function] As String, Optional ByVal [Delegate] As Type = Nothing) As Object
        If [Delegate] Is Nothing Then
            [Delegate] = GetType(DefaultAPI)
        End If
        Dim [Module] As IntPtr = LoadLibrary(Library)
        If [Module] = IntPtr.Zero Then
            Throw New Exception("Could not load DLL")
        End If
        Dim Method As IntPtr = GetProcAddress([Module], [Function])
        If Method = IntPtr.Zero Then
            FreeLibrary([Module])
            Throw New Exception("DLL was loaded but the method was not found")
        End If
        MsgBox(Method)
        Dim Value As Object = Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(Method, [Delegate]).DynamicInvoke
        FreeLibrary([Module])
        Return Value
    End Function
    Public Overloads Shared Function Invoke(ByVal Library As String, ByVal [Function] As String, ByVal [Delegate] As Type, ByVal ParamArray Arguments As Object()) As Object
        If [Delegate] Is Nothing Then
            [Delegate] = GetType(DefaultAPI)
        End If
        Dim [Module] As IntPtr = LoadLibrary(Library)
        If [Module] = IntPtr.Zero Then
            Throw New Exception("Could not load DLL")
        End If
        Dim Method As IntPtr = GetProcAddress([Module], [Function])
        If Method = IntPtr.Zero Then
            FreeLibrary([Module])
            Throw New Exception("DLL was loaded but the method was not found")
        End If
        Dim Value As Object = Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(Method, [Delegate]).DynamicInvoke(Arguments)
        FreeLibrary([Module])
        Return Value
    End Function
#End Region


End Class

DynamicAPI.Invoke("C:\myDLL.dll", "Exec", GetType(keypo)) I use it like this and the code on the .dll is this:

VB.NET:
Function Exec(Byval param0 as string,byval param1 as string)
Msgbox("It works" & param0)
End Function

I call it but it still gives me an error. (DLL was loaded but the method was not found)

This error is because GetProcAddress returns 0.Even if I tried with a new function without parameters. :/
 
Last edited:
You could try a dll exports viewer to see what is exported.
 
You could try a dll exports viewer to see what is exported.

Could you provide me one or something?Actually I found out how it works for vb.net this is the code:

VB.NET:
'Dim ass As System.Reflection.Assembly = Assembly.LoadFrom("C:\myDLL.dll")
        'Dim obj As Object = ass.CreateInstance("DLL_.Class1", True)
        Dim t As Type = obj.GetType()
        Dim params(1) As Object
        params(0) = "a"
        params(1) = "b"
        Dim method As MethodInfo = t.GetMethod("Exec")
        method.Invoke(obj, BindingFlags.Instance, Nothing, params, Nothing)

But I can't figure out how it would work for win32 standard dll.I made one in C++ with a function this one:

VB.NET:
int __stdcall MyFunction(char *szString, int nFirstNum, int nSecondNum)   
{   
    //Display a messagebox With the String (just wanted To show you   
    //how To pass a String)   
    MessageBox(NULL,szString,"Sample Code", NULL);   
   
    //Return the sum of the two integers. Notice a VB Long Is an Integer   
    //in c++.    
    return nFirstNum + nSecondNum;   
}

Now Im trying to call that function from inside my vb.net executable, I should use the api's now I think.
 
Could you provide me one or something?
Any one should do, try search for 'dll export viewer'.
Actually I found out how it works for vb.net this is the code:
That is for a managed library, something else entirely.
But I can't figure out how it would work for win32 standard dll.I made one in C++ with a function this one:
How to export functions from a C++ library is a topic for a C++ forum, not a VB.Net forum.
Now Im trying to call that function from inside my vb.net executable, I should use the api's now I think.
Statically you can call unmanaged functions using the Declare statement or DllImport attribute, or dynamically using the GetProcAddress method along with Marshal.GetDelegateForFunctionPointer, but the function needs to be properly exported or using the exported name or ordinal.
 
Statically you can call unmanaged functions using the Declare statement or DllImport attribute, or dynamically using the GetProcAddress method along with Marshal.GetDelegateForFunctionPointer, but the function needs to be properly exported or using the exported name or ordinal.

Thank you so much, you have provided huge help.

As I show above my c++ code. I use this function to call it from a library that I 've found over the net, the one I posted above the DynamicAPI class.

This is the code I use to call the dll:

VB.NET:
 Try
            DynamicAPI.Invoke("C:\example1.dll", "MyFunction", Nothing)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

I get this error again.

(DLL was loaded but the method was not found).

While my code on c++ appears to be right.


MyFunction(char *szString, int nFirstNum, int nSecondNum)


However the export viewer showed nothing, so the real problem must be lying inside there.
 
Back
Top