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
LVL 8
Leo EikelmanDirector, IT and Business DevelopmentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

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

please see this portion

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

0
Raj_KauCommented:
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
      
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Leo EikelmanDirector, IT and Business DevelopmentAuthor Commented:
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

0
Leo EikelmanDirector, IT and Business DevelopmentAuthor Commented:
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
0
Leo EikelmanDirector, IT and Business DevelopmentAuthor Commented:
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
0
Raj_KauCommented:
no you do not need because as i think its your public member


Raj
0
Raj_KauCommented:
you write here

>>The getURL method takes in 2 Strings and returns a String

so you havet to write your code as


#include <afx.h>


typedef CString ( *hh1)(LPSTR strPass1, LPSTR strPass2);

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

     return 0;
}


you have to add afx.h for CString

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.