Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > API > Visual Basic and C++ DLL


Reply
 
Thread Tools Display Modes
  #1  
Old 01-02-2003, 07:51 AM
Mick George
Guest
 
Posts: n/a
Question Visual Basic and C++ DLL

All,

I have searched the archives here as well as other resources and I am still having an issue with calling a function in a C++ dll from VB. I have pasted the VB test code that I am using and the C++ function declare below in hopes that some one can spot something that I am doing wrong.

When running the test code the dll loads successfully but a call to the TestScript() function raises the error "Can't find DLL entry point test_vbscript in C:\Mcam91\Chooks\vbscript.dll" I have tried a number of things including declaring the TestScript function to return an integer or a long and in both cases makes not apparent difference. We have also tried with and without declaring __declspec in the C++ declaration.

I am running XP Pro, VB6, VC6++

Code:
' -- Module Declares Public Declare Function TestScript Lib "C:\Mcam91\Chooks\vbscript.dll" Alias "test_vbscript" () As Integer Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long Public Const DEF_SCRIPT_DLL As String = "C:\Mcam91\Chooks\vbscript.dll" Public Sub Main() On Error GoTo PROC_ERR Dim lngRet As Long Dim ret As Integer ' -- Works as expected lngRet = LoadLibrary(DEF_SCRIPT_DLL) If lngRet <> 0 Then ' -- Should call a simple messagebox Call TestScript '<- Raises error ' -- Check If ret = 0 Then MsgBox "Call to TestScript failed ", vbExclamation, "DLL Test" Else ' Add code... End If Else MsgBox "Could not load dll '" & DEF_SCRIPT_DLL & "' ", vbExclamation, "DLL Test" End If PROC_EXIT: ' -- Clean up If lngRet <> 0 Then FreeLibrary lngRet Exit Sub PROC_ERR: Err.Source = "modCode::Main(Sub)" MsgBox Err.Description & vbCrLf & vbCrLf & _ "Error Number " & Err.Number & vbCrLf & _ "Error Source " & Err.Source & " ", vbExclamation, "DLL Test" Debug.Print Err.Description Resume PROC_EXIT End Sub


/////// C++ Code Begins /////////

/* __________ test_vbscript __________ */


__declspec(dllexport) int __stdcall test_vbscript(void)
{
int iRet = MessageBox (NULL, "Success", "VBScript test", MB_OK);
CString str("AfxMessageBox text");
iRet = AfxMessageBox(str, MB_SETFOREGROUND);
return(99); // for testing
}

/////// C++ Code Ends /////////
Reply With Quote
  #2  
Old 01-02-2003, 08:23 AM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,314
Default

I see a couple problems right off, or at least potential problems.

__declspec(dllexport) int __stdcall test_vbscript(void)

1) The call returns an int in C++, this is a Long in VB.

2) You need a .def file so there is no name mangling, this is likely why VB cannot find the function name.


The .def file only needs:

Library
DllName

Exports
test_vbscript

Also, this is sufficient in the C++ function definition:

int __stdcall test_vbscript(void)
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #3  
Old 01-02-2003, 08:44 AM
Mick George
Guest
 
Posts: n/a
Default

OnErr0r,

The .def was it, thank you very much!!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb