test: base.c Source File

Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

# base.c

Go to the documentation of this file.

    00001 /****************************** Module Header ******************************\
    00002 * Module Name: base.c
    00003 *
    00004 * Copyright (c) 1985 - 1999, Microsoft Corporation
    00005 *
    00006 * Contains private versions of routines that used to be in kernel32.dll
    00007 *
    00008 * History:
    00009 * 12-16-94 JimA         Created.
    00010 \***************************************************************************/
    00011
    00012 #include "precomp.h"
    00013 #pragma hdrstop
    00014
    00015 #include <ntddbeep.h>
    00016
    00017 /***************************************************************************\
    00018 * RtlLoadStringOrError
    00019 *
    00020 * NOTE: Passing a NULL value for lpch returns the string length. (WRONG!)
    00021 *
    00022 * Warning: The return count does not include the terminating NULL WCHAR;
    00023 *
    00024 * History:
    00025 * 04-05-91 ScottLu      Fixed - code is now shared between client and server
    00026 * 09-24-90 MikeKe       From Win30
    00027 * 12-09-94 JimA         Use message table.
    00028 \***************************************************************************/
    00029
    00030 int RtlLoadStringOrError(
    00031     UINT wID,
    00032     LPWSTR lpBuffer,            // Unicode buffer
    00033     int cchBufferMax,           // cch in Unicode buffer
    00034     WORD wLangId)
    00035 {
    00036     PMESSAGE_RESOURCE_ENTRY MessageEntry;
    00037     int cch;
    00038     NTSTATUS Status;
    00039
    00040     /*
    00041      * Make sure the parms are valid.
    00042      */
    00043     if (!lpBuffer || (cchBufferMax-- == 0))
    00044         return 0;
    00045
    00046     cch = 0;
    00047
    00048     Status = RtlFindMessage((PVOID)hModuleWin, (ULONG_PTR)RT_MESSAGETABLE,
    00049             wLangId, wID, &MessageEntry);
    00050     if (NT_SUCCESS(Status)) {
    00051
    00052         /*
    00053          * Copy out the message.  If the whole thing can be copied,
    00054          * copy two fewer chars so the crlf in the message will be
    00055          * stripped out.
    00056          */
    00057         cch = wcslen((PWCHAR)MessageEntry->Text) - 2;
    00058         if (cch > cchBufferMax)
    00059             cch = cchBufferMax;
    00060
    00061         RtlCopyMemory(lpBuffer, (PWCHAR)MessageEntry->Text, cch * sizeof(WCHAR));
    00062     }
    00063
    00064     /*
    00065      * Append a NULL.
    00066      */
    00067     lpBuffer[cch] = 0;
    00068
    00069     return cch;
    00070 }
    00071
    00072
    00073 /***************************************************************************\
    00074 * UserSleep
    00075 *
    00076 * Kernel-mode version of Sleep() that must have a timeout value and
    00077 * is not alertable.
    00078 *
    00079 * History:
    00080 * 12-11-94 JimA         Created.
    00081 \***************************************************************************/
    00082
    00083 VOID UserSleep(
    00084     DWORD dwMilliseconds)
    00085 {
    00086     LARGE_INTEGER TimeOut;
    00087
    00088     TimeOut.QuadPart = Int32x32To64( dwMilliseconds, -10000 );
    00089     KeDelayExecutionThread(UserMode, FALSE, &TimeOut);
    00090 }
    00091
    00092
    00093 /***************************************************************************\
    00094 * UserBeep
    00095 *
    00096 * Kernel-mode version of Beep().
    00097 *
    00098 * History:
    00099 * 12-16-94 JimA         Created.
    00100 \***************************************************************************/
    00101
    00102 BOOL UserBeep(
    00103     DWORD dwFreq,
    00104     DWORD dwDuration)
    00105 {
    00106     OBJECT_ATTRIBUTES ObjectAttributes;
    00107     UNICODE_STRING NameString;
    00108     NTSTATUS Status;
    00109     IO_STATUS_BLOCK IoStatus;
    00110     BEEP_SET_PARAMETERS BeepParameters;
    00111     HANDLE hBeepDevice;
    00112     LARGE_INTEGER TimeOut;
    00113
    00114     CheckCritOut();
    00115
    00116     if (gbRemoteSession) {
    00117         if (gpRemoteBeepDevice == NULL)
    00118             Status = STATUS_UNSUCCESSFUL;
    00119         else
    00120             Status = ObOpenObjectByPointer(
    00121                           gpRemoteBeepDevice,
    00122                           0,
    00123                           NULL,
    00124                           EVENT_ALL_ACCESS,
    00125                           NULL,
    00126                           KernelMode,
    00127                           &hBeepDevice);
    00128     } else {
    00129
    00130         RtlInitUnicodeString(&NameString, DD_BEEP_DEVICE_NAME_U);
    00131
    00132         InitializeObjectAttributes(&ObjectAttributes,
    00133                                    &NameString,
    00134                                    0,
    00135                                    NULL,
    00136                                    NULL);
    00137
    00138         Status = ZwCreateFile(&hBeepDevice,
    00139                               FILE_READ_DATA | FILE_WRITE_DATA,
    00140                               &ObjectAttributes,
    00141                               &IoStatus,
    00142                               NULL,
    00143                               0,
    00144                               FILE_SHARE_READ | FILE_SHARE_WRITE,
    00145                               FILE_OPEN_IF,
    00146                               0,
    00147                               (PVOID) NULL,
    00148                               0L);
    00149     }
    00150
    00151     if (!NT_SUCCESS(Status)) {
    00152         return FALSE;
    00153     }
    00154
    00155     /*
    00156      * 0,0 is a special case used to turn off a beep.  Otherwise
    00157      * validate the dwFreq parameter to be in range.
    00158      */
    00159     if ((dwFreq != 0 || dwDuration != 0) &&
    00160         (dwFreq < (ULONG)0x25 || dwFreq > (ULONG)0x7FFF)) {
    00161
    00162         Status = STATUS_INVALID_PARAMETER;
    00163     } else {
    00164         BeepParameters.Frequency = dwFreq;
    00165         BeepParameters.Duration = dwDuration;
    00166
    00167         Status = ZwDeviceIoControlFile(hBeepDevice,
    00168                                        NULL,
    00169                                        NULL,
    00170                                        NULL,
    00171                                        &IoStatus,
    00172                                        IOCTL_BEEP_SET,
    00173                                        &BeepParameters,
    00174                                        sizeof(BeepParameters),
    00175                                        NULL,
    00176                                        0);
    00177     }
    00178
    00179     EnterCrit();
    00180     _UserSoundSentryWorker();
    00181     LeaveCrit();
    00182
    00183     if (!NT_SUCCESS(Status)) {
    00184         ZwClose(hBeepDevice);
    00185         return FALSE;
    00186     }
    00187
    00188     /*
    00189      * Beep device is asynchronous, so sleep for duration
    00190      * to allow this beep to complete.
    00191      */
    00192     if (dwDuration != (DWORD)-1 && (dwFreq != 0 || dwDuration != 0)) {
    00193         TimeOut.QuadPart = Int32x32To64( dwDuration, -10000);
    00194
    00195         do {
    00196             Status = KeDelayExecutionThread(UserMode, FALSE, &TimeOut);
    00197         }
    00198         while (Status == STATUS_ALERTED);
    00199     }
    00200     ZwClose(hBeepDevice);
    00201     return TRUE;
    00202 }
    00203
    00204 void RtlInitUnicodeStringOrId(
    00205     PUNICODE_STRING pstrName,
    00206     LPWSTR lpstrName)
    00207 {
    00208     if (IS_PTR(lpstrName)) {
    00209         RtlInitUnicodeString(pstrName, lpstrName);
    00210     } else {
    00211         pstrName->Length = pstrName->MaximumLength = 0;
    00212         pstrName->Buffer = lpstrName;
    00213     }
    00214 }