Solved

searching for a .dll and store the location not to search again

Posted on 2002-06-26
25
211 Views
Last Modified: 2010-05-02
In order to get a programme startet written in VB6.0 I have to call a externall dll lets say "external.dll"

So I used the declare statement to set the entry point for the other programme

Declare Sub CALLDLL   Lib "d:\...\...\external.dll" Alias "_vbmainace@76" (ByVal........)

At several different locations in the VB programme I then can call the subroutine CALLDLL and execute the external dll. It all works fine on my machine the problem starts when I want to install this programme on different machines!!


Now my question:
In the moment I write the path were the "external.dll" is located into the programme code.
The problem is that the location of the "externall.dll" is not specified by default on the users machine.
In theory I only have to figure out the location when I install my VB programme on a different machine and from than it should not alter at all.

Is there any procedure to search automaticely for the "external.dll" file during installation and write the location with path into a special file ( or perhaps the registry ???) and than implement this location into the source code. So that the programme can execute the file without searching for the location every time it is running ???
0
Comment
Question by:themroc
  • 11
  • 8
  • 3
  • +3
25 Comments
 
LVL 2

Expert Comment

by:uncle_med
Comment Utility


Why don't you use relative path. If you create a subfolder in the folder where your app resides you can use:

App.Path & "\FolderName\DllFile.Dll"

This way, no matter where your app is installed it will always look in the subfolder in the app folder.
0
 

Author Comment

by:themroc
Comment Utility
The problem is that the "external.dll" can be anywhere on the machine were I am going to install my application.
So in a first step I have to search fro the location of this .dll file
0
 
LVL 2

Expert Comment

by:uncle_med
Comment Utility
So external.dll is not actually part of your installation. It can already be anywhere on your clent machine. Is that right?
0
 

Author Comment

by:themroc
Comment Utility
The problem is that the "external.dll" can be anywhere on the machine were I am going to install my application.
So in a first step I have to search fro the location of this .dll file
0
 

Author Comment

by:themroc
Comment Utility
That is exactly right, "external.dll" is already on the client machine before installation. So in a first step I have to locate this file and then I have to write the location somewhere to avoid searching for it every time I run my programme
0
 
LVL 2

Accepted Solution

by:
uncle_med earned 105 total points
Comment Utility
If your DLL is a com dll then windows automatically looks in your app directory and also system (ofcourse the dll must be registered).
If its not, I guess you have to search the whole disk.
Have you thought about including the dll in your installtion routine. That way you will always know where the dll is going to be. The down side is that clients will probably end up with 2 copies of the dll, but surely that's not a big problem.
0
 

Author Comment

by:themroc
Comment Utility
Because the .dll is part of a comercial programme I did not dvelope I think it is not possible to distribute the .dll a second time. I think I would not mind to search the whole disk. The problem is more that the client has to search during installation. Then the search result has to be writen in a location were I can access it easily everytime I run my programme.
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
Here is an idea.  When you start your app on the users machine do a search in the most common places where it usually is.  If it is not there then do a full search.  Once you have the file location write it to an INI file in the app.path.  Next time your program starts you will have it.  You can use the below INI API functions to read and write the INI file.

FYI...If you do not list the location of your dll, I believe windows will look in the Windows\system directory for the dll first, and then I believe the Windows directory.  It does this by default so if the DLL is in the windows\system or windows directory it should be able to find it without you doing anything.

Paste this code in a module in your app.

'Declares
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
    "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString _
    As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
    "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
'----------------------------------------------------

'Functions
Public Function ReadINI(SectionName As String, LeftOfEqual As String, ReturnVar As Variant, TheAnswer As String, BufferSize As Integer, INIFilePath As String) As Long

        cReturnLng = GetPrivateProfileString(SectionName, LeftOfEqual, ReturnVar, TheAnswer, BufferSize, INIFilePath)
        ReadINI = cReturnLng
       
End Function
Public Function WriteINI(SectionName As String, LeftOfEqual As String, RightOfEqual As String, INIFilePath As String) As Long

    WriteINI = WritePrivateProfileString(SectionName, LeftOfEqual, RightOfEqual, INIFilePath)
   
End Function
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
I am trying to think of a better way for you to do this.  I do not think you can use a variable in a declare, which means you cannot pass it a path.

When you find the file you can make a copy of it into the windows\system dir or your app dir then you do not have to store the name anywhere or change any code.  Copying the file shouldn't do any harm except if the external program is ever updated you will want to make sure you get the new copy of the dll.

