Author Topic: Early vs Late and the GAC  (Read 6401 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Early vs Late and the GAC
« on: November 15, 2011, 05:26:33 PM »
Ok, so I know I need to learn how to do Late binding.  That being said, I can make the MS examples work, but I cant seem to make it work from within Autocad.  In desperation to finish a project, I switched to early binding, and Jeff and a few others helped me get it working here
 
Now, I deployed it to everyone, and of course, it works on half the machines.  I have checked, and of the 2 machines it doesn't work on, they do have Excel 2007, so I am assuming the Type 12 library is there.  The error message talks about the GAC, so I came back for help.  Any ideas why it runs perfect on 1 machine and not another?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Early vs Late and the GAC
« Reply #1 on: November 15, 2011, 06:32:33 PM »
What does the error say?
 
All 64bit, 32bit, mix, etc......?

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Early vs Late and the GAC
« Reply #2 on: November 15, 2011, 07:05:32 PM »
I vaguely remember coming across something about the GAC when trying to fix something else, where it mentioned that version identifiers are vitally important when pulling things out of the GAC since different versions are considered to be "different" and thus you may have multiple versions of the same assembly haunting the GAC.  Or some such.   :ugly:
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Early vs Late and the GAC
« Reply #3 on: November 16, 2011, 09:04:01 AM »
32bit XP Office 2007, acad 2012

Error msg System.IO.FileNotFoundException Could not load file or assembly Microsoft.Office.Interop.Excel, Version = 12.0.0.0 Culture=neutral PublicKeyToken = 71e9bce111e9429c or one of its dependencies.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Early vs Late and the GAC
« Reply #4 on: November 16, 2011, 09:06:52 AM »
It works on 2 machines just fine.  I just dont have a clue what its doing.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Early vs Late and the GAC
« Reply #5 on: November 16, 2011, 09:09:05 AM »
Here is the dialog box
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Early vs Late and the GAC
« Reply #6 on: November 16, 2011, 05:22:10 PM »
Try this one and see if it errors.
 
I uploaded it and solution is at bottom of post.
It has no references to Type 12 Library and binds at runtime(Late Binding)
 
Built with Acad .NET wizard template thingy
All it has is one command in it to open Excel with a blank workbook
Code: [Select]
[CommandMethod("OpenExcelLateBind")]
        public void OpenExcelLateBind()
        {
            Type excelType = Type.GetTypeFromProgID("Excel.Application");
            object exl = Activator.CreateInstance(excelType);
            object wb = exl.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, exl, null);
            wb.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, wb, null);
            object[] param = new Object[1];
            param[0] = true;
            exl.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, exl, param);
        }

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Early vs Late and the GAC
« Reply #7 on: November 17, 2011, 08:50:51 AM »
Thanks Jeff, Now to see if I can merge this with what I have.  i will be back Im sure.....
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Early vs Late and the GAC
« Reply #8 on: November 17, 2011, 09:41:04 AM »
OK, this is turning into a PITA!  Is everything have to be created using InvokeMember, meaning range, selected cell, columns, etc?  This being my first attempt at Late binding, i'm really lost.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

kaefer

  • Guest
Re: Early vs Late and the GAC
« Reply #9 on: November 17, 2011, 09:51:37 AM »
OK, this is turning into a PITA!  Is everything have to be created using InvokeMember, meaning range, selected cell, columns, etc?  This being my first attempt at Late binding, i'm really lost.

Cmdr,
the short answer is: yes, everything have to. But no worry, as we were all at that point sometimes before, there are a few helper methods out there, like Get, Set and Invoke.

For e.g. ranges, have again a look at this thread AutoCAD to Excel and back again, whence this code:

Code: [Select]
        // Operates on Excel's Range object only
        static object XlRangef(this object o, int r0, int c0, int r1, int c1) {
            return o.Get("Range", o.Get("Cells", r0, c0), o.Get("Cells", r1, c1));
        }

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Early vs Late and the GAC
« Reply #10 on: November 17, 2011, 10:33:33 AM »
As kaefer has also mentioned in other threads since you are using 2012 you will be able to use 4.0 with dynamics
If you use solution posted you can add this command but need to add a reference to Microsoft.CSharp
 
 
Code: [Select]
  [CommandMethod("OpenExcelLateBindDynamic")]
        public void OpenExcelLateBindDynamic()
        {
            dynamic exl = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));
            dynamic wb = exl.Workbooks;
            wb.Add();
            exl.Visible = true;
        }
 

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Early vs Late and the GAC
« Reply #11 on: November 17, 2011, 10:34:14 AM »
thanks kaefer.  I have been studying your code along with Jeffs code to try and make this work.  I'll keep at it.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Early vs Late and the GAC
« Reply #12 on: November 17, 2011, 10:35:23 AM »
Thanks Jeff, I will see if I can make that option work as well.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Early vs Late and the GAC
« Reply #13 on: November 17, 2011, 10:41:25 AM »
OK, this is turning into a PITA!  Is everything have to be created using InvokeMember, meaning range, selected cell, columns, etc?  This being my first attempt at Late binding, i'm really lost.

If you look at it as maybe as
pass in the string of name of method,
BindingFlags to let it know if it setting, getting a property or calling a method etc.....
The object it is calling the method on
a array of the parameters(size is the number of parameters) to be passed in or null if none
 

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Early vs Late and the GAC
« Reply #14 on: December 09, 2011, 10:25:27 AM »
ok Jeff, Im stuck again.  I got this far
Code: [Select]
Type excelType = Type.GetTypeFromProgID("Excel.Application");
            object exl = Activator.CreateInstance(excelType);
            object wb = exl.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, exl, null);
           
            wb.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, null, wb, new object[] { strMyFile });
            object[] param = new Object[1];
            param[0] = true;
            exl.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, exl, param);
            param[0] = false;
            exl.GetType().InvokeMember("UserControl", BindingFlags.SetProperty, null, exl, param);
           
            param[0] = "Sheet1";
            object ws = exl.GetType().InvokeMember("Sheets", BindingFlags.GetProperty, null, exl, param);
           
            object rng = exl.GetType().GetProperty("Range", BindingFlags.SetProperty, null, null );
but I cant seem to create or set a Range object.  I have tried every BindingFlags prop I can think might work.
 
I also tried ws.GetType, but that didn't work either
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)