Microsoft KB Archive/179269

= HOWTO: Use Source Profiler to Profile Dynamic-Link Libraries =

Article ID: 179269

Article Last Modified on 3/21/2005

-

APPLIES TO

 Microsoft Source Profiler 1.2, when used with:  Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q179269

<div class="summary_section">

SUMMARY
The Microsoft Visual C++ Help discusses two ways to profile a dynamic-link library (DLL), but it does not indicate when to use each example. This article describes a general-purpose method of profiling DLLs.

<div class="moreinformation_section">

MORE INFORMATION
The first part of the Help on profiling a DLL describes the use of self- profiling DLLs, using the PREP option /OM. Self-profiling DLLs are not necessary, except when profiling DLLs that are dynamically loaded using the LoadLibrary instruction. Use the following batch file example (name it Profdll.bat) to profile a single DLL, accessed with LoadLibrary. Profdll.bat profiles a DLL with the assumption that the .exe file has not been prepared for profiling.

Sample Batch File
<pre class="fixed_text">  COPY %1.dll save PREP /OM %1.dll COPY %1._ll %1.dll PROFILE /I %1 /O %1 %2 %3 %4 %5 %6 COPY save %1.dll PREP /M %1 PLIST %1 If you have a main program (for example, Hearts.exe) that uses a DLL (for example, Cards.dll) and you want to profile only the DLL, you can run the batch file as: <pre class="fixed_text">  PROFDLL cards hearts Note that the command above generates a warning message that the main program has not been prepared for profiling. You can ignore this message.

The second example batch file in the Help shows how to profile multiple DLLs as well as the calling executable. This example profiles an executable named "wingame" and two DLLs named "aliens" and "hiscore":

Sample Batch File
<pre class="fixed_text">  PREP /OM /FC wingame.exe aliens.dll hiscore.dll PROFILE wingame.exe PREP /M wingame PLIST wingame >wingame.out This method is most effective when the DLLs are loaded at run time, through the use of an import library. The Source Profiler, however, does not report any data for a dynamically loaded DLL if you use function profiling. The maximum number of DLLs that can be profiled this way is:
 * 7 for Visual C++, versions 2.x and 4.x
 * 15 for Visual C++, version 5.0
 * 31 for Visual C++, version 6.0

When you combine import DLLs and LoadLibrary DLLs in a single executable, the Help is unclear about how to handle this. The first method provides information about a single DLL, and the second method does not work for all types of profiling.

You must combine these two methods when considering larger, more complex projects. First, create self-profiling objects for all items in the profile: <pre class="fixed_text">  PREP /OM /FT myfile.exe import1.dll loadlib1.dll ... where "myfile.exe" is the executable program, "import1.dll" is a DLL called through an import library, and "loadlib1.dll" is a DLL called through the LoadLibrary and GetProcAddress functions.

Next, use the first method and copy the _LL files over all DLLs loaded using LoadLibrary: <pre class="fixed_text">  copy loadlib1.dll loadlib1.tmp copy loadlib1._ll loadlib1.dll The rest of the procedure is the same as for most profiles: <pre class="fixed_text">  PROFILE myfile.exe PREP /M myfile PLIST myfile >> profile.out Putting it all together, you can generalize the process as follows: <ol> <li>PREP /OM /[profiling type] exename dllname1 dllname2 ... where [profiling type] is either /FT, /FC, /FV, /LC, or /LV</li> <li> If a DLL is loaded using LoadLibrary, copy the _LL file over the existing .dll file. You should backup the original DLL, just in case: <pre class="fixed_text">     copy dllname.dll dllname.tmp copy dllname._ll dllname.dll where "dllname" is the name of the DLL accessed using LoadLibrary. Note that the _LL file is not created for /LC and /LV profiling types. </li> <li>Run the remaining PROFILE, PREP, and PLIST steps as outlined above and in the Visual C++ Help.</li></ol>

You should not try to profile a DLL that may have been loaded at run time and dynamically in the same application.

<div class="references_section">