:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / lib / user32 / misc / resources.c
1 #include <string.h>
2 #include <windows.h>
3 #include <ddk/ntddk.h>
4 #include <kernel32/error.h>
5
6 int
7 STDCALL
8 LoadStringA( HINSTANCE hInstance,
9              UINT uID,
10              LPSTR lpBuffer,
11              int nBufferMax)
12 {
13   HRSRC rsc;
14   PBYTE ptr;
15   int len;
16   int count, dest = uID % 16;
17   PWSTR pwstr;
18   UNICODE_STRING UString;
19   ANSI_STRING AString;
20   NTSTATUS Status;
21   
22   rsc = FindResource( (HMODULE)hInstance,
23                       MAKEINTRESOURCE( (uID / 16) + 1 ),
24                       RT_STRING );
25   if( rsc == NULL )
26     return 0;
27   // get pointer to string table
28   ptr = (PBYTE)LoadResource( (HMODULE)hInstance, rsc );
29   if( ptr == NULL )
30     return 0;
31   for( count = 0; count <= dest; count++ )
32     {
33       // walk each of the 16 string slots in the string table
34       len = (*(USHORT *)ptr) * 2;  // length is in unicode chars, convert to bytes
35       ptr += 2;    // first 2 bytes are length, string follows
36       pwstr = (PWSTR)ptr;
37       ptr += len;
38     }
39   if( !len )
40     return 0;   // zero means no string is there
41   // convert unitocde to ansi, and copy string to caller buffer
42   UString.Length = UString.MaximumLength = len;
43   UString.Buffer = pwstr;
44   memset( &AString, 0, sizeof AString );
45   Status = RtlUnicodeStringToAnsiString( &AString, &UString, TRUE );
46   if( !NT_SUCCESS( Status ) )
47     {
48       SetLastErrorByStatus( Status );
49       return 0;
50     }
51   nBufferMax--;  // save room for the null
52   if( nBufferMax > AString.Length )
53     nBufferMax = AString.Length;
54   memcpy( lpBuffer, AString.Buffer, nBufferMax );
55   lpBuffer[nBufferMax] = 0;
56   RtlFreeAnsiString( &AString );
57   return nBufferMax;
58 }
59