Link to home
Start Free TrialLog in
Avatar of Leo Eikelman
Leo Eikelman

asked on

Calling a function in a VB DLL from C++

I have a DLL compiled in VB6 and I need to use a method from it in a C++ application.  I have looked at the LoadLibrary method but can't get it to work properly.

Say I have the following points:

1.  The DLL is in C:\Temp\Example.dll
2.  Example.dll consits of 4 classes.  I want to use the method getURL from the Locator class.
3.  The getURL method takes in 2 Strings and returns a String

What are the steps I need to follow to be able to call this method from the VB DLL??

I have never done this before and I haven't used C++ for 2 years so try to explain step by step if possible.

Thank you.

Leo
Avatar of Raj_Kau
Raj_Kau

this is not the correct one what you asked but it help you

http://www.codeproject.com/dll/dll_calling.asp
and this one solve your problem

http://www.flipcode.com/articles/article_vbdlls.shtml

please see this portion

>>>>The Client Application (C++)

otherwise the code to use dll is

typedef DWORD ( *hh1)(LPSTR strPass1, LPSTR strPass2);
hh1 function_from_dll;

CString strRet;
CString strPass1;
CString strPass2

      HINSTANCE hLib;
      hLib = LoadLibrary(("Example.dll"));
      if(hLib!=NULL)
      {
            function_from_dll= (hh1)GetProcAddress(hLib, ("getURL"));
            strRet= function_from_dll(strPass1,strPass2);
      }
      FreeLibrary(hLib);



if you have any query then you can also ask


Raj
      
Avatar of Leo Eikelman

ASKER

I'm not sure how to use your code... I'm using VC++ 6.0 and I have the following code

#include <iostream>
#include <atlbase.h>
#include <cstring>

int main()
{
      typedef DWORD ( *hh1)(LPSTR strPass1);
      hh1 function_from_dll;

      CString strRet;
      CString strPass1;
      strPass1 = "*";

    HINSTANCE hLib;
    hLib = LoadLibrary(("C:\\Program Files\\restofPass\\Example.dll"));
    if(hLib!=NULL)
    {
         function_from_dll= (hh1)GetProcAddress(hLib, ("getURL"));
         strRet= function_from_dll(strPass1);
    }
    FreeLibrary(hLib);

      return 0;
}


I get the following errors:

--------------------Configuration: loadlibrary - Win32 Debug--------------------
Compiling...
loadlibrary.cpp
C:\Temp\C++LoadLibrary\loadlibrary.cpp(10) : error C2065: 'CString' : undeclared identifier
C:\Temp\C++LoadLibrary\loadlibrary.cpp(10) : error C2146: syntax error : missing ';' before identifier 'strRet'
C:\Temp\C++LoadLibrary\loadlibrary.cpp(10) : error C2065: 'strRet' : undeclared identifier
C:\Temp\C++LoadLibrary\loadlibrary.cpp(11) : error C2146: syntax error : missing ';' before identifier 'strPass1'
C:\Temp\C++LoadLibrary\loadlibrary.cpp(11) : error C2065: 'strPass1' : undeclared identifier
C:\Temp\C++LoadLibrary\loadlibrary.cpp(12) : error C2440: '=' : cannot convert from 'char [2]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.

loadlibrary.obj - 6 error(s), 0 warning(s)


??

What am i doing wrong?

Leo

I've tried revising my code to use the string class and I use this code:

#include <iostream>
using namespace std;

#include <atlbase.h>
#include <string>

using std::string;

int main()
{
      typedef string ( *hh1)(string strPass1);
      hh1 function_from_dll;

      string strRet;
      string strPass1;
      strPass1 = "*";

    HINSTANCE hLib;
    hLib = LoadLibrary("C:\\Program Files\\somepath\\Example.dll");
    if(hLib!=NULL)
    {
         function_from_dll= (hh1)GetProcAddress(hLib, ("getURL"));
         strRet= function_from_dll(strPass1);
    }
      else
      {
            cout << "ERROR: Unable to load library!" << endl;
            return -1;
      }
      cout << "The results: ";
      cout << strRet;
    FreeLibrary(hLib);

      return 0;
}


I get no error on compile, but when I run it I get an error that is something like this:

"The uinstruction at "0x00000" referenced memory at "0x00000". The memory could not be "read".


What does this mean?   The DLL is working fine because I have tested it with ASP pages and other VB Applications.

Leo
I think I narrowed down the problem to this line:

>>function_from_dll= (hh1)GetProcAddress(hLib, ("getURL"));

The getURL method is inside a class called Locator

would I not have to give any reference to the Locator class in my application?

Leo
no you do not need because as i think its your public member


Raj
ASKER CERTIFIED SOLUTION
Avatar of Raj_Kau
Raj_Kau

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial