Submit MS Application Development Questions to Computer Education Techniques CETi

Submit MS Application Development Questions to Computer Education Techniques CETi

Submit MS Application Development Questions


Submit MS Applications Development Questions

Technology Update

Knowledge Base

Submit MS Application Development Questions to Computer Education Techniques CETi Advanced search

Interrelated Information Technology

MS Visual Basic Questions MS Visual C++ Questions
MS Operating Systems and Networks Questions .NET Questions
Java Questions Computer Applications Questions

The Computer Education Techniques knowledge base is a service for answering questions, inclusive of the research and validation of the accuracy of information in the public domain. Citation of source documentation and examples are used to provide answers to the questions. Utilization of the information of this service and reliance on the answers, information or other materials received through this web site is done at your own risk.

MS Visual Basic

Q I am responsible for maintaining Visual Basic code. Is it possible to modify the VB code to:

1 - Run a command line application.
2 - Open up another Windows program.
3 - Bring up the default web browser or email program.

A Yes, all three can be implemented programmatically.

Sample Code:
The classes and methods in System.Diagnostics.Process can be used to accomplish these tasks.

  Private Sub Button1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
  End Sub

Sample Code:
This code will retrieve the results and wait until the process stops.

  Private Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
      Dim psi As New _
psi.RedirectStandardOutput = True
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.UseShellExecute = False
      Dim listFiles As System.Diagnostics.Process
listFiles = System.Diagnostics.Process.Start(psi)
      Dim myOutput As System.IO.StreamReader _
        = listFiles.StandardOutput
      If listFiles.HasExited Then
        Dim output As String = myOutput.ReadToEnd
      End If
  End Sub

Q How can a form or dialog be centered programmatically on the screen?
A Use the following code snippet in order to center a form or dialog programmatically on the screen:

   Private Sub Form_Load()
        Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
   End Sub

Q How can version information be returned from the AssemblyInfo file?
A Use the following VB .NET code at run-time in order to return version information.

Sample Code:

    Function GetVersion() As String
    With System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly.Location)
    Return .FileMajorPart & "." & .FileMinorPart & "." & .FileBuildPart & "." & .FilePrivatePart
    End With
    End Function

    Function GetComments() As String
    With System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly.Location)
    Return .Comments
    End With
    End Function

Q How is text converted from lower case to upper case in VB.NET?

Use the following code for programmatically converting text from lower to upper case.

Sample Code:

    Module Module1
    Sub Main ()
            Dim str1 as String=”Welcome to String”
            Dim str2 as String
    End Sub

Q How are Procedure Delegates created?
A Delegates are used to work with the address of procedures. Sometimes it will be useful to pass the location of a procedure to other procedures.  Delegates are used to display “Hello from Delegates”.
  Sample Code:

  Module Module1
  Delegate Sub SubDelegate1(ByVal str as String)
  Sub Main()
        Dim Mess as SubDelegate1
        Mess-AddressOf Display
        Mess.Invoke(“Hello from Delegates”)
  End Sub
  Sub Display(ByVal str as String)
  End Sub
  End Module

Q What is anchoring and docking?
A In Visual Basic, the terms docking and anchoring are used to ensure that a control covers the whole client area of a form.
Docking When docked a window adheres to the edges of its container. The property of the control from the windows can be used to "dock" a particular control. Selecting the Dock property opens up a small window like structure from which a selection can be made on the Form to control the docked state.
Anchoring An anchor places a control to the edges of its container. Selecting this property in the properties window opens up a small window from to select the edge for anchoring the control.

Q How can the computer name and IP address be programmatically obtained?
A The following code uses the System.NET.DNS namespace to access the "computer name" and it's IP address.

  Sample Code

  Option Strict Off
  Imports system
  Imports System.Net.DNS
  Public Class GetIP
  Shared Function GetIPAddress() As String
  Dim sam As System.Net.IPAddress
  Dim sam1 As String
  With system.Net.DNS.GetHostByName(system.Net.DNS.GetHostName())
      sam = New System.Net.IPAddress(.AddressList(0).Address)
      sam1 = sam.ToString
  End With
  GetIPAddress = sam1
  End Function
      Shared Sub main()
          Dim shostname As String
          shostname = system.Net.DNS.GetHostName
          console.writeline("Name of the System is = " & shostname)
          console.writeline("Your IP address is= " & GetIPAddress)
      End Sub
  End Class

Q What is managed code and managed data?
A Managed code is the code that is written to target the services of the CLR: Common Language Runtime. In order to target these services, the code must provide a minimum level of information to the runtime. All of the C#, Visual Basic .NET and J# .NET code is managed by default.

Managed data is allocated and de-allocated by the Common Language Runtime's garbage collector. C#, Visual Basic, and J#.NET data is managed by default. C++ data can be marked as unmanaged through the use of special keywords. Visual Studio .NET C++ data is unmanaged by default; this occurs even when the /CLR switch is used. When Managed Extensions for C++ are being used, a class can be marked as managed by using the __gc keyword.

With Managed data, the memory used for instances of the class is managed by the garbage collector. The class also becomes a full participating member of the .NET Framework; a major is proper interoperability with classes written in other languages.

A managed class can only inherit from one base class.

Q How can an .EXE be run from a VB.NET application?
A In order to run a .EXE file from a VB.NET application, it will be necessary to import the System.Diagnostics namespace.
The following code uses is used run Notepad from a VB.NET application.

Sample Code:

  Imports System
  Imports System.Diagnostics
  Dim program As New Process()
  program.StartInfo.FileName = "Notepad.exe"
  program.StartInfo.Arguments = " "

MS Visual C++

Q What is a buffer overrun? How can I protect my organization from buffer overruns?
A Buffer overruns are a programming mistake; they involve copying the contents of a region of memory into another region of memory that is too small to accommodate the source block.

  char* source = "A reasonably long string";
  char dest[10];
  ::strcpy(dest, source);

It can be extremely difficult to eliminate buffer overruns in large C/C++ code bases One approach is to limit the size of the data copied in order that it is not greater than the size of the destination buffer.

The utilization of managing technologies such as .NET and Java can significantly reduce the potential for buffer overruns. Buffer protection can be activated by turning on the /GS compiler switch. With Visual Studio, the switch can be activated from the Code Generation option page on the C/C++ tab. By default, the setting will be disabled for the Debug configuration and enabled for the Release configuration.

However, a single compiler switch will not ensure an application is secure. It can help make an application more secure, but security vulnerabilities come in many different forms. Buffer overruns are a security vulnerability. A malicious input can modify the execution flow of a process. Buffer overruns can result in an entire process, machine, or domain being compromised. If the identity that the process is running under is a highly trusted account, such as an administrator or the Local System account, the damage that the hackers can cause is severe and potentially widespread.

Code Red and Blaster worms, were the result of buffer overruns in C/C++ code.

Q Can a WinHelp be called from a C++ MFC program?
  Help can be programmatically called from anywhere in code. The most common reasons for doing this would be to open the WinHelp Table of Contents from the help menu or to call a general help topic from a Help button on a form. Help can also be called using keywords.

The WinHelp function starts WINHLP32.EXE and passes additional data indicating the nature of the help requested by the application.

In order to use the following API calls, the WinHelp.bas or Winhelp.cls must be attached.
BOOL WinHelp( HWND hWndMain, LPCTSTR lpszHelp, UINT uCommand, DWORD dwData )

  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero.

Q What is a view? How is it different from an application?
A When an application is created, the intention will be for the users to see, view, and manipulate its documents. The process of making up a program is to create the application using a class derived from CWinApp and surrounding the program with a frame either based on CFrameWnd or from one of its derived classes. This type of a simple program is designed to show the presence of an application. In order to assist the user with files, the program should be equipped with a document. A document is a representation of a file and in order to present a document to the user, an object called a view, will need to be provided.

There are several ways to present documents. In order to support these variances, the MFC provides different view-based classes. MFC provides a class named CView, for laying a foundation for those classes. Although the MFC provides various view-based classes, there is a high likelihood, that none of them would fit the particular functionality that is required to be implemented.

For this reason, the CView class is:

1 - The ancestor to the other view-based classes,
2 - A view in its own right and can be used to create an application that none of the other classes can handle.

CView is an abstract class; it cannot be declared a CView variable.

The following code will produce an error.

   CView *view = new CView;

In order to use CView, a class must be derived from it.

Q How can a window be made not movable?
A This can be done by adding the handle function to the WM_WINDOWPOSCHANGING message for the window and write next:

   void CMyDialog::OnWindowPosChanging(WINDOWPOS* pWndPos)
           if (IsWindowVisible())
                      pWndPos->flags |= SWP_NOMOVE;

Q What is better: AfxEndThread()or return?
A Our system consultants recommended avoid using AfxEndThread() function as well as ExitThread in Win32 API). When there is a thread in some objects, the destructor will not be called for them in case of using AfxEndThread().

When the return statement is used, the compiler implicitly adds to the code the appropriate destructor calls before the thread function really ends. Accordingly, AfxEndThread() call is not safe in terms of memory leaks when destructors for objects must be called.

   Declaration of AfxEndThread():
         void AFXAPI AfxEndThread(
               UINT nExitCode,
               BOOL bDelete = TRUE);


nExitCode Specifies the exit code of the thread.
bDelete Deletes the thread object from memory.

If the decision has been made to specify thread exit code this way, the return statement will provide the thread return code.

The second parameter will need to be set to TRUE or FALSE for the member m_bAutoDelete (public) for CWndThread object.

It is not a good practice to use:

• AfxEndThread() function.
• ExitThread() function.

Nor is it a good practice to use TerminateThread() API function.

Q How can a MFC project be created with built-in help files?
A When the MFC App Wizard is used for creating a project the WinHelp support can be included for the main window as part of the automatically generated files.

In order create a new project with context sensitive help support:

1. From the Visual C++ File menu, choose New.
2.  Choose MFC AppWizard (.exe).
3.  When given the option, make sure that the Context Sensitive Help box is checked.
4.  Continue through the process to set up the program.

This will create the following:

  • An application with a menu and if requested a toolbar.
  • A help file with topics for each of the automatically generated menu items and toolbar buttons.
    • The help author can use this framework as a starting point.
  • A .hm file with the topic ID to number mapping.
    • This file can be used directly by WinHelp.
  • An operational What's This? Help button on the toolbar.
    • Clicking this button puts the program into What's This? Help mode.
    • Clicking on a menu or control then opens its help.
  • Shift-F1 hooked up to put the program into What's this? Help mode.
    • F1 hooked up to open general help for the child window with focus or the control on which the user is currently clicking.
  • When a dialog box is created, F1 will be hooked up to open a general help topic for the whole dialog box.
  • All help topics opened in the main WinHelp window.


1. Since no dialog boxes are created in this process, it will be necessary to add context sensitive help support for each dialog box as it is created.

2. Once there is an initial help file and .hm file, it is usually easier to have the help author add new topics with a help authoring tool.

3. The program can still be used to generate the framework help for each build, because it will provide appropriate additional topic mapping entries.