• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Call a procedure in VB from a standard DLL(not ActiveX)

Hi All

I want to call a procedure in VB from a standard DLL(not ActiveX) .

I am using LoadLibrary/GetProcAddress API call to load the DLL and setting a pointer to the function. I am totally new to this, and I am unable to figure out how to pass the reqired parameters to make the call.

I need to be able to make a call like :

INT IGSearch(Address,City, State,Zip, StateCode,CountyCode, MSACode,TrackBNA)

Please send me the exact way to do it.

Thanks
Pahmn
0
pahmn
Asked:
pahmn
  • 11
  • 11
1 Solution
 
pahmnAuthor Commented:
The DLL name is IGSetup
0
 
Richie_SimonettiIT OperationsCommented:
Is it a 32-bit dll?
If so, why don't you use standard "declare function....."?
0
 
pahmnAuthor Commented:
I have no idea on the standard declare procedure. Please guide me by sending me a sample for my case.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Richie_SimonettiIT OperationsCommented:
is it a COM dll?
If not, something like:
private declare function IGSearch lib "igsearch" (Address,City, State,Zip, StateCode,CountyCode, MSACode,TrackBNA) as integer

I assumed that dll is called "igsearch", replace with a valid name.
What is strange to me is return value integer, are you sure that it is a 32-bit dll?
I dont know data types of parameters so you must to replace accordingly.

This is an example of standard declare procedure for a give api:

Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long)
As Long
0
 
pahmnAuthor Commented:
Is there some way to know that whether it is a 32-bit DLL or not. I was given this DLL by a company to use. I do not have its details...
0
 
Richie_SimonettiIT OperationsCommented:
Do you have Dependancy Walker (from visual studio)?
If so, open that dll with it.
Optionally, you could use quickview to see it.
0
 
pahmnAuthor Commented:
Which quickview? I cannot even add the dll in the references in VB, so I am not sure how would I open it in a Dependancy walker...

I am totally new to this kind of DLLS. I just make and use ActiveX DLLS...
0
 
pahmnAuthor Commented:
I have the following details about the parameters:

INT IGSearch(LPCTSTR lpAddress, lpCity, lpState, lpZipCode,
lpStateCode, lpCountyCode, lpMSACode, lpTractBNA)

Arguments
lpAddress, lpCity, lpState, lpZipCode
Specifies the address to be given as input.

lpStateCode, lpCountyCode, lpMSACode, lpTractBNA
Specifies output values to be filled-in by the Function.

I do not understand how to use these parameters as input and output?
0
 
Richie_SimonettiIT OperationsCommented:
Give me a mail and i could provide you with dependancy walker.
0
 
pahmnAuthor Commented:
Please mail me at miya5656@yahoo.com
0
 
Richie_SimonettiIT OperationsCommented:
Done.
0
 
pahmnAuthor Commented:
Thanks Richie

It is a 32-bit DLL.
When I declare the DLL and try to use it, it gives me an Entry point not found error...

I think this is a fairly common error with this kind of DLLs, but how can this be resolved.

Pahmn
0
 
Richie_SimonettiIT OperationsCommented:
Well, are you sure that is the real name for function?
You could check it with Dependancy walker also (it list all exported/imported function that uses)
0
 
pahmnAuthor Commented:
Yes, I checked it in the dependency walker, and also I have the documentation for the DLL, which lists the name of the function.
0
 
Richie_SimonettiIT OperationsCommented:
Well, do you know if it was used with vb before?
Dll that are not Activex dll must follow stdcall convention to allow vb to use it.
How about documentation, any clue there?

See this article at MSDN:
Calling DLL Functions from Visual Basic Applications
Home |  Overview |  How Do I |  FAQ |  Details |  Sample

In order for Visual Basic applications (or applications in other languages such as Pascal or Fortran) to call functions in a C/C++ DLL, the functions must be exported using the correct calling convention without any name decoration done by the compiler. In Windows 3.x and 16-bit editions of Visual C++, you could use the _pascal keyword as part of the definition of the exported function. However, there is no _pascal keyword in the 32-bit editions of Visual C++. Instead the WINDEF.H header file has PASCAL defined as __stdcall. This creates the correct calling convention for the function (the called function cleans up the stack and parameters are passed from right to left) but decorates the function name differently. So, when __declspec(dllexport) is used on an exported function in a DLL, the decorated name is exported instead of the desired PASCAL style name, which is undecorated and all uppercase.

PASCAL name decoration is simply the undecorated symbol name in uppercase letters. The __stdcall name decoration prefixes the symbol name with an underscore (_) and appends the symbol with an at sign (@) character followed by the number of bytes in the argument list (the required stack space). So, the function when declared as:

   int  __stdcall func (int a, double b)

is decorated as:

   _func@12

The C calling convention (__cdecl) decorates the name as _func. Whereas the desired PASCAL style name is FUNC.

