0

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.

  • The function doesn't exist in your DLL. Or the DLL doesn't exist. – Blindy May 12 '20 at 15:31
  • Why not load the dll with `DLLImport` then call the function normally instead of trying to get the address of the function and manually jumping to that? – preciousbetine May 12 '20 at 15:38
  • @preciousbetine Won't that create a new instance of the DLL rather thank connecting to the existing one that is already running? – Watthehell86 May 12 '20 at 16:27
  • @Blindy as i mentions I have confirmed both – Watthehell86 May 12 '20 at 16:28
  • It doesn't matter what you confirmed or didn't confirm, I'm telling you the problem is that the function doesn't exist in the DLL with that name, or the DLL doesn't exist in a place where Windows can use it. – Blindy May 12 '20 at 16:41
  • @Blindly how can make the DLL useable able by windows? There is one more test I did, instead of passing the file path to my dll in LoadLibraryA i send an one of DLL’s where kernal32 is located and pDll value was still equals to intPtr.zero . I am not sure if that means anything to you . – Watthehell86 May 13 '20 at 02:51
  • Error checking is **not** optional when you pinvoke, these OS functions do not throw exceptions. You must check their return value for IntPtr.Zero and throw Win32Exception. Add SetLastError:=True to the GetProcAddress declaration as well. Do keep in mind that this code is not in any way superior to just declaring that function as a [DllImport]. – Hans Passant May 14 '20 at 22:38

0 Answers0