Show / Hide Table of Contents

Class Kernel

Inheritance
System.Object
Kernel
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: CADability
Assembly: CADability.dll
Syntax
public class Kernel

Methods

Beep(Int32, Int32)

The Beep function generates simple tones on the speaker. The function is synchronous; it does not return control to its caller until the sound finishes.

Declaration
public static bool Beep(int frequency, int duration)
Parameters
Type Name Description
System.Int32 frequency

Frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).

Windows 95/98/Me: The Beep function ignores this parameter.

System.Int32 duration

Duration of the sound, in milliseconds.

Windows 95/98/Me: The Beep function ignores this parameter.

Returns
Type Description
System.Boolean

If the function succeeds, the return value is true.

If the function fails, the return value is false. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

Terminal Services: The beep is redirected to the client.

Windows 95/98/Me: On computers with a sound card, the function plays the default sound event. On computers without a sound card, the function plays the standard system beep.

FreeLibrary(IntPtr)

The FreeLibrary function decrements the reference count of the loaded dynamic-link library (DLL). When the reference count reaches zero, the module is unmapped from the address space of the calling process and the handle is no longer valid.

Declaration
public static bool FreeLibrary(IntPtr moduleHandle)
Parameters
Type Name Description
System.IntPtr moduleHandle

Handle to the loaded DLL module. The LoadLibrary(String) or GetModuleHandle(String) function returns this handle.

Returns
Type Description
System.Boolean

If the function succeeds, the return value is true.

If the function fails, the return value is false. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

Each process maintains a reference count for each loaded library module. This reference count is incremented each time LoadLibrary(String) is called and is decremented each time FreeLibrary is called. A DLL module loaded at process initialization due to load-time dynamic linking has a reference count of one. This count is incremented if the same module is loaded by a call to LoadLibrary(String).

Before unmapping a library module, the system enables the DLL to detach from the process by calling the DLL's DllMain function, if it has one, with the DLL_PROCESS_DETACH value. Doing so gives the DLL an opportunity to clean up resources allocated on behalf of the current process. After the entry-point function returns, the library module is removed from the address space of the current process.

It is not safe to call FreeLibrary from DllMain. For more information, see the Remarks section in DllMain.

Calling FreeLibrary does not affect other processes using the same library module.

See Also
GetModuleHandle(String)
LoadLibrary(String)

GetDllDirectory(Int32, StringBuilder)

The GetDllDirectory function retrieves the application-specific portion of the search path used to locate DLLs for the application.

Declaration
public static int GetDllDirectory(int bufferLength, StringBuilder buffer)
Parameters
Type Name Description
System.Int32 bufferLength

Size of the output buffer, in characters.

System.Text.StringBuilder buffer

Pointer to a buffer that receives the application-specific portion of the search path.

Returns
Type Description
System.Int32

If the function succeeds, the return value is the length of the string copied to buffer, in characters, not including the terminating null character. If the return value is greater than bufferLength, it specifies the size of the buffer required for the path.

If the function fails, the return value is zero. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

See Also
SetDllDirectory(String)

GetModuleFileName(IntPtr, StringBuilder, Int32)

The GetModuleFileName function retrieves the fully qualified path for the specified module.

To specify the process that contains the module, use the GetModuleFileNameEx function.

Declaration
public static int GetModuleFileName(IntPtr module, StringBuilder fileName, int size)
Parameters
Type Name Description
System.IntPtr module

Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path for the current module.

System.Text.StringBuilder fileName

Pointer to a buffer that receives a null-terminated string that specifies the fully-qualified path of the module. If the length of the path exceeds the size specified by the size parameter, the function succeeds and the string is truncated to size characters and null terminated.

The path can have the prefix "\\?\", depending on how the module was loaded.

System.Int32 size

Size of the filename buffer, in TCHARs.

Returns
Type Description
System.Int32

If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs. If the buffer is too small to hold the module name, the string is truncated to size, and the function returns size.

If the function fails, the return value is zero. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

If a DLL is loaded in two processes, its file name in one process may differ in case from its file name in the other process.

