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

Calling a DLL function

I am working in Access 2002, specifically VBA.

I have a custom DLL that has functions I need to call. I did not write the DLL and only know the functions that the provider told me I can call. For example:

I have this line in a public code module

Public Declare Function OpenDrawer Lib "MSPOS_USB.dll" (ByVal DeviceHandle As Long) As Integer

I call the function like this, result = OpenDrawer(DrawerHandle).

Is there a way, in the declaration, to replace the value in the strings dynamically? Instead of "MSPOS_USB.dll" I would like to modify this value in runtime. I tried using a string variable and it will not work.

Thanks,

Brian

0
bselltiz
Asked:
bselltiz
  • 4
  • 4
  • 2
  • +1
1 Solution
 
flavoCommented:
>Is there a way, in the declaration, to replace the value in the strings dynamically?

No, sorry...
0
 
mydatabaseguyCommented:
Maybe you could dynamically create a module, append the code you want to the module, save the module, then delete the module when done. However, I am not sure how VBA would handle API calls. I think the dll file is loaded into memory when the application starts, and I am not sure if you would be able to load it at runtime.

I think the best you could do is create a class module as a wrapper for the DLL functions.
declare all your different dll files there and create one class with different methods, properties, etc...
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi Brian,

You can load DLL's dynamically based on filename (using the LoadLibrary API call - see link below), but I'm curious why you would wish to "Instead of "MSPOS_USB.dll" I would like to modify this value in runtime".  Perhaps you could elaborate on your reason for wanting to do this, so we may advise accordingly.

[ http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp ]

Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long

BFN,

fp.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bselltizAuthor Commented:
I was having grief when calling the function.
Public Declare Function OpenDrawer Lib "MSPOS_USB.dll" (ByVal DeviceHandle As Long) As Integer - On some computers it would work fine, on some it would not. When I put the actual path to the DLL in the call, it always works. - Public Declare Function OpenDrawer Lib "C:\Progra~1\InstallFolder\MSPOS_USB.dll" (ByVal DeviceHandle As Long) As Integer
The only problem is that path may change, depending on where my application winds up getting installed. The DLL will always reside in the install folder of my application. A USB cash drawer is what is getting controlled by this DLL. When I install the cash drawer on a machine, I have to use their software to set it up. I'm sure that this is part of the problem, although I don't know any other way of getting around it then having a copy of the DLL in my install folder and then pointing to it.
I don't understand much of what is going on behind the scenes here, so there may be an entirely different solution, I can't say.
0
 
mydatabaseguyCommented:
Sounds like you should focus your attention on registering the dll in your setup. If you register the dll with windows you should not have to put a path when declaring the function. The most common approach is to place a copy of the dll file in the windows system32 folder. Then register the dll using regsvr32. However there are alternatives to using regsvr32. (http://www.vbusers.com/code/codeget.asp?ThreadID=46&PostID=1&NumReplies=0)
If you have access to Office Developer editions there is a package and deployment wizard which will handle your setup for you.
0
 
bselltizAuthor Commented:
regsvr32 will not work, something about it not being a COM or ActiveX dll. Another thing is that the cash drawer may get added on at some point after the application has already been installed, so I can't necessarily depend on the installation package.
0
 
[ fanpages ]IT Services ConsultantCommented:
If you use Public Declare Function OpenDrawer Lib "MSPOS_USB.dll" (ByVal DeviceHandle As Long) As Integer, then you can simply add (progrmatically) the actual folder where the DLL is located to the user's PATH environment variable.

Is this a practical approach?

BFN,

fp.
0
 
bselltizAuthor Commented:
Hey,

I don't really know if it's a practical approach, I'm not quite sure how to do it, not quite sure what exactly it is I would be altering and not quite sure if it would cause any other problems with other programs.

I'm open to hearing more though :->

Brian
0
 
[ fanpages ]IT Services ConsultantCommented:
Hi Brian,

The PATH environment variable contains a list of folders where an application will try to resolve references to files (such as Dynamic Link Libraries) that it cannot find in the same folder as the application.

The individual folders in this 'list' are separated by semi-colon characters.  When a reference to a file is requested, that does not have an explicit path specified (e.g. simply "MSPOS_USB.dll", rather than "c:\windows\system\MSPOS_USB.dll") the list is read from left to right (most prominent to least prominent) and the contents of each folder in the list are checked to see if the requested file is present.  If it is, then it is used, if not, then the next folder in the list is checked, until the end of the list is reached.

The considerations to take on board with regards other applications is that the PATH variable can only contain a maximum number of characters (253, I think, certainly no more than 255), and you may end up reference a different file than you intended (in other applications) if the order of the folders is not considered.

To reduce the impact to other applications, the folder should be added to the end of the PATH so that any previous folders still have priority.

If you wish to try this yourself, you may review the PATH environment variable by using the "System" Control Panel Applet, selecting the "Advanced" tab & then clicking the [Environment Variables] button at the bottom of the dialog window.  The (global machine) setting for 'PATH' will be in the System Variables (bottom) list.  If your current Windows user has overridden the setting to make a localised difference to the global machine setting you will see this in the top list.  You can make changes - but be careful to note what the setting was originally - in case unexpected issues arise later (as I mentioned above).  Confirm with [OK].  Then close the "System Properties" applet with [OK] also.

If you are familiar with the MS-DOS (Command) Prompt, simply type SET PATH and press [Enter] to check any changes have been applied correctly.  If not, go back to the "System" applet to revise.

BFN,

fp.

0
 
bselltizAuthor Commented:
fanpages - worked great, thanks!
0
 
[ fanpages ]IT Services ConsultantCommented:
You're very welcome.

Thanks for your points/grading.

BFN,

fp.
[ http://NigelLee.info ]
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!

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now