Here is file search routine.  There are many others on PSC you can search for as well.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=14249&lngWId=1

0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
You will want to test this of course on your machine.  Copy this file to a different dir and call it from there just to be sure it is not dependent on another file in the directory or something.
0
 
LVL 2

Expert Comment

by:egsemsem
Comment Utility
Did you try writing "external.dll" only with no path, for example:
    Declare Sub CALLDLL Lib "external.dll" Alias "_vbmainace@76" (ByVal........)
If 'external.dll' is registered properly on the client System, I think windows will find its path correctly. And if this method didn't succeed, try to add a refrence to 'external.dll' in your project instead of using declare function, it might succeed.



thanks,

Osama

0
 

Author Comment

by:themroc
Comment Utility
If I write external.dll only into the declare function than it coemes up with the message "File not found"

How do I add a reference to "external.dll" instead of using the declare function ???
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:themroc
Comment Utility
If I write external.dll only into the declare function than it coemes up with the message "File not found"

How do I add a reference to "external.dll" instead of using the declare function ???
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
It does not sound like this is a COM component and therefore cannot be registered and referenced.  If it was a COM component you never would have used Declare to begin with.  If there was documentation that came with the dll, and if it told you to declare the dll then it is not a COM component and a reference will not work.

If you would like to try to register the dll just in case here is how.

At a command prompt type regsvr32 c:\external.dll (or path to dll)

If you get an error, and the software that uses the dll works correctly, of course, then this is not a COM component and you must use DECLARE.
0
 

Author Comment

by:themroc
Comment Utility
thanks,
I tried and got the message:
was loaded but the DLLRegister Service entry point was not found. DLLRegister Service may not be exported, or a corrupped version of "external.dll was in memory.
Consider PView to detect and remove it
__________

Im not sure how to interpret this, but one think is clear the dll is not corrupt. So I think it is not a proper COM component.

Im not quiet sure what to do next ???


0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
Have you tried my comment above about finding the file on the machine and copying it to a directory?

The easiest is probably to distribute this file with your app in the same directory so when you install your file this file is installed as well.

You can write code that looks for the file in the app.path.  If it cannot find it then copy it from a network drive someonewhere.

You could also run the file from a network drive.  I don't know if declare statements support UNC names (\\Server\path) so everyone should have the same drive mapping if you do this.


0
 

Author Comment

by:themroc
Comment Utility
In the moment I think the best will be  to distribute the file with the application.

I will wait a little bit till closing the question,
perhaps someone else comes up over night with a brilliant idea.
Thanks a lot so far !!!!!!

themroc
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
:)
0
 

Author Comment

by:themroc
Comment Utility
In the moment I think the best will be  to distribute the file with the application.

I will wait a little bit till closing the question,
perhaps someone else comes up over night with a brilliant idea.
Thanks a lot so far !!!!!!

themroc
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
Just thought of something else.  When you distribute the file place it in the Windows\system or Winnt\system32 (or whatever the system directory is).  This way you do not need to hard code a path in the Declare.  You can then declare like this and the app will find the file...

Declare Sub CALLDLL Lib "external.dll" alias "_vbmainace@76" (ByVal........)

This will be of benefit if you want to change the directory of your application code will not have to change.
0
 

Author Comment

by:themroc
Comment Utility
Final distribution of points

I'm a little bit in a dilema,
Gencross did a lot of support and Im very gratefull for this but in the end we came up with the solution uncle med alredy proposed. Is there a way to share points?

Because to be fair I think I have to give the points to uncle med??
0
 
LVL 27

Expert Comment

by:Ark
Comment Utility
Hi
Take a look on my code at http://www.freevbcode.com/ShowCode.Asp?ID=1863
>>This advanced module allows you to call API functions by name without declaring and to return values from them. It uses Assembly Language within VB code to accomplish this task.<<

With this code you can call your dll dinamically, without declarations.

Cheers
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
You can split the points by asking community support to lower the points on this question by half then accept an answer to the question.  Post another question with title of "Points for...".  One of us will comment on it and you accept the comment as an answer.

0
 

Expert Comment

by:ComTech
Comment Utility
As per request in Community Support, the points have been lowered for a split.

Thanks,
ComTech
CS Admin @ EE
0
 

Author Comment

by:themroc
Comment Utility
In the end I went for this solution,
its not optimum but it solved the problem so far
thanks
0

Featured Post

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!

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

771 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

7 Experts available now in Live!

Get 1:1 Help Now