A call to PInvoke function Microsoft.ReportViewer.Common

  • Last Post 03 December 2021
admin posted this 23 October 2019

This post shows you How to fix: Managed Debugging Assistant 'PInvokeStackImbalance' when using Microsoft.ReportViewer.Common in C# .NET.

Managed Debugging Assistant 'PInvokeStackImbalance' 

Message=Managed Debugging Assistant 'PInvokeStackImbalance' : 'A call to PInvoke function 'Microsoft.ReportViewer.Common!Microsoft.ReportingServices.Rendering.ImageRenderer.FontPackage::CreateFontPackage' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.'

The problem occurs when using multiple font in RDLC report.

To fix Microsoft.ReportViewer.Common!Microsoft.ReportingServices.Rendering.ImageRenderer.FontPackage

You can add


to DeviceInfo as shown below.

string deviceInfo = "<DeviceInfo>" +
                    "  <OutputFormat>PDF</OutputFormat>" +
                    "  <PageWidth>8.27in</PageWidth>" +
                    "  <PageHeight>11.69in</PageHeight>" +
                    "  <MarginTop>0.25in</MarginTop>" +
                    "  <MarginLeft>0.4in</MarginLeft>" +
                    "  <MarginRight>0in</MarginRight>" +
                    "  <MarginBottom>0.25in</MarginBottom>" +
                    "  <EmbedFonts>None</EmbedFonts>" +
string mimeType;
string encoding;
string fileNameExtension;
Warning[] warnings;
string[] streams;
byte[] renderedBytes = report.Render("PDF", deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
return File(renderedBytes, mimeType);
Order By: Standard | Newest | Votes
Paraglider posted this 05 December 2019

Thanks mate, you made my day!

jedarc posted this 17 June 2021

Yes, this no longer generates the error in Visual Studio, but currently Firefox depends on embedded fonts to function properly: https://wiki.mozilla.org/PDF.js/fonts

Vporras posted this 03 December 2021

I congratulate you for such a timely contribution, simple, 
simple and effective solution, sincere greetings my dear