Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Assign a name to a string C# (Process.GetCurrentProcess().MainWindowTitle)

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
rita.aguiar
2170 Views, 12 Replies

Assign a name to a string C# (Process.GetCurrentProcess().MainWindowTitle)

I am creating a plugin for Revit that registers several events within its application.

For every time an event happens, a line is writen on a txt file telling me about the event such as:

The user opened a document on Autodesk Revit 2019 (...)

I am obtaining the "Autodesk Revit 2019" (name of application) by getting the name of the MainWindowTitle of the application like so: Process.GetCurrentProcess().MainWindowTitle

I would like to attribute a name to this string (although I know that this is not exacly a string, but that it gets some characters that spell words).

 

I have been trying:

 

public static string originalString = Process.GetCurrentProcess().MainWindowTitle;

 

(...)

 

Trace.WriteLine("O utilizador " + Environment.UserName + " abriu um documento no " + originalString + " a " + DateTime.Now + " (DocumentOpenedEventArgs)");

 

Which writes in the txt file:

 

O utilizador rita.aguiar abriu o  a 20/09/2018 10:36:42 (ApplicationInitializedEventArgs)

 

See attached pngs for more information.

As you can read, it did not write on the txt file "Autodesk Revit 2019 - [Home]" as I hoped for.

 

If I had writen Process.GetCurrentProcess().MainWindowTitle directly on the Trace.WriteLine I would have obtained "Autodesk Revit 2019 - [Home]", but I wish to write an assigned name instead.

 

How to successfully write "Autodesk Revit 2019 - [Home]" by assigning a name to Process.GetCurrentProcess().MainWindowTitle?


Later I would like to obtain this name by insted getting just Autodesk Revit 2019 like so:


public static string originalString = Process.GetCurrentProcess().MainWindowTitle;

public static string[] splittedString = originalString.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

public static string AppName = splittedString[0];

 

Any help would be appretiated!

12 REPLIES 12
Message 2 of 13
jeremytammik
in reply to: rita.aguiar

Sorry, your code is unreadable in the screen snapshots. How about pasting the relevant code text instead, using the <i> button above?

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 13
rita.aguiar
in reply to: jeremytammik

Thank you.

 

The relevant code is:

public static string originalString = Process.GetCurrentProcess().MainWindowTitle;

The originalString name is being used here:

Trace.WriteLine("O utilizador " + Environment.UserName + " abriu um documento no " + originalString + " a " + DateTime.Now);


Which writes on the .txt file:

O utilizador rita.aguiar abriu o  a 20/09/2018 10:36:42

 

As you can read, it did not write "Autodesk Revit 2019 - [Home]" (i.e. the MainWindowTitle) between "o" and "a" on the .txt file as I hoped for.


Message 4 of 13
jeremytammik
in reply to: rita.aguiar

Weird.

 

Look at it step by step in the debugger.

 

Then you can see for yourself exactly what is going on.

 

You could also add some more intermediate lines and variables for absolute clarity:

 

string originalString = Process
  .GetCurrentProcess()
  .MainWindowTitle;

string s2 = "O utilizador " 
  + Environment.UserName 
  + " abriu um documento no " 
  + originalString + " a " + DateTime.Now;

Trace.WriteLine( s2 );

 

The debugger is good!

 

Invaluable, in fact.

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 13
rita.aguiar
in reply to: jeremytammik

Thank you for your help.

 

When I use the debug mode to run the app (before setting any breakpoints yet) nothing is being written on the .txt file as I hoped for, so I can't be sure if the app is working this way.

If I run debug mode with a breakpoint on the Trace.WriteLine I don't get an yellow arrow to step into this code, instead I get a warning saying "The breakpoint will not currently be hit. No symbols have been loaded for this document."

I looked at:
https://stackoverflow.com/questions/2155930/how-do-i-remedy-the-the-breakpoint-will-not-currently-be...
and realized that no Module has loaded, they all say "Skipped loading" in the Symbol Status column

 

 

Message 6 of 13
rita.aguiar
in reply to: rita.aguiar

The problem is that

 

Process.GetCurrentProcess().MainWindowTitle

retrieves an empty string ""

 

https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.mainwindowtitle?redirectedfro...

 

"A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window (so that MainWindowHandle is zero), MainWindowTitle is an empty string (""). If you have just started a process and want to use its main window title, consider using the WaitForInputIdle method to allow the process to finish starting, ensuring that the main window handle has been created. Otherwise, the system throws an exception."

 

Message 7 of 13
stever66
in reply to: rita.aguiar

Try:

 

Process.GetCurrentProcess().MainWindowTitle.ToString();

 

or maybe .AsString();

 

 

Message 8 of 13
rita.aguiar
in reply to: stever66

Thank you @stever66

But the MainWindowTitle retrives an empty string, so converting it to string retrives an empty string.

Message 9 of 13
Revitalizer
in reply to: rita.aguiar

Hi,

 

due to the new docking system introduced in Revit 2019, there is a MainWindowHandle Property in both UIApplication and UIControlledApplication classes.

It says in RevitAPI.chm:

 

"Returns the main window handle of the Revit application. This handle should be used when displaying modal dialogs and message windows to insure that they are properly parented. This property replaces System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle property, which is no longer a reliable method of retrieving the main window handle starting with Revit 2019."

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 10 of 13
Revitalizer
in reply to: Revitalizer

To get a text from an IntPtr window handle:

 

https://www.pinvoke.net/default.aspx/user32/getwindowtext.html?diff=y

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 11 of 13
stever66
in reply to: Revitalizer

Revitalizer:

 

Hopefully Rita can follow your comments, but you have lost me. 

 

I see that the MainWindowHandle has been added to the UIApplication and UIControlledApplication, but I can't seem to figure out exactly how to use it.

 

I looked at your link, but there is so much there,  I'm not sure if all of it applies or just some part?

 

I think this is all above my understanding of the Revit API.  But even in 2019 I still get:

 

TaskDialog.Show("Revit Window", Process.GetCurrentProcess().MainWindowTitle.ToString());

 

to run, and it gives me the name of the Revit app (Revit MEP 2019), the central file name, and the name of the viewport I'm in.   Even if I run it before opening a file, I get:

 

"Autodesk Revit 2019 - {Recent Files]".

 

So I'm not sure why it would ever return an empty string.

 

 

 

 

 

 

Message 12 of 13
Revitalizer
in reply to: stever66

Hi,

 

in Revit 2019, if view windows are pulled off the main window, there may be more than one Revit application window.

 

I think if you open the views in just one single Revit 2019 window, of course the 2018 code will function since it just finds the only one.

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





Message 13 of 13
rita.aguiar
in reply to: Revitalizer

I forgot to write a solution, but better late than never:

I was trying to obtain Revit's window title before the plugin got the chance to launch and completely open the Revit window.

The solution I found was to first instantiate the variables I need to obtain the window title:

string originalString;
string[] splittedString;
string AppName;

And later define these variables inside a method which will be used when these names are required.

originalString = Process.GetCurrentProcess().MainWindowTitle;
splittedString = originalString.Split(new[] { " -" }, StringSplitOptions.RemoveEmptyEntries);
AppName = splittedString[0];

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community