Loading C++ Dll into vb.net application

181 Views Asked by At

I am new to programming and I am still learning so I might no be using the correct terminology for all the following questions.

I wrote a customization code for a third party application complied it as a dll and configured the application to load the dll which is working and I have confirmed it. This customization was written in C++. I made sure exported a function inside the customization and confined that it is visible using DLL export viewer.

Now, I have a different application that is in vb.net and I am trying to hook into the C++ customization DLL specifically to access the exported function.

Here is a code sample of what I am trying to do.

Class oClass

<DllImport("kernel32.dll", ExactSpelling:=True, PreserveSig:=False, SetLastError:=True, CharSet:=CharSet.Ansi)>
    Private Shared Function LoadLibraryA(dllToLoad As String) As IntPtr
    End Function

<DllImport("kernel32.dll", ExactSpelling:=True, PreserveSig:=False)>
Private Shared Function GetProcAddress(hModdule As IntPtr, procedureName As String) As IntPtr
End Function

<DllImport("kernel32.dll", ExactSpelling:=True, PreserveSig:=False)>
Private Shared Function FreeLibrary(hModdule As IntPtr) As Boolean
End Function

Private Delegate Sub FunctionDelegate(ByVal input As String)

Private Function vbCallingFunction()    
        Dim pDll As IntPtr = LoadLibraryA("FilePathToTheDll")
        Dim pAddressOfFunctionToCall As IntPtr = GetProcAddress(pDll, "FunctionName")
        Dim Testcall As FunctionDelegate = Marshal.GetDelegateForFunctionPointer(Of FunctionDelegate)(pAddressOfFunctionToCall)
        Testcall("It worked")
End Function

So this not working, when it reaches the GetDelegateForFunctionPointer it throws System.ArgumentNullException:'Value cannot be null. Parameter name:ptr'. So I went back and checked the pDll int pointer value and it seems to be equal to IntPtr.Zero. I verified that the third party application is running and the dll is loaded.

0

There are 0 best solutions below