Retrieving ExecuteCommand results

M

Mario Vázquez

Hi,

I've wrote the following lines of code to open the "Add Connection" dialog
of the IDE at design time.

EnvDTE.DTE d = (EnvDTE.DTE)this.Site.GetService(typeof(EnvDTE.DTE));

// Find first the path of the current project (there is a more elegant way?)
object[] o = (object[])d.ActiveSolutionProjects;
EnvDTE.Project prj = (EnvDTE.Project)o[0];
string prjPath = prj.FullName;
prjPath = prjPath.Substring( 0, prjPath.LastIndexOf("\\") + 1 );

if (this.DesignMode && !System.IO.File.Exists( prjPath + "App.Config" ))
throw new Exception( "App.config not found." ); }

// Open the create connection dialog.
d.ExecuteCommand( "Tools.ConnectToDataBase", "" );

It works nice but, I don't know how to achieve the connection string created
by the user :S
How to do it?

I don't have either any idea of which arguments are supposed to be passed to
the ExecuteCommand method, because help doesn't help too much...
Where can I found documentation about the DTE?

Thanks,
Mvr
 
C

Carlos J. Quintero [VB MVP]

Hi Mario,

I don't think that's possible because although the IDTCommandTarget.Exec
method allows to output results, DTE.ExecuteCommand seems to be input only,
if any.

You have resources about DTE on my web site (below). See specially the new
book about VS 2005 of Craig Skibo, free for MSDN users. Maybe there is
something there.
--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
M

Mario Vázquez

Hi Carlos,

Thanks for repply.

I wonder why it has to be so difficult to find information about these
interesting things...
I've look for IDTCommandTarget interface in the MSDN and, as ussual, there
are not samples about how implement it.
I've tried to get a copy of the "Working with Visual Studio 2005" book you
recommended in this newsgroup too, but finally I gave up.
Well, I hope someday someone put a copy to share somewhere...
Meanwhile, I'll try to find something about it in http://www.mztools.com

Thanks anyway.
Mario Vazquez



Carlos J. Quintero said:
Hi Mario,

I don't think that's possible because although the IDTCommandTarget.Exec
method allows to output results, DTE.ExecuteCommand seems to be input
only, if any.

You have resources about DTE on my web site (below). See specially the new
book about VS 2005 of Craig Skibo, free for MSDN users. Maybe there is
something there.
--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


Mario Vázquez said:
Hi,

I've wrote the following lines of code to open the "Add Connection"
dialog
of the IDE at design time.

EnvDTE.DTE d = (EnvDTE.DTE)this.Site.GetService(typeof(EnvDTE.DTE));

// Find first the path of the current project (there is a more elegant
way?)
object[] o = (object[])d.ActiveSolutionProjects;
EnvDTE.Project prj = (EnvDTE.Project)o[0];
string prjPath = prj.FullName;
prjPath = prjPath.Substring( 0, prjPath.LastIndexOf("\\") + 1 );

if (this.DesignMode && !System.IO.File.Exists( prjPath + "App.Config" ))
throw new Exception( "App.config not found." ); }

// Open the create connection dialog.
d.ExecuteCommand( "Tools.ConnectToDataBase", "" );

It works nice but, I don't know how to achieve the connection string
created
by the user :S
How to do it?

I don't have either any idea of which arguments are supposed to be passed
to
the ExecuteCommand method, because help doesn't help too much...
Where can I found documentation about the DTE?

Thanks,
Mvr
 
C

Carlos J. Quintero [VB MVP]

Hi Mario,
I wonder why it has to be so difficult to find information about these
interesting things...

That's life...
I've look for IDTCommandTarget interface in the MSDN and, as ussual, there
are not samples about how implement it.

The commands are intended only to be invoked and forget about them, so this
approach won´t work.
I've tried to get a copy of the "Working with Visual Studio 2005" book you
recommended in this newsgroup too, but finally I gave up.
Well, I hope someday someone put a copy to share somewhere...

It is illegal to put this book to share, but if you register VS 2005 using
the Help\Register... menu, you receive an e-mail with a link and after
logging in you can download the book. Why did you give up?

That said, the book does not contain info about your problem, I read two
chapters per day and I read yesterday the chapter about commands.

So, you need a radical different approach: the "Connect to database" dialog
is new in VS 2005, and all new things are written in managed code. You can
use a tool like Reflector for .NET (http://www.aisto.com/roeder/dotnet/) and
then try to locate the VS assembly that contains the code for that dialog.
You must search in VS folders or in the GAC, because some VS assemblies are
only in the GAC. Once you have located the assembly and the class used for
that dialog, maybe you can create an instance of it or at the very least
learn about how it is implemented.... all of this at your own risk.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
M

Mario Vázquez

Hi Carlos,

I wrote you after more than an hour of tryin in vane to download the desired
book, after registering. So I was quite despaired.
I've received in my Inbox two messages from Microsoft confirming my
subscription and I followed the link they sent me but I didn't be able to
dowload
the book.
Now I've seen that you have replied to my message, and before write you,
I've tried again.
And great! I've got it! I don't know what i was doing wrong. I feel much
better.

Well, I don't understand why the IDE lets me open an enviroment form (such
as "Connect to database") if does not give me any mechanism to obtain the
results of it. Explore the assembly it sounds quite hard... but I'll try.

Thanks a lot for your patience.

Regards,
Mario Vazquez
 
C

Carlos J. Quintero [VB MVP]

Hi Mario,

Don´t give up, you never know when it will succeed :)

There were some problems with the Connect logon page for some people, but
either it works now or you can contact them to get it fixed. I'm glad that
you have now the book.

About the other question, again, don´t give up. I have made very difficult
things in my add-in (much more than showing a dialog), struggling for whole
weeks, and the Reflector tool helped me a lot to understand some internal
behaviors of the .NET Framework and the VS IDE. It is a very friendly tool,
just configure it to show the code in your language of choice, load some
assembly, explore it and relax while you see all that clear code...

On the other hand, another approach, to build your own connect string dialog
should not be that difficult in .NET 2.0, where you have new classes for
connect string builders, etc.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
M

Mario Vázquez

Hi Carlos,

I'm being searching with Reflector the ExecuteCommand() method in the
EnvDte.dll assembly and I found that it's an external method :/
Where it's supposed to be that code? What is the GAC?

Anyway, it seems to be the hard way... althogh I think that I'd have to use
Reflector more often than I do, if I want to do this kind of things.
On the other hand, another approach, to build your own connect string
dialog should not be that difficult in .NET 2.0, where you have new
classes for connect string builders, etc.

Well, this sounds more easy. I've found the ConnectionStringEditor class. Is
it one of these "connect string builders" you reffer?

Again, thanks a lot for your help.
(You seem to be the only soul in this desert....)

Regards,
Mario Vazquez
 
C

Carlos J. Quintero [VB MVP]

Hi Mario,
What is the GAC?

You must be kidding about this one, right? ;-) If not, GAC is the Global
Assembly Cache, read the .NET docs about this.
Well, this sounds more easy. I've found the ConnectionStringEditor class.
Is it one of these "connect string builders" you reffer?


I have not used them, I only know that they exist in .NET 2.0 but good
Anyway, it seems to be the hard way...

Not so hard when you know where to look, it took a minute to find it:

- Use Reflector for .NET to open the assembly C:\Program Files\Microsoft
Visual Studio 8\Common7\IDE\Microsoft.VisualStudio.DataTools.dll
- Expand the Microsoft.VisualStudio.DataTools.DataServices and you have:

* The DataConnection class
* The DataConnectionDialog, which has an interesting method:

Public Overrides Function ShowDialog(ByVal connect As Boolean) As
DataConnection

which is what you are looking for. Notice that the DataConnectionDialog
class is not public, so you will need to use Reflection to get the type and
then create and instance of it using the Activator class. Notice that all
this is very tricky, so you may want to use your own dialog.

It's time to go home in this part of the world....


--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
M

Mario Vázquez

Hi Carlos,
(from the same part of the world as yours -I'm from Gerona-)

I keep on trying to open the DataConnectionDialog using reflection, as the
first step of your suggestions.
....but unsuccessfully, at the moment

I'm trying this (among many other things):

First I've added a reference to the
Microsoft.VisualStudio.DataTools.DataServices assembly on my components
project.

Type t;
object obj;

t =
Type.GetType("Microsoft.VisualStudio.DataTools.DataServices.DataConnectionDialog");
obj = Activator.CreateInstance( t, false);
obj.GetType().InvokeMember( "ShowDialog", BindingFlags.InvokeMethod |
BindingFlags.Default | BindingFlags.Instance, null, obj, new object[] { });

and I receive the following exception message: Method
Microsoft.VisualStudio.DataTools.DataServices.DataConnectionDialog not
found,
although the type t seems to be well suited when debugging.
:-S

I've tried something like this too:

Type t =
Type.GetType("Microsoft.VisualStudio.DataTools.DataServices.DataConnectionDialog");
object obj;
obj = Activator.CreateInstance( t );
obj.GetType().InvokeMember("ShowDialog", BindingFlags.Default |
BindingFlags.InvokeMethod, null, obj, new object[] {false});

but with no better results...

Well, I have to practice more with reflection.

There's another thing I'd like to comment you.
You said that the good thing it would be to create my own dialog based on
the things learned using Reflector (I'm agree with it). But all the code I
see using that tool is a quite long list of declarations with no code
anywhere.
What is supposed to do with all this?

Thanks for all.
Mario Vazquez
 
C

Carlos J. Quintero [VB MVP]

Mario Vázquez said:
Hi Carlos,
(from the same part of the world as yours -I'm from Gerona-)

Ah, good to know...
I keep on trying to open the DataConnectionDialog using reflection, as the
first step of your suggestions.
...but unsuccessfully, at the moment

The correct code (VB.NET, adapt it to C#) is:

Dim objAssembly As System.Reflection.Assembly
Dim objType As Type
Dim objDataConnectionDialog As Object
Dim objResult As Object

objAssembly = System.Reflection.Assembly.LoadFrom("C:\Archivos de
programa\Microsoft Visual Studio
8\Common7\IDE\Microsoft.VisualStudio.DataTools.dll")

objType =
objAssembly.GetType("Microsoft.VisualStudio.DataTools.DataServices.DataConnectionDialog")

objDataConnectionDialog = System.Activator.CreateInstance(objType)

objResult = objType.InvokeMember("ShowDialog",
Reflection.BindingFlags.InvokeMethod Or Reflection.BindingFlags.Instance Or
Reflection.BindingFlags.Public, Nothing, objDataConnectionDialog, Nothing)

However, you will get an Unexpected exception about the IUIService or
something like that. It is because the dialog expects to be hosted inside
VS, which provides some services to do it. It will be difficult to solve
this.
You said that the good thing it would be to create my own dialog based on
the things learned using Reflector (I'm agree with it). But all the code I
see using that tool is a quite long list of declarations with no code
anywhere.
What is supposed to do with all this?

When you right-click in a method in the treeview to the left, there is a
context menu "Disassemble" which shows the code in a pane to the right. You
can change the .NET language of the output code in the options window.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
M

Mario Vázquez

Hi Carlos,
The correct code (VB.NET, adapt it to C#) is:

Dim objAssembly As System.Reflection.Assembly
Dim objType As Type
Dim objDataConnectionDialog As Object
Dim objResult As Object

objAssembly = System.Reflection.Assembly.LoadFrom("C:\Archivos de
programa\Microsoft Visual Studio
8\Common7\IDE\Microsoft.VisualStudio.DataTools.dll")

objType =
objAssembly.GetType("Microsoft.VisualStudio.DataTools.DataServices.DataConnectionDialog")

objDataConnectionDialog = System.Activator.CreateInstance(objType)

objResult = objType.InvokeMember("ShowDialog",
Reflection.BindingFlags.InvokeMethod Or Reflection.BindingFlags.Instance
Or Reflection.BindingFlags.Public, Nothing, objDataConnectionDialog,
Nothing)

That's more or less just what I've tried... and still does'nt work.
I recieve an "Exception has been thrown by the target of an invocation".
I'm trying to open this dialog from an UITypeEditor form. I don't know if
this could be the cause. Or maybe the fact that the DataConnectionDialog is
an internal class. It is possible to instantiate internal classes with
reflection?

Anyway, for me the interesting thing of all of this is take advantage of the
code that Reflector shows...
When you right-click in a method in the treeview to the left, there is a
context menu "Disassemble" which shows the code in a pane to the right.
You can change the .NET language of the output code in the options window.

Oh yes, I know it. But this was just my question. When I do this with the
DataConnectionDialog I don't get much more than some quite strange
declarations and I don't know how to profit by it.

Another thing is rounding my mind is how you found the DataConnectionDialog
among so large list of types... I've seen that Reflector has an option to
search types by its name, but, is there any way to do it if I don't know the
name of the class I'm looking for? For example, suppose that I would like to
show the Font dialog in another place than the property browser (it's only
an example). Is there a direct way to know what's the name of that control?
or I have to look for something which name sounds like something similar?

Sorry for so much questions...

Regards,
Mario Vazquez
 
C

Carlos J. Quintero [VB MVP]

Mario Vázquez said:
I'm trying to open this dialog from an UITypeEditor form. I don't know if
this could be the cause.

Then try first from a Windows Form application in VB.NET (my sample), then
move to a C# Windows Application and then to a C# UITypeEditor.
Or maybe the fact that the DataConnectionDialog is an internal class. It
is possible to instantiate internal classes with reflection?

Yes, it is.
Oh yes, I know it. But this was just my question. When I do this with the
DataConnectionDialog I don't get much more than some quite strange
declarations and I don't know how to profit by it.

When you right-click a class and disassemble it, you get the list of
functions with no code. You must right-click and disassemble each function
in the treeview to get its code. There are also add-ins for Reflector (on
their web site) that allows you to disassemble a whole assembly to a file
and you can see the whole class code.
Another thing is rounding my mind is how you found the
DataConnectionDialog among so large list of types...

First I found the assembly (I was lucky to find it quickly), loaded it and
the browsed its classes. It was pretty obvious that something named
DataConnectionDialog was what we were looking for...
I've seen that Reflector has an option to search types by its name, but,
is there any way to do it if I don't know the name of the class I'm
looking for? For example, suppose that I would like to show the Font
dialog in another place than the property browser (it's only an example).
Is there a direct way to know what's the name of that control? or I have
to look for something which name sounds like something similar?

You have to browse the names and guess... I have spent long time browsing
assemblies using Reflector...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top