Solved

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

Posted on 2002-03-28
22
258 Views
Last Modified: 2012-08-14
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
Comment
Question by:pahmn
  • 11
  • 11
22 Comments
 

Author Comment

by:pahmn
ID: 6903894
The DLL name is IGSetup
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6904525
Is it a 32-bit dll?
If so, why don't you use standard "declare function....."?
0
 

Author Comment

by:pahmn
ID: 6905744
I have no idea on the standard declare procedure. Please guide me by sending me a sample for my case.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6906372
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
 

Author Comment

by:pahmn
ID: 6909757
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6910677
Do you have Dependancy Walker (from visual studio)?
If so, open that dll with it.
Optionally, you could use quickview to see it.
0
 

Author Comment

by:pahmn
ID: 6910974
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
 

Author Comment

by:pahmn
ID: 6911018
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6911841
Give me a mail and i could provide you with dependancy walker.
0
 

Author Comment

by:pahmn
ID: 6913904
Please mail me at miya5656@yahoo.com
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6913962
Done.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:pahmn
ID: 6916734
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6917086
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
 

Author Comment

by:pahmn
ID: 6918737
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
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 50 total points
ID: 6918833
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
 

Author Comment

by:pahmn
ID: 6918846
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6918906
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
 

Author Comment

by:pahmn
ID: 6919598
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6920575
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
 

Author Comment

by:pahmn
ID: 6921457
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6921788
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6949525
Thanks for "A" grade!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now