For the ANSI version of the function, the number of TCHARs is the number of bytes; for the Unicode version, it is the number of characters.

Windows Me/98/95: This function retrieves long file names when an application's version number is greater than or equal to 4.00 and the long file name is available. Otherwise, it returns only 8.3 format file names.

See Also
GetModuleHandle(String)
LoadLibrary(String)

GetModuleHandle(String)

The GetModuleHandle function retrieves a module handle for the specified module if the file has been mapped into the address space of the calling process.

To avoid the race conditions described in the Remarks section, use the GetModuleHandleEx function.

Declaration
public static IntPtr GetModuleHandle(string moduleName)
Parameters
Type Name Description
System.String moduleName

Pointer to a null-terminated string that contains the name of the module (either a .dll or .exe file). If the file name extension is omitted, the default library extension .dll is appended. The file name string can include a trailing point character (.) to indicate that the module name has no extension. The string does not have to specify a path. When specifying a path, be sure to use backslashes (\), not forward slashes (/). The name is compared (case independently) to the names of modules currently mapped into the address space of the calling process.

If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process.

Returns
Type Description
System.IntPtr

If the function succeeds, the return value is a handle to the specified module (IntPtr).

If the function fails, the return value is NULL (IntPtr.Zero). To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

The returned handle is not global or inheritable. It cannot be duplicated or used by another process.

The GetModuleHandle function returns a handle to a mapped module without incrementing its reference count. Therefore, use care when passing the handle to the FreeLibrary(IntPtr) function, because doing so can cause a DLL module to be unmapped prematurely.

This function must be used carefully in a multithreaded application. There is no guarantee that the module handle remains valid between the time this function returns the handle and the time it is used. For example, a thread retrieves a module handle, but before it uses the handle, a second thread frees the module. If the system loads another module, it could reuse the module handle that was recently freed. Therefore, first thread would have a handle to a module different than the one intended.

See Also
FreeLibrary(IntPtr)
GetModuleFileName(IntPtr, StringBuilder, Int32)

GetProcAddress(IntPtr, String)

The GetProcAddress function retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).

Declaration
public static IntPtr GetProcAddress(IntPtr module, string processName)
Parameters
Type Name Description
System.IntPtr module

Handle to the DLL module that contains the function or variable. The LoadLibrary(String) or GetModuleHandle(String) function returns this handle.

System.String processName

Pointer to a null-terminated string that specifies the function or variable name, or the function's ordinal value. If this parameter is an ordinal value, it must be in the low-order word; the high-order word must be zero.

Returns
Type Description
System.IntPtr

If the function succeeds, the return value is the address of the exported function or variable.

If the function fails, the return value is NULL (IntPtr.Zero). To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

The spelling and case of a function name pointed to by processName must be identical to that in the EXPORTS statement of the source DLL's module-definition (.def) file. The exported names of functions may differ from the names you use when calling these functions in your code. This difference is hidden by macros used in the SDK header files.

The processName parameter can identify the DLL function by specifying an ordinal value associated with the function in the EXPORTS statement. GetProcAddress verifies that the specified ordinal is in the range 1 through the highest ordinal value exported in the .def file. The function then uses the ordinal as an index to read the function's address from a function table. If the .def file does not number the functions consecutively from 1 to N (where N is the number of exported functions), an error can occur where GetProcAddress returns an invalid, non-NULL address, even though there is no function with the specified ordinal.

In cases where the function may not exist, the function should be specified by name rather than by ordinal value.

See Also
FreeLibrary(IntPtr)
GetModuleHandle(String)
LoadLibrary(String)

GetProcessWorkingSetSize(IntPtr, out Int32, out Int32)

The GetProcessWorkingSetSize function retrieves the minimum and maximum working set sizes of the specified process.

Declaration
public static bool GetProcessWorkingSetSize(IntPtr process, out int minimumWorkingSetSize, out int maximumWorkingSetSize)
Parameters
Type Name Description
System.IntPtr process

Handle to the process whose working set sizes will be obtained. The handle must have the PROCESS_QUERY_INFORMATION access right.

System.Int32 minimumWorkingSetSize

Pointer to a variable that receives the minimum working set size of the specified process, in bytes. The virtual memory manager attempts to keep at least this much memory resident in the process whenever the process is active.

System.Int32 maximumWorkingSetSize

Pointer to a variable that receives the maximum working set size of the specified process, in bytes. The virtual memory manager attempts to keep no more than this much memory resident in the process whenever the process is active when memory is in short supply.

Returns
Type Description
System.Boolean

If the function succeeds, the return value is true.

If the function fails, the return value is false. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

The "working set" of a process is the set of memory pages currently visible to the process in physical RAM memory. These pages are resident and available for an application to use without triggering a page fault. The minimum and maximum working set sizes affect the virtual memory paging behavior of a process.

See Also
SetProcessWorkingSetSize(IntPtr, Int32, Int32)

GetSystemDirectory(StringBuilder, Int32)

The GetSystemDirectory function retrieves the path of the system directory. The system directory contains system such files such as dynamic-link libraries, drivers, and font files.

This function is provided primarily for compatibility. Applications should store code in the Program Files folder and persistent data in the Application Data folder in the user's profile.

Declaration
public static int GetSystemDirectory(StringBuilder buffer, int size)
Parameters
Type Name Description
System.Text.StringBuilder buffer

Pointer to the buffer to receive the null-terminated string containing the path. This path does not end with a backslash unless the system directory is the root directory. For example, if the system directory is named Windows\System on drive C, the path of the system directory retrieved by this function is C:\Windows\System.

System.Int32 size

Maximum size of the buffer, in TCHARs. This value should be set to at least MAX_PATH+1 to allow sufficient space for the path and the null terminator.

Returns
Type Description
System.Int32

If the function succeeds, the return value is the length, in TCHARs, of the string copied to the buffer, not including the terminating null character. If the length is greater than the size of the buffer, the return value is the size of the buffer required to hold the path.

If the function fails, the return value is zero. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

Applications should not create files in the system directory. If the user is running a shared version of the operating system, the application does not have write access to the system directory.

See Also
GetWindowsDirectory(StringBuilder, Int32)

GetSystemInfo(out Kernel.SYSTEM_INFO)

The GetSystemInfo function returns information about the current system.

To retrieve accurate information for a Win32-based application running on WOW64, call the GetNativeSystemInfo function.

Declaration
public static void GetSystemInfo(out Kernel.SYSTEM_INFO systemInfo)
Parameters
Type Name Description
Kernel.SYSTEM_INFO systemInfo

Pointer to a Kernel.SYSTEM_INFO structure that receives the information.

See Also
Kernel.SYSTEM_INFO

GetSystemWindowsDirectory(StringBuilder, Int32)

The GetSystemWindowsDirectory function retrieves the path of the shared Windows directory on a multi-user system.

Declaration
public static int GetSystemWindowsDirectory(StringBuilder buffer, int size)
Parameters
Type Name Description
System.Text.StringBuilder buffer

Pointer to the buffer to receive a null-terminated string containing the path. This path does not end with a backslash unless the Windows directory is the root directory. For example, if the Windows directory is named Windows on drive C, the path of the Windows directory retrieved by this function is C:\Windows. If the system was installed in the root directory of drive C, the path retrieved is C:.

System.Int32 size

Maximum size of the buffer specified by the buffer parameter, in TCHARs. This value should be set to at least MAX_PATH+1 to allow sufficient space for the path and the null-terminating character.

Returns
Type Description
System.Int32

If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs, not including the terminating null character.

If the length is greater than the size of the buffer, the return value is the size of the buffer required to hold the path.

If the function fails, the return value is zero. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

On a system that is running Terminal Server, each user has a unique Windows directory. The system Windows directory is shared by all users, so it is the directory where an application should store initialization and help files that apply to all users.

With Terminal Services, the GetSystemWindowsDirectory function retrieves the path of the system Windows directory, while the GetWindowsDirectory(StringBuilder, Int32) function retrieves the path of a Windows directory that is private for each user. On a single-user system, GetSystemWindowsDirectory is the same as GetWindowsDirectory(StringBuilder, Int32).

Windows NT 4.0 Terminal Server Edition: To retrieve the shared Windows directory, call GetSystemDirectory(StringBuilder, Int32) and trim the "System32" element from the end of the returned path.

See Also
GetWindowsDirectory(StringBuilder, Int32)

GetTickCount()

The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.

Declaration
public static int GetTickCount()
Returns
Type Description
System.Int32

The return value is the number of milliseconds that have elapsed since the system was started.

Remarks

The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.

If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.

To obtain the time elapsed since the computer was started, retrieve the System Up Time counter in the performance data in the registry key HKEY_PERFORMANCE_DATA. The value returned is an 8-byte value.

GetWindowsDirectory(StringBuilder, Int32)

The GetWindowsDirectory function retrieves the path of the Windows directory. The Windows directory contains such files as applications, initialization files, and help files.

This function is provided primarily for compatibility. Applications should store code in the Program Files folder and persistent data in the Application Data folder in the user's profile.

Declaration
public static int GetWindowsDirectory(StringBuilder buffer, int size)
Parameters
Type Name Description
System.Text.StringBuilder buffer

Pointer to the buffer to receive the null-terminated string containing the path. This path does not end with a backslash unless the Windows directory is the root directory. For example, if the Windows directory is named Windows on drive C, the path of the Windows directory retrieved by this function is C:\Windows. If the system was installed in the root directory of drive C, the path retrieved is C:.

System.Int32 size

Maximum size of the buffer specified by the buffer parameter, in TCHARs. This value should be set to MAX_PATH.

Returns
Type Description
System.Int32

If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs, not including the terminating null character.

If the length is greater than the size of the buffer, the return value is the size of the buffer required to hold the path.

If the function fails, the return value is zero. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

The Windows directory is the directory where an application should store initialization and help files. If the user is running a shared version of the system, the Windows directory is guaranteed to be private for each user.

If an application creates other files that it wants to store on a per-user basis, it should place them in the directory specified by the HOMEPATH environment variable. This directory will be different for each user, if so specified by an administrator, through the User Manager administrative tool. HOMEPATH always specifies either the user's home directory, which is guaranteed to be private for each user, or a default directory (for example, C:\USERS\DEFAULT) where the user will have all access.

Terminal Services: If the application is running in a Terminal Services environment, each user has a unique Windows directory. If an application that is not Terminal-Services-aware calls this function, it retrieves the path of the Windows directory on the client, not the Windows directory on the server.

See Also
GetSystemDirectory(StringBuilder, Int32)
GetSystemWindowsDirectory(StringBuilder, Int32)

GlobalMemoryStatus(out Kernel.MEMORYSTATUS)

The GlobalMemoryStatus function obtains information about the system's current usage of both physical and virtual memory.

To obtain information about the extended portion of the virtual address space, or if your application may run on computers with more than 4 GB of main memory, use the GlobalMemoryStatusEx function.

Declaration
public static void GlobalMemoryStatus(out Kernel.MEMORYSTATUS buffer)
Parameters
Type Name Description
Kernel.MEMORYSTATUS buffer

Pointer to a Kernel.MEMORYSTATUS structure. The GlobalMemoryStatus function stores information about current memory availability into this structure.

Remarks

You can use the GlobalMemoryStatus function to determine how much memory your application can allocate without severely impacting other applications.

The information returned by the GlobalMemoryStatus function is volatile. There is no guarantee that two sequential calls to this function will return the same information.

On computers with more than 4 GB of memory, the GlobalMemoryStatus function can return incorrect information. Windows 2000 and later report a value of -1 to indicate an overflow. Earlier versions of Windows NT report a value that is the real amount of memory, modulo 4 GB. For this reason, use the GlobalMemoryStatusEx function instead.

On Intel x86 computers with more than 2 GB and less than 4 GB of memory, the GlobalMemoryStatus function will always return 2 GB in the TotalPhys member of the Kernel.MEMORYSTATUS structure. Similarly, if the total available memory is between 2 and 4 GB, the AvailPhys member of the Kernel.MEMORYSTATUS structure will be rounded down to 2 GB. If the executable is linked using the /LARGEADDRESSWARE linker option, then the GlobalMemoryStatus function will return the correct amount of physical memory in both members.

See Also
Kernel.MEMORYSTATUS

IsProcessorFeaturePresent(Int32)

The IsProcessorFeaturePresent function determines whether the specified processor feature is supported by the current computer.

Declaration
public static bool IsProcessorFeaturePresent(int processorFeature)
Parameters
Type Name Description
System.Int32 processorFeature

Processor feature to be tested. This parameter can be one of the following values:

ValueDescription
The 3D-Now instruction set is available.
The compare and exchange double operation is available (Pentium, MIPS, and Alpha).

Floating-point operations are emulated using a software emulator.

This function returns true if floating-point operations are emulated; otherwise, it returns false.

Windows NT 4.0: This function returns false if floating-point operations are emulated; otherwise, it returns true. This behavior is a bug that is fixed in later versions.

Pentium: In rare circumstances, a floating-point precision error can occur.
The MMX instruction set is available.
The processor is PAE-enabled.
The RDTSC instruction is available.
The SSE instruction set is available.
The SSE2 instruction set is available.

Returns
Type Description
System.Boolean

If the feature is supported, the return value is true.

If the feature is not supported, the return value is false.

LoadLibrary(String)

The LoadLibrary function maps the specified executable module into the address space of the calling process.

Declaration
public static IntPtr LoadLibrary(string fileName)
Parameters
Type Name Description
System.String fileName

Pointer to a null-terminated string that names the executable module (either a .dll or .exe file). The name specified is the file name of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.def) file.

If the string specifies a path but the file does not exist in the specified directory, the function fails. When specifying a path, be sure to use backslashes (\), not forward slashes (/).

If the string does not specify a path, the function uses a standard search strategy to find the file. See the Remarks for more information.

Returns
Type Description
System.IntPtr

If the function succeeds, the return value is a handle to the module (IntPtr).

If the function fails, the return value is NULL (IntPtr.Zero). To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Windows Me/98/95: If you are using LoadLibrary to load a module that contains a resource whose numeric identifier is greater than 0x7FFF, LoadLibrary fails. If you are attempting to load a 16-bit DLL directly from 32-bit code, LoadLibrary fails. If you are attempting to load a DLL whose subsystem version is greater than 4.0, LoadLibrary fails. If your DllMain function tries to call the Unicode version of a function, LoadLibrary fails.

Remarks

LoadLibrary can be used to map a DLL module and return a handle that can be used in GetProcAddress(IntPtr, String) to get the address of a DLL function. LoadLibrary can also be used to map other executable modules. For example, the function can specify an .exe file to get a handle that can be used in FindResource or LoadResource. However, do not use LoadLibrary to run an .exe file, use the CreateProcess function.

If the module is a DLL not already mapped for the calling process, the system calls the DLL's DllMain function with the DLL_PROCESS_ATTACH value. If the DLL's entry-point function does not return TRUE, LoadLibrary fails and returns NULL. (The system immediately calls your entry-point function with DLL_PROCESS_DETACH and unloads the DLL.)

It is not safe to call LoadLibrary from DllMain. For more information, see the Remarks section in DllMain.

Module handles are not global or inheritable. A call to LoadLibrary by one process does not produce a handle that another process can use — for example, in calling GetProcAddress(IntPtr, String). The other process must make its own call to LoadLibrary for the module before calling GetProcAddress(IntPtr, String).

If no file name extension is specified in the fileName parameter, the default library extension .dll is appended. However, the file name string can include a trailing point character (.) to indicate that the module name has no extension. When no path is specified, the function searches for loaded modules whose base name matches the base name of the module to be loaded. If the name matches, the load succeeds. Otherwise, the function searches for the file in the following sequence:

  1. The directory from which the application loaded.
  2. The current directory.
  3. The system directory. Use the GetSystemDirectory(StringBuilder, Int32) function to get the path of this directory.
  4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.

    Windows Me/98/95: This directory does not exist.

  5. The Windows directory. Use the GetWindowsDirectory(StringBuilder, Int32) function to get the path of this directory.
  6. The directories that are listed in the PATH environment variable.

Windows Server 2003, Windows XP SP1: The default value of HKLM\System\CurrentControlSet\Control\Session Manager\SafeDllSearchMode is 1 (current directory is searched after the system and Windows directories).

Windows XP: If HKLM\System\CurrentControlSet\Control\Session Manager\SafeDllSearchMode is 1, the current directory is searched after the system and Windows directories, but before the directories in the PATH environment variable. The default value is 0 (current directory is searched before the system and Windows directories).

The first directory searched is the one directory containing the image file used to create the calling process (for more information, see the CreateProcess function). Doing this allows private dynamic-link library (DLL) files associated with a process to be found without adding the process's installed directory to the PATH environment variable.

The search path can be altered using the SetDllDirectory(String) function. This solution is recommended instead of using SetCurrentDirectory or hard-coding the full path to the DLL.

If a path is specified and there is a redirection file for the application, the function searches for the module in the application's directory. If the module exists in the application's directory, the LoadLibrary function ignores the specified path and loads the module from the application's directory. If the module does not exist in the application's directory, LoadLibrary loads the module from the specified directory.

See Also
FreeLibrary(IntPtr)
GetProcAddress(IntPtr, String)
GetSystemDirectory(StringBuilder, Int32)
GetWindowsDirectory(StringBuilder, Int32)
SetDllDirectory(String)

QueryPerformanceCounter(out Int64)

The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter.

Declaration
public static bool QueryPerformanceCounter(out long performanceCount)
Parameters
Type Name Description
System.Int64 performanceCount

Pointer to a variable that receives the current performance-counter value, in counts.

Returns
Type Description
System.Boolean

If the function succeeds, the return value is true.

If the function fails, the return value is false. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

On a multiprocessor machine, it should not matter which processor is called. However, you can get different results on different processors due to bugs in the BIOS or the HAL. To specify processor affinity for a thread, use the SetThreadAffinityMask function.

See Also
QueryPerformanceCounterFast(out Int64)
QueryPerformanceFrequency(out Int64)

QueryPerformanceCounterFast(out Int64)

The QueryPerformanceCounterFast function retrieves the current value of the high-resolution performance counter.

Declaration
public static int QueryPerformanceCounterFast(out long performanceCount)
Parameters
Type Name Description
System.Int64 performanceCount

Pointer to a variable that receives the current performance-counter value, in counts.

Returns
Type Description
System.Int32

If the function succeeds, the return value is true.

If the function fails, the return value is false.

Remarks

This version of QueryPerformanceCounter(out Int64) is slightly faster. It does not set the last Windows error. Use with care.

On a multiprocessor machine, it should not matter which processor is called. However, you can get different results on different processors due to bugs in the BIOS or the HAL. To specify processor affinity for a thread, use the SetThreadAffinityMask function.

See Also
QueryPerformanceCounter(out Int64)
QueryPerformanceFrequency(out Int64)

QueryPerformanceFrequency(out Int64)

The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. The frequency cannot change while the system is running.

Declaration
public static bool QueryPerformanceFrequency(out long frequency)
Parameters
Type Name Description
System.Int64 frequency

Pointer to a variable that receives the current performance-counter frequency, in counts per second. If the installed hardware does not support a high-resolution performance counter, this parameter can be zero.

Returns
Type Description
System.Boolean

If the installed hardware supports a high-resolution performance counter, the return value is true.

If the function fails, the return value is false. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error. For example, if the installed hardware does not support a high-resolution performance counter, the function fails.

Remarks

Note The frequency of the high-resolution performance counter is not the processor speed.

See Also
QueryPerformanceCounter(out Int64)

SetDllDirectory(String)

The SetDllDirectory function modifies the search path used to locate DLLs for the application.

Declaration
public static bool SetDllDirectory(string pathName)
Parameters
Type Name Description
System.String pathName

Pointer to a null-terminated string that specifies the directories to be added to the search path, separated by semicolons. If this parameter is NULL, the default search path is used.

Returns
Type Description
System.Boolean

If the function succeeds, the return value is true.

If the function fails, the return value is false. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

The SetDllDirectory function affects all subsequent calls to the LoadLibrary(String) and LoadLibraryEx functions. After calling SetDllDirectory, the DLL search path is:

  1. The directory from which the application loaded.
  2. The directory specified by the pathName parameter.
  3. The system directory. Use the GetSystemDirectory(StringBuilder, Int32) function to get the path of this directory. The name of this directory is System32.
  4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
  5. The Windows directory. Use the GetWindowsDirectory(StringBuilder, Int32) function to get the path of this directory.
  6. The directories that are listed in the PATH environment variable.

To revert to the default search path used by LoadLibrary(String) and LoadLibraryEx, call SetDllDirectory with NULL.

See Also
GetDllDirectory(Int32, StringBuilder)
GetSystemDirectory(StringBuilder, Int32)
GetWindowsDirectory(StringBuilder, Int32)
LoadLibrary(String)

SetProcessWorkingSetSize(IntPtr, Int32, Int32)

The SetProcessWorkingSetSize function sets the minimum and maximum working set sizes for the specified process.

Declaration
public static bool SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize)
Parameters
Type Name Description
System.IntPtr process

Handle to the process whose working set sizes is to be set.

The handle must have the PROCESS_SET_QUOTA access right.

System.Int32 minimumWorkingSetSize

Minimum working set size for the process, in bytes. The virtual memory manager attempts to keep at least this much memory resident in the process whenever the process is active.

If both minimumWorkingSetSize and maximumWorkingSetSize have the value -1, the function temporarily trims the working set of the specified process to zero. This essentially swaps the process out of physical RAM memory.

System.Int32 maximumWorkingSetSize

Maximum working set size for the process, in bytes. The virtual memory manager attempts to keep no more than this much memory resident in the process whenever the process is active and memory is in short supply.

If both minimumWorkingSetSize and maximumWorkingSetSize have the value -1, the function temporarily trims the working set of the specified process to zero. This essentially swaps the process out of physical RAM memory.

Returns
Type Description
System.Boolean

If the function succeeds, the return value is true.

If the function fails, the return value is false. To get extended error information, call System.Runtime.InteropServices.Marshal.GetLastWin32Error.

Remarks

The working set of a process is the set of memory pages currently visible to the process in physical RAM memory. These pages are resident and available for an application to use without triggering a page fault. The minimum and maximum working set sizes affect the virtual memory paging behavior of a process.

The working set of the specified process can be emptied by specifying the value -1 for both the minimum and maximum working set sizes.

If the values of either minimumWorkingSetSize or maximumWorkingSetSize are greater than the process' current working set sizes, the specified process must have the SE_INC_BASE_PRIORITY_NAME privilege. Users in the Administrators and Power Users groups generally have this privilege.

The operating system allocates working set sizes on a first-come, first-served basis. For example, if an application successfully sets 40 megabytes as its minimum working set size on a 64-megabyte system, and a second application requests a 40-megabyte working set size, the operating system denies the second application's request.

Using the SetProcessWorkingSetSize function to set an application's minimum and maximum working set sizes does not guarantee that the requested memory will be reserved, or that it will remain resident at all times. When the application is idle, or a low-memory situation causes a demand for memory, the operating system can reduce the application's working set. An application can use the VirtualLock function to lock ranges of the application's virtual address space in memory; however, that can potentially degrade the performance of the system.

When you increase the working set size of an application, you are taking away physical memory from the rest of the system. This can degrade the performance of other applications and the system as a whole. It can also lead to failures of operations that require physical memory to be present; for example, creating processes, threads, and kernel pool. Thus, you must use the SetProcessWorkingSetSize function carefully. You must always consider the performance of the whole system when you are designing an application.

See Also
GetProcessWorkingSetSize(IntPtr, out Int32, out Int32)
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX