Skip to content

Commit f25a70b

Browse files
author
dahall
committed
Fixed bug in LoadString overload (#570)
1 parent 9f3b4a5 commit f25a70b

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

CodeGen/Generator/VanaraAttributeGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ private static void BuildAddAsMethod(SourceProductionContext context, Compilatio
460460
var paramNode = tmpbuilder.docs?.SelectSingleNode($"//param[@name='{ps.Identifier.Text}']");
461461
var returnsNode = tmpbuilder.docs?.SelectSingleNode("//returns") ??
462462
tmpbuilder.docs?.DocumentElement?.AppendChild(tmpbuilder.docs?.CreateElement("returns")!);
463-
returnsNode?.InnerXml = paramNode?.InnerXml;
463+
if (returnsNode is not null)
464+
returnsNode.InnerXml = paramNode?.InnerXml;
464465
paramNode?.ParentNode?.RemoveChild(paramNode);
465466
}
466467
}

PInvoke/User32/WinUser.Res.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public static SafeHICON LoadImage_Icon([Optional] HINSTANCE hinst, SafeResourceI
379379
/// </returns>
380380
[PInvokeData("WinUser.h", MSDNShortId = "ms647486")]
381381
[DllImport(Lib.User32, CharSet = CharSet.Auto, SetLastError = true)]
382-
[System.Security.SecurityCritical]
382+
[System.Security.SecurityCritical, SuppressAutoGen]
383383
public static extern int LoadString(HINSTANCE hInstance, int uID, StringBuilder lpBuffer, int nBufferMax);
384384

385385
/// <summary>Loads a string resource from the executable file associated with a specified module.</summary>
@@ -390,17 +390,16 @@ public static SafeHICON LoadImage_Icon([Optional] HINSTANCE hinst, SafeResourceI
390390
/// <param name="uID">The identifier of the string to be loaded.</param>
391391
/// <returns>If the function succeeds, the return value is the full resource string.</returns>
392392
[PInvokeData("WinUser.h", MSDNShortId = "ms647486")]
393-
public static string LoadString(HINSTANCE hInstance, int uID)
393+
public static string? LoadString(HINSTANCE hInstance, int uID)
394394
{
395-
IntPtr p = default;
396-
var l = LoadString(hInstance, uID, p, 0);
395+
var l = LoadString(hInstance, uID, out var p);
397396
if (l == 0) Win32Error.ThrowLastError();
398-
return StringHelper.GetString(p, CharSet.Auto, l * Marshal.SystemDefaultCharSize)!;
397+
return p;
399398
}
400399

401400
[DllImport(Lib.User32, CharSet = CharSet.Auto, SetLastError = true)]
402401
[System.Security.SecurityCritical]
403-
private static extern int LoadString(HINSTANCE hInstance, int uID, IntPtr lpBuffer, int nBufferMax);
402+
private static extern int LoadString(HINSTANCE hInstance, int uID, out StrPtrAuto lpBuffer, int nBufferMax = 0);
404403

405404
/// <summary/>
406405
[StructLayout(LayoutKind.Sequential, Pack = 1)]

UnitTests/PInvoke/User32/User32Tests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ public void GetSetWindowLongTest()
185185
public void LoadImageTest() => throw new NotImplementedException();
186186

187187
[Test]
188-
public void LoadStringTest() => throw new NotImplementedException();
188+
public void LoadStringTest()
189+
{
190+
using var lib = Kernel32.LoadLibraryEx(TestCaseSources.ResourceFile, Kernel32.LoadLibraryExFlags.LOAD_LIBRARY_AS_DATAFILE);
191+
Assert.That(lib, ResultIs.ValidHandle);
192+
Assert.That(LoadString(lib, 103), Is.EqualTo("DummyResourceExe"));
193+
}
189194

190195
[Test]
191196
public void LoadStringTest1() => throw new NotImplementedException();

0 commit comments

Comments
 (0)