With the foundation of WorldWind Series 1, we can now debug and run it normally! Let's take a look at the features of the software so that we can know what features WorldWind has to learn.
Start our "WorldWind Learning Series 2: capture the thief first capture the King" to analyze the WorldWind main form and learn from the main function portal step by step. At least for me, there is a lot of knowledge to learn. (Supplement: The WorldWind. CS form design interface cannot be accessed. I have discovered this problem for a long time, but it cannot be solved. Let's look at it based on the function.CodeRight)
1. Use System. version to read the software version information and format the output. We configure the software version outside, and the version is automatically changed in the "about" section.
Get formatted version number
// Establish the version number string used for User display,
// Such as the splash and help-> about screens.
// To change the application. productversion make
// Changes in \ WorldWind \ assemblyinfo. CS
// For Alpha/beta versions, include "alphan" or "betan"
// At the end of the format string.
Version ver = New System. Version (application. productversion );
Release = String . Format ( " {0}. {1}. {2}. {3} " , Ver. Major, Ver. Minor, Ver. Build, Ver. Revision );
2. Check whether the software has an instance started. If yes, it will not start.
Determine whether an instance has been started
// If world wind is already running, pass any CommandLine
// Arguments from this instance, and quit.
Intptr handle = Getwwhandle (); // Obtain the thread pointer.
If ( ! System. intptr. Zero. Equals (handle ))
{
If (ARGs. Length > 0 )
Nativemethods. sendargs (handle, String . Join ( " \ N " , ArgS ));
Return ;
}
The getwwhandle function is parsed by obtaining the thread pointer.
Public Static Intptr getwwhandle ()
{
Return Nativemethods. findwindow ( Null , " NASA World Wind " );
}
It can be seen that it calls the findwindow method of the nativemethods class, which is essentially a reference to the unmanaged function findwindow of user32.dll. That is, getwwhandle () calls the unmanaged function findwindow of user32.dll.
Unmanaged function calls
/// <Summary>
/// API function to find Window Based on windowname and class.
/// </Summary>
[Dllimport ( " User32.dll " )]
Internal Static Extern Intptr findwindow ( String Lpclassname, String Lpwindowname );
See my platform call for reference: C # use an unmanaged DLL Function
Note: You can use this method to determine whether an instance has been started. In the past, I used to write code to determine whether a process exists and whether an instance was started.
Determine whether the program process already exists
/// <Summary>
/// Determine whether this existsProgramProcess
/// </Summary>
/// <Returns> </returns>
Private Static Bool Isexistprocess ()
{
Bool Result = False ;
Process [] P = Process. getprocessesbyname (path. getfilenamewithoutextension (application. executablepath ));
If (P ! = Null && P. Length > 1 )
{
Result = True ;
}
ReturnResult;
}
3. determine the number of protocols that already exist in the computer. It mainly targets bugs in. NET Framework 1.1 (more than 50 protocols are not allowed ). Because the software needs to enable its ownNew Protocol "WorldWind ://".The problem does not exist after 2.0.
// Abort if 50 bindings problem present and user opts to go To the download page
If (bindingscheck. polictybindingswarning () return;
Although we can say that this function can be avoided, the knowledge points in its functions are worth looking. Inclutybindingswarning () mainly includes determineprotocolbindings () and isbindingshotfixapplied (). The first is the number of binding protocols, and the other is the CLR version number.
Static Int Determineprotocolbindings ()
{
Int Errornumber = 0 ;
Const Long Sizeofoneprotocolinfostructure = 628 ; // Here we can see that the structure length of the protocol information is fixed to 628
Long Sizeofallprotocolinfostructures = 0 ;
// Request all protocol info structures, but provide no space to store them-
// This will yield the total size of the structure array in bytes
Wscenumprotocols (intptr. Zero, intptr. Zero, Ref Sizeofallprotocolinfostructures, Ref Errornumber );
If (Errornumber ! = Wsaenobufs) Return - 1 ; // Not what we expected, unable to determine Number of bindings
//Divide total size by size of one structure to get number of bindings.
Return(Int) (Sizeofallprotocolinfostructures/Sizeofoneprotocolinfostructure );
}
// Determines whether the CLR version is newer than the first one that has Ded the fix.
Static Bool Isbindingshotfixapplied ()
{
// Check if CLR version is 1.1.4322.946 or higher
Return System. environment. Version > = New Version ( " 1.1.4322.946 " ); // System. environment. version is used to read the version number of the system environment. }
The previous function determineprotocolbindings () also calls an external unmanaged code function. As follows:
[Dllimport ("ws2_32.dll")]
Static extern int wscenumprotocols (intptr lpiprotocols, intptr lpprotocolbuffer, ref long lpdwbufferlength, ref int lperrno );
I will write this article first today. I have analyzed it too deeply. If I write this article, I will write this series into 100 articles. For the purpose of application, the analysis is not too thorough. In the future series, I will simplify some content through outline guidance, and I will pay attention to key knowledge points.