dllimport - object reference not set to instance

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having problems when I run the following code. I get the error message
that the Object reference not set to an instance of the object. Does anyone
have any idea what I am doing wrong? I am trying to access unmanaged code in
a .dll.

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCalculate.Click

Dim i As Integer
Dim press, temp As Single
Dim gas As New Fluid

press = 250.0
temp = 400.0

i = gas.Density(press, temp)


End Sub

Public Class Fluid

<DllImport("C:\Program Files\REFPROP\refprop.dll",
EntryPoint:="TPRHOdll", SetLastError:=True, _
CharSet:=CharSet.Unicode, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function _
Density(ByVal Press As Single, ByVal Temp As Single) As Integer
End Function

End Class
 
I am having problems when I run the following code. I get the error message
that the Object reference not set to an instance of the object. Does anyone
have any idea what I am doing wrong? I am trying to access unmanaged code in
a .dll.

Are you sure the DllImport method signature is correct?

BTW, you don't have to create an instance of the Fluid class to call
Shared methods on it.


Mattias
 
Mattias,

No, I am not sure that the method signature is correct. Actually, I don't
even really know what that is! Can you explain it a little more?

As for creating the instance for the fluid class, I didn't think I need to
instantiate it to use the methods but it was a shot at trying to solve the
error message (of no use obviously).

Thanks for any input you might have.
 
No, I am not sure that the method signature is correct. Actually, I don't
even really know what that is! Can you explain it a little more?

The method signature consists of the parameter types and position (and
possibly the return type, depending on whos definition you follow),
and it's crucial that you get it right for DllImport methods to work
correctly.

So what caused you to write it as

Public Shared Function Density(ByVal Press As Single, ByVal Temp As
Single) As Integer

You must have some documentaton or declaration in another language of
the Density function to base that on.


Mattias
 
Mattias,

You are correct, it was a method signature problem. I have made some
progress and am not getting the object error anymore. I have an example of
how to make the call in VB and that is what I was using. I am still having
some problems getting it to work. The Density function I posted was simple.
The one I am having real problems is this one:

<DllImport("c:/Program Files/REFPROP/Refprop.dll",
EntryPoint:="SETUPdll", SetLastError:=True, _
CharSet:=CharSet.Unicode, ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function _
SetupDLL(ByRef nc As Int32, ByVal hfld As String, ByVal hfmix As String,
ByVal hrf As String, ByRef ierr As Int32, _
ByVal herr As String, ByVal ln1 As Int32, ByVal ln2 As Int32, ByVal ln3
As Int32, ByVal ln4 As Int32) As Integer

End Function

The example from VB that I have is:

Public Declare Sub SETUPdll Lib "Refprop" (i As Long, ByVal hfld As String,
ByVal hfmix As String, ByVal hrf As String, ierr As Long, ByVal herr As
String, ln1 As Long, ln2 As Long, ln3 As Long, ln4 As Long)

The problem is in sending the strings to the dll. For some reason the dll is
not getting them and I get a valid error back under ierr that says it could
not open the files. I think it must be in the way I am sending the string.
Any ideas?

Thanks
 
Back
Top