Link to home
Start Free TrialLog in
Avatar of redundguy
redundguy

asked on

Run Time Error 91

I have an application with 2 projects: an exe and an ActiveX DLL project.
The DLL contains some classes.
These 2 projects are in 2 different directories.
I grouped these 2 projects (in group1.vbg) and I developed an application that run very well.
But when I generate an exe file from the exe project and a dll file from the ActiveX project the things don?t work.
I have registered the dll with regsvr32 and I have referenced this dll in the exe project.
When I run the program I get Run Time Error 91 (Object variable or with block variable not set), when the program tries to instantiate a class from dll for the first time.
Why did I get this error? Is it something like I haven?t set a path to the other directory with dll files?
MSDN help tells that the error occurs when it isn?t a reference to BOT project (ActiveX DLL project). But I have the reference to BOT project.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Runtime 91 is the Object Variable or With Block not set!

Your ActiveX Dll project should be set to Binary Compatible in order to function correctly when compiled...
You can meanwhile inverse the order: first compile the DLL and then the EXE (you should not need to register this DLL, as during the MAKE it is registered automatically by VB).

Anyway, you might try to Add both projects into a project Group (See File->Add Project), which should simplify things for the debugging...

Cheers
You probably referenced the dll in your project group. So I suggest you open the EXE project only, set the new reference to the compiled dll and then recompile the executable.

regards,
CJ
Avatar of redundguy
redundguy

ASKER

I have set DLL project to Binary Compatible and I get the same error.
When I use Group of projects my reference is set to the vbp file of ActiveX project.
When I open only EXE project my reference is set to dll file of the ActiveX project.
When I run the group of 2 projects all run OK.
When I run EXE project I get error 91.

I am not very interested in what I have done wrong; but I?m more interested in what have I do to create and run the exe and dll files.

Thanks
When you het the errors message, which module causes the error is it the EXE or the DLL?

Also, show us the code where you are creating a reference to your object within the DLL.

Does it look something like this:

Dim MyObj

Set MyObj=CreateObject("MyProject.MyClassName")

?





are you using late or early binding?
>>When I open only EXE project my reference is set to dll file of the ActiveX project

this leads me to believe he is using early binding (otherwise, supplying the reference would be pointless)

it sounds like a versioning problem.  try and unregister the component using regsvr32 /u [componentpath]
you really dont need to set binary compatibility until you know your component is pretty well developed, and doing so wont cause possible future requirements to break compatibility.
as mentioned previously, simply choosing File -> Make dll will register your component (on your machine).  in the exe that is referencing the component, open up project -> references, and deselect the component and click OK.  then, open up project -> references again and select your newly compiled component.

in addition, in code, ensure you have actually created an instance of your component before using it.  e.g.,

'you didnt do something like this
Dim obj As MyComponent.ClassName
Call obj.SomeMethod

ensure it is declared using the New keyword, or preferrably, setting the variable to a new instance of your component when it is needed.
Thanks, but I think that Visual Basic cheated me. I tried to trace the DLL program with MsgBoxes included in DLL program, and I got an error in DLL program. The error is related to a connection object.

In fact it is a small story. I got an error related to connection object. Then I insert a code line (MsgBox oConnection,,?connection string?) before the line where I got the error. When I run the program in IDE I see the connection string and then the program run OK. When I run the EXE program I get this 91 runtime error.

Now I have to discover the error cause. It is something related to using public variable of type connection in the Modules section of ActiveX project. One object (class) of the DLL project sets the public variable (connection) and other objects (classes) use this connection. I get the error in the first object code which try to use the connection.
I repeat that I don?t get the error when I run the program in IDE, only when I run the EXE and DLL programs.

>>I repeat that I don?t get the error when I run the program in IDE, only when I run the EXE and DLL programs

this can happen if your exe is not referencing the most current version of your component.  try de-referencing and then re-referencing as i suggested above.  if that does not work, post some code (or check what differences there are in code between your test app and actual exe)
I have to reformulate the issue: Problem with Public variable in ActiveX DLL project

I have an application with 2 projects: an exe and an ActiveX DLL project.
The DLL contains some classes and is  ?Single threaded apartment?.
The module section of DLL contains a public variable vp1 (integer)
The EXE program instantiates an object in DLL program and call a method that set the vp1 variable.
Then the EXE program instantiate another object in DLL  program and call a method that show the value of vp1.
But I don?t see the value already set  with the first object: vp1 variable isn?t public for all classes from DLL project.
Why ?
I mention that these things happen when I run the exe file of the EXE project. When I run the Project from IDE, the second class shows the right value for the public variable.
I have to add some words about EXE project:
It is a form. In the load event I instantiate an object in DLL program and call a method that set the vp1 variable.
In the Click event of a button I instantiate another object in DLL  program and call a method that show the value of vp1. I cannot get the value allready set.
If I instantiate both objects in the same procedure (in load event of the form) I can get the correct value for vp1 when I call the method of the second object.


please clarify what you mean when you say "object".  do you mean you are creating a new instance of the same class?  a different class in the DLL?  how many classes are in your DLL?  just give us the basic outline of your component, e.g.,

DLLName
   -Class1
   -Class2
   -Class3
   -Module1


and then show the variable declarations and code you use to create each class and how you are calling the methods.
My application is quite complex but I actually reduced my problem at this code:

DLLname project contains:
-in Modules section:
public vp1 as integer
-in Class Modules section I have:
Class1, that have a method (method1) that sets vp1 variable
Class2 that have a method (method2) that gets vp1 variable and shows it.
In fact I want to share data between classes of the same DLL.

The exe program is a form with 2 buttons.

In the click event of the first button I have:

Dim oClass1 as DLLname.Class1
Set oClass1= New DLLname.Class1
OClass1.method1

In the click event of the second button I have:

Dim oClass2 as DLLname.Class2
Set oClass2= New DLLname.Class2
Oclass2.method2

If I run my exe file application, method2 doesn?t show the value already set with method1.

If I run the application from the IDE all is OK. (But my application will be an exe file).

(Another thing is: If I instantiate and run method of both classes in the same event procedure, the method2 show the right value. But I really need 2 buttons)

The problem is that I cannot share data between classes. A solution is to keep the common data in the EXE project and to send this data as a parameter to the second class method (method2). I'm going to do that if I cannot use a public variable in the DLL.

what is your threading model?  it should be set to apartment threading if you wish to share global data in this way.
I have set to apartment threading but it doesn't work.

I'm going to use the solution with keeping common data in the client exe and pass them as parameter to dll methods.

Maybe there are a direct solution to my problem but I realy have no time now to try.

Thanks to all of you




I have got a brilliant answer from Jim McNamara,
 on vb-world Q & A forum:

In general, you are trying to violate basic COM rules -
encapsulation in this case. Don't do it, not because of purist reasons, but because it won't work reliably as already you see, I'm not going into why it won't work. Just don't do it.
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial