Solved

Using VBA code to instantiate object of Visual Foxpro DLL utilizing Regfree COM

Posted on 2010-09-11
22
1,469 Views
Last Modified: 2013-11-26
Hello all,
Thank you very much in advance for any help anyone can offer because I am completely stuck:

My company purchased a set of dlls created with Visual Foxpro. I created a front-end app using VBA/Excel to manage and utilize these dlls. The app works fine if the dlls are registered and a reference is added to the app. The problem is we need to use it in a server environment and are not able to have multiple instances of the app running. This is also because the dlls open reference tables upon use and lock them until they are closed. We tried having multiple copies of the Excel app/dll folders in different folders, but that does not work since it seems you can only have one copy of a COM dll registered at a time, hence the need for regfree COM.
I tried creating manifest files for the dlls, but I am not sure if the manifests are the problem or if it's my code. This is the code that I used to get just one of dlls working:

Option Explicit
Option Private Module

Sub test()

Dim oTemp4 As Object
Dim o As Object

Set o = CreateObject("Microsoft.Windows.ActCtx")
o.Manifest = "C:\Program Files\App\User 1\grccountry.dll.manifest"
Set oTemp4 = o.CreateObject("grccountry.grccountry")

            'Code to use object
           
Set oTemp4 = Nothing
           
End Sub


I get 'Runtime error 429 - Activex component can't create object'. I know I must be missing something, but I have spent several days searching and haven't had any luck. Can anyone help? Thanks.
0
Comment
Question by:Dilemma2010
  • 10
  • 9
  • 3
22 Comments
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 33655976
"because the dlls open reference tables upon use and lock them until they are closed."

If the vfp dlls are programmed this way using the dll regfree doesn't change things, or are tables referenced in a path relative to the dll? If one COM instance opens tables exclusive, no other instance can do, neither the same nor a copy of the dll.

As long as there is no configuration you could change of which dbfs to use, you can't change anything from outside the dll. The vfp programmer should change it's code to use the data, the vfp tables, in shared mode.

A vfp COM dll is by default not for SingleUse only, you can compile this way, but typically a vfp COM class is usable in as many instances as you want, as other COM servers are, there is no foxpro specialty forbiding this.

I think your problem rather is exclusive use of tables, not exclusive use of the dll, so referencing a dll copy via regfree manifest does not help.

Bye, Olaf.
0
 
LVL 27

Expert Comment

by:CaptainCyril
ID: 33655998
I second Olaf. The problem is the dll holding the files hostage until it is done with them so you cannot have another instance to handle this.

There are two ways to fix this if you can have the dlls changed by the author or if you have the source code:

1) Make them shared and available for multi-instance
2) Make the single instance/thread dll handle many users by queuing requests
0
 

Author Comment

by:Dilemma2010
ID: 33656030
Thanks so much for the quick response. I know that the dlls look for the reference tables in a position relative to the dlls.. So if you have two sets of copies of the dlls you would have two sets of tables. As long as the tables are located in the same relative position to the dlls that are opening them, they will work:

1.) C:\Program Files\Program\User 1\Tables\
2.) C:\Program Files\Program\User 2\Tables\

Where the dlls are located in the "User 1" or "User 2" directories and the reference tables are located in the "Tables" directories. What I want to know is if I am implementing regfree com procedures correctly. I suppose the problem can also be in the manifest file. I can post that as well. By the way, the error that I am getting happens here:

Set oTemp4 = o.CreateObject("grccountry.grccountry")

Thanks.
0
 
LVL 27

Expert Comment

by:CaptainCyril
ID: 33656097
The dlls are not called like exes. They have class names and one of them can be registered at a time unless they have distinct names.

If you give 5 names on building the dll you can only have 5 users working.

The better fix is to make it queue or allow multi-threading without locking the files. You can lock a record on update it's ok but not lock the file.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 33656146
Cyril, regfree COM via manifest files is working with any COM dll, also foxpro, you don't need to compile with different COm class names, if a manifest is existing parallel to the EXE instanciating a COM class in a dll, that exe will not look into the registry for the location of the COM dll, so you can use a dll witout registering it, or you can use diferent locations of the dll or use different versions with different EXEs.

The manifest does need to be with the EXE though, not with the dll, in any case you'd need to switch it for each user.
Perhaps that's what you got wrong, Dilemma. As you work in Excel you actually would need to put a manifest to the Excel.exe, called Excel.exe.manifest, but you can only put one there for all users. So regfree com will not help you.

Bye, Olaf.

0
 

Author Comment

by:Dilemma2010
ID: 33656152
I was trying to do all of this without having the developers change anything since that will be a hassle. I know regfree com works. I need to use these dlls without registering them, so I would like to know if what I'm doing (my code) is correct. This is the preferred way right now since I did not author the original dlls and reference tables. So to be clear, I would like to instantiate an object from a VFP dll without having to register it. Here's the code again:

Option Explicit
Option Private Module

Sub test()

Dim oTemp4 As Object
Dim o As Object

Set o = CreateObject("Microsoft.Windows.ActCtx")
o.Manifest = "C:\Program Files\App\User 1\grccountry.dll.manifest"
Set oTemp4 = o.CreateObject("grccountry.grccountry")

            'Code to use object
           
Set oTemp4 = Nothing
           
End Sub

Right now I haven't even gotten to the point of trying to run multiple instances since I can't get my code to run. Can anyone help?
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 33656156
There is a much easier solution for the dll to work with many users. It should not use data relative to it's location, it should use GetEnv("USERPROFILE") to determine the home path of a user's profile directory then go from there to documents or wherever the userspecific dbfs are installed.

Bye, Olaf.
0
 

Author Comment

by:Dilemma2010
ID: 33656158
Would there be any other solution for what I'm trying to do and still use the original dlls and VBA? There has to be right?
0
 
LVL 27

Expert Comment

by:CaptainCyril
ID: 33656169
Olaf, I am not familiar with regfree COM?

I built my dlls using FoxPro and they run when registered in Windows registry (regsvr32) and they can be called by their name and not by their physical location on the hard disk.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 33656187
regfree com does not change how you call a COM class, you still do the CREATEOBJECT() in the exe or VBA code, but the way winodows looks up where the dll is has changed. Since Win2003/XP it first looks for a .manifest file at the EXE location, the manifest contains the information which normally would be written to the regsitry. Therefore you can use the same class names within different EXEs and still through the manifest point to different locations/versions of the dll containing that COM class.

Bye, Olaf.
0
 

Author Comment

by:Dilemma2010
ID: 33656225
That sounds like that can work for me. Would you have any suggestions as to what I would use to create the manifests?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 33656237
Dilemma,

the dilemma is, you can have one manifest per EXE, but you'd need one for each user. You could exchange the manifest, but only if users will be allowed to write into program files/Office. You can do that but would of course lower security of the Office installation.

You can create manifests with whateever program able to create text files, but the problem is the user needs write access to overwrite the manifest file for his DLL location and the manifest needs to be there before the user opens excel I think it's not sufficient to overwrite the manifest just  before instnciating the COM class.

Bye, Olaf.
0
 

Author Comment

by:Dilemma2010
ID: 33656268
I read something about hosting the CLR directly when trying to host a .net runtime inside VFP. I wonder if I can do something similar here. This is the link:

http://www.west-wind.com/wconnect/weblog/ShowEntry.blog?id=631

I just don't want to give up on multiple instances until I'm absolutely sure there is no way it can be done with my current scenario.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 33656275
Multiple instances of the COM class is not your problem at all. This has nothing to do with needin to host the foxpro runtime many times, this just is about using data files exclusively, as far as you described it.

Bye, Olaf.
0
 

Author Comment

by:Dilemma2010
ID: 33656280
Your right. What I should have said is simply using multiple copies of the same dll/table combination in different directories, but on the same machine.
0
 

Author Comment

by:Dilemma2010
ID: 33656291
I also tried this:

http://msdn.microsoft.com/en-us/library/bb687915.aspx

I don't think I implemented it correctly though.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 33656304
Looking back at the code you initially provideded:

Set o = CreateObject("Microsoft.Windows.ActCtx")
o.Manifest = "C:\Program Files\App\User 1\grccountry.dll.manifest"
Set oTemp4 = o.CreateObject("grccountry.grccountry")

That's a new to me way to use regfree com. Forget about Excel.exe.manifest then, that's ok.

But you'd need a different Manifest file for each user in which you'd point to the users file locations of his dll copy, so you'll need to change "User 1" to the active user account name both here in this using code and in each manifest file.

Bye, Olaf.
0
 

Author Comment

by:Dilemma2010
ID: 33656330
That's actually ok for me. I think there may be a problem with my manifest files though because I still can't instantiate that "o" object. I used mt.exe to create the manifest. Here it is:




grccountry.dll.manifest
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 33656359
Declare Function name Lib ... etc is not for possible COM DLLs.

Bye, Olaf.
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
ID: 33656371
Sure the problem is with o, not with oTemp4?

What your manifest file is missing is the full path to the dll. file name="grccountry.dll" will not work.
That's also why you need a manifest for each user, as the path is different for each user, isn't it?

Bye, Olaf.
0
 

Author Comment

by:Dilemma2010
ID: 33656991
I tried to put the complete path in the manifest file and VBA throws an error about the manifest file. I guess it's not possible then. Thanks a million though.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 33657095
I have no immediate idea, but perhaps you just need to dig a little further into how manifests work. For now I see there are both exe manifests for the application using a COM class and dll manifests. Perhaps you need both for it to work.

Bye, Olaf.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

760 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

20 Experts available now in Live!

Get 1:1 Help Now