To get the decorated name, set the Generate Mapfile option in the Linker General category setting. Use of __declspec(dllexport) does the following:

If the function is exported with the C calling convention (_cdecl), it strips the leading underscore (_) when the name is exported.


If the function being exported does not use the C calling convention (for example, __stdcall ), it exports the decorated name.
So to simulate PASCAL name decoration and calling conventions, you must have the "Called Function stack clean-up" provided by using __stdcall and the undecorated uppercase name.

Because there is no way to override where the stack clean up occurs, you must use __stdcall. To undecorate names with __stdcall, you must specify them by using aliases in the EXPORTS section of the .DEF file. This is shown below for the following function declaration:

   int  __stdcall MyFunc (int a, double b);
   void __stdcall InitCode (void);

In the .DEF file:

   EXPORTS
      MYFUNC=_MyFunc@12
      INITCODE=_InitCode@0

For DLLs to be called by programs written in the 32-bit version of Visual Basic version 4.0, the alias technique shown in this article is needed in the .DEF file. If alias is done in the Visual Basic program, use of aliasing in the .DEF file is not necessary. It can be done on the Visual Basic program by adding an alias clause to the declare statement as shown here:

Declare Function MyFunc Lib "dlllibname" Alias "_MyFunc@12"  (...)
                                As Integer

The complete syntax for the Visual Basic declare statement follows:

[Public | Private ] Declare Function name Lib
     "libname" [Alias "aliasname" ] [([arglist])][As type]

Note   A very good discussion (with example code) of calling a C DLL from Visual Basic can be found in the file VB4DLL.TXT in the VB directory.

Export from a DLL


Export from a DLL using .DEF files


Export from a DLL using __declspec(dllexport)


Export C++ functions for use in C-language executables


Determine which exporting method to use


'*************************
I recommend you read this article and others in links in that page.
Do you have MSDN installed?
0
 
pahmnAuthor Commented:
Richie, I am posting my exact code. There are two functions to be called prior to calling the IGSearch. When I am calling the first function IGSetupGetPath , I should be getting the path in IGPath, which I am not getting. And hence, the subsequent calls fail. Though I am getting the value of Buffer Size, I do not get the exact path....
Any ideas???


==========================================================

Private Declare Function IGsearch Lib "C:\Winnt\system32\IGSystem.dll" (Address As String, City As String, State As String, Zip As String, StateCode As String, CountyCode As String, MSACode As String, TrackBNA As String) As Integer
Private Declare Function IGSetupGetPath Lib "C:\Winnt\system32\IGSetup.dll" (PathBuffer As String, BuffSize As Integer, ForceDisp As Boolean) As Boolean
Private Declare Function IGSystemSetPath Lib "C:\Winnt\system32\IGSystem.dll" (IGPath As String) As Boolean

Private Sub Form_Load()
dim flgPath as Boolean
dim flgPath1 as Boolean
dim test as Integer
Dim IGPath As String
Dim PSize As Integer

flgPath = IGSetupGetPath(IGPath, PSize, True)
flgPath1 = IGSystemSetPath(IGPath)
 
test = IGsearch("2 Ada", "Irvine", "CA", "92618", StateCode, CountyCode, MSACode, TrackBNA)


end sub
0
 
Richie_SimonettiIT OperationsCommented:
Private Declare Function IGsearch Lib "IGSystem" (Address As String, City As String,
State As String, Zip As String, StateCode As String, CountyCode As String, MSACode As String, TrackBNA
As String) As Integer
Private Declare Function IGSetupGetPath Lib "IGSetup" (PathBuffer As String, BuffSize
As Integer, ForceDisp As Boolean) As Boolean
Private Declare Function IGSystemSetPath Lib "IGSystem" (IGPath As String) As
Boolean

Try passing arg ByVal. It appears like a trial and error way of life but ....
Could you send me the dll?
0
 
pahmnAuthor Commented:
Richie

Thanks for all your help.
It finally started working after passing args ByVal, and filling the variables with spaces prior to making the call. But the strange thing is that sometimes it works, and sometimes it doesn't. Is there a way to set this DLL to nothing after the job is done?

Thanks again
Pahmn
0
 
Richie_SimonettiIT OperationsCommented:
That dll is not an object so we can't set it to "nothing". If you are using loadlibrary instead of "declare" way, it could be released from memory when it would be longer needed.
 
0
 
pahmnAuthor Commented:
I had tried LoadLibrary, but was unable to figure out how to call the functions from it. If we stick to the Declare method, when does the dll get released from the memory?

Thanks
Pahmn
0
 
Richie_SimonettiIT OperationsCommented:
that's a good question by itself!!!
I think that the life of that dll in memory should be in scope of calling thread. I mean, if you use that function inside an other fucntion, dll should be released from memory after you reached en function statement (or a little after that).
0
 
Richie_SimonettiIT OperationsCommented:
Thanks for "A" grade!
0
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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