Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

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
0
Leo Eikelman
Asked:
Leo Eikelman
  • 5
  • 3
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now