Author Topic: cast error with COM Objet and A2010  (Read 7152 times)

0 Members and 1 Guest are viewing this topic.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
cast error with COM Objet and A2010
« on: June 03, 2009, 02:01:36 AM »
Hi,

I need to get the support search pathes.
I read it can be done using COM, so I try this :

AcadPreferences prefs = (AcadPreferences)Application.Preferences;

It works fine with 2007, 2008 and 2009 versions but returns an exception with A2010:

"Impossible d'effectuer un cast d'un objet COM de type 'System.__ComObject' en
type d'interface 'Autodesk.AutoCAD.Interop.AcadPreferences'.
"

"Unable to make a cast of a COM object of type 'System.__ComObject' in
interface type 'Autodesk.AutoCAD.Interop.AcadPreferences."

I know I can have it also reading the registry, but I wanted to know if this problem is knowed and if ther're solutions.
Thanks.
« Last Edit: June 03, 2009, 02:23:54 AM by gile »
Speaking English as a French Frog

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Re: cast error with COM Objet and A2010
« Reply #1 on: June 03, 2009, 02:34:36 AM »
an alternative method might be to p/invoke acedGetEnv, I think there are a few examples around

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Re: cast error with COM Objet and A2010
« Reply #2 on: June 03, 2009, 02:38:00 AM »
there is also a similar thread , http://www.theswamp.org/index.php?topic=20237.0

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: cast error with COM Objet and A2010
« Reply #3 on: June 03, 2009, 02:51:13 AM »
Thanks Daniel

p/invoke methods are still a little over my head, but I'll try.
Speaking English as a French Frog

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Re: cast error with COM Objet and A2010
« Reply #4 on: June 03, 2009, 02:55:29 AM »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: cast error with COM Objet and A2010
« Reply #5 on: June 03, 2009, 03:02:11 AM »
Yes, it seems to be a nice way too.
Thanks again.
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: cast error with COM Objet and A2010
« Reply #6 on: June 03, 2009, 03:18:24 AM »
What is this Master stuff Daniel ...  :-D

I'm just an old Lisp hacker trying to keep up.

///kdub

ps: but thanks for the thought :)
« Last Edit: June 03, 2009, 03:24:41 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: cast error with COM Objet and A2010
« Reply #7 on: June 03, 2009, 07:22:45 AM »
Another question to the 'masters'

Have I to dispose the IConfigurationSection object got with OpenCurrentProfile() ?

Here's how I wrote the method, is it the good way ?

Code: [Select]
private void helpButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (IConfigurationSection profile = AcApp.UserConfigurationManager.OpenCurrentProfile())
                {
                    string acadProp =(string)profile.OpenSubsection("General").ReadProperty("ACAD", string.Empty);
                    string[] pathes = acadProp.Split(new char[] { ';' });
                    string fullName = "";
                    for (int i = 0; fullName == "" && i != pathes.Length; i++)
                    {
                        string path = pathes[i];
                        if (path != "")
                        {
                            string[] files = Directory.GetFiles(path, "cadnormh.chm");
                            if (files.Length > 0)
                                fullName = files[0];
                        }
                    }
                    if (fullName != "")
                        Help.ShowHelp(this, fullName);
                    else
                        ed.WriteMessage("Le fichier \"CADnormH.chm\" est introuvable.\n");
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
            }
        }
Speaking English as a French Frog

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8712
  • AKA Daniel
Re: cast error with COM Objet and A2010
« Reply #8 on: June 03, 2009, 08:59:18 AM »
personally, I don't think you need to call dispose since IConfigurationSection is just an interface class..
but if you want to be on the safe side, do it that way you have it.
just a note, IMO you should initialize empty strings with string.empty and use string.IsNullOrEmpty() to test against null or empty strings  :-)

Spike Wilbury

  • Guest
Re: cast error with COM Objet and A2010
« Reply #9 on: June 03, 2009, 10:24:55 AM »
I need to get the support search pathes.

Hi Gile,

If you just want to extract the paths, you can use ACADPREFIX system variable - maybe.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: cast error with COM Objet and A2010
« Reply #10 on: June 03, 2009, 10:38:04 AM »
Thanks again Daniel
I thaught I had to dispose because the interface is disposable.

LE,
You're the man !
Sometimes, I can't see my nose in the middle of my face :oops:
Thanks !
Speaking English as a French Frog

Spike Wilbury

  • Guest
Re: cast error with COM Objet and A2010
« Reply #11 on: June 03, 2009, 11:22:18 AM »
LE,
You're the man !
Sometimes, I can't see my nose in the middle of my face :oops:
Thanks !

:)

Merci, tu es le bienvenu

Glenn R

  • Guest
Re: cast error with COM Objet and A2010
« Reply #12 on: June 03, 2009, 11:34:34 AM »
From memory, this will not pick up any changes made during the current session, as the registry is written on AutoCAD shutdown...at least that's what I recall anyway...

Spike Wilbury

  • Guest
Re: cast error with COM Objet and A2010
« Reply #13 on: June 03, 2009, 12:36:52 PM »
Hi Gile,

I normally find my file depencies, based on the location of my application, maybe if you use something like:

Code: [Select]
public string GetAppPath()
{
    System.Reflection.Module[] modules = System.Reflection.Assembly.GetExecutingAssembly().GetModules();
    string aPath = System.IO.Path.GetDirectoryName(modules[0].FullyQualifiedName);
    if ((aPath != "") && (aPath[aPath.Length - 1] != '\\'))
    {
        aPath += '\\';
    }
    return aPath;
}

for example:

Command: APPATH
C:\Programming\C#\BlockManager\bin\Debug\

string file = "cadnormh.chm";
string lookFor = appPath + file;

« Last Edit: June 03, 2009, 12:42:05 PM by LE »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: cast error with COM Objet and A2010
« Reply #14 on: June 03, 2009, 01:10:53 PM »
Muchas gracias Luis

Most of the time the help file should be in the same folder as the dll, but if the user has moved it to another folder in the search path, I'd rather use something like the findfile LISP function.

Your snippet will be usefull for a newby like me.
Speaking English as a French Frog