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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error message when trying to execute a custom DLL function- any ideas?

Status
Not open for further replies.

BrianJH

Programmer
May 26, 2003
26
US
Hello,
I have created a DLL using VC++ 6.0 with one entry point: CString test(CString string,BOOL mode).

As you can see in the code below I know how to access and use the Windows API...and in this brief test project it works. However, when I click the button and the program comes to the function from my custom DLL, it gives me this error:
An unhandled exception of type 'System.NullReferenceException' occurred in testdc.exe

Additional information: Object reference not set to an instance of an object.

Could this error be related to a problem in my DLL function? I am under the impression that this error is a coding error, and not related to the DLL function, but what reference is it talking about?

Here is the code:



Public Class Form1
Inherits System.Windows.Forms.Form
Declare Function test Lib "database.dll" (ByVal str As String, ByVal mode As Boolean) As String

Declare Function GetKeyState Lib "user32.dll" ( _
ByVal nVirtKey As Long) As Integer


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
Label1.Text = ""
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim a As Integer = GetKeyState(72)
Label2.Text = String.Format("{0}", a)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text += test("Hello World", True)
End Sub
End Class


Thank you for your time.
 
What's a CString datatype? Is it your typical null-terminated character vector?

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
in C++ CString is a null-terminated string just like String in VB. I don't know if it is true in vb, but in C++ CString is declared as a class.
 
Strings in VB6 are COM BStr types. When calling a DLL (Declare), VB6 does the translation for you.

The problem that you might be having is that your DLL function returns a string. VB has traditionally had problems doing that. Try changing your DLL's function to return the new string in the parameter list:
Code:
void test(IN CString sString, IN bool bMode, OUT CString sResult)
{
  ;
}
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
I am not using vb6, I am using .net; the database class is written in VC++ 6.

I tried the code you provided and it wouldn't work. I also tried using the C++ string class instead of CString with the same error. The function DLL runs seperately from vb correct? the dll accesses a file on C:\ and returns the contents...I will post that code as well.

string CDatabaseApp::test(string str, BOOL mode)
{
if(mode = true){
CFile file;
file.Open("C:\\vir.txt",CFile::modeRead,NULL);
char source[20000];
file.Read(source,20000);
CString ret = source;
ret.TrimRight();
str += ret;
return str;
}
}

I declare it in VB.net like this:

Declare Function test Lib "database.dll" (ByVal str As String, ByVal mode As Boolean) As String

And I use it like this:

Dim str As String
str = test("Hello World", True)
Label1.Text += str
 
Try changing the return type of your C++ function from a string to a void. Stick the return value in with the other parameters. Like I said, VB has traditionally had problems with Win32 DLLs that return strings.

Also - how did you declare the calling convention for your class? You should be doing something like this:
Code:
#define DllExport   __declspec( dllexport )

class DllExport CDatabaseApp
{
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top