Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electron-Edge 6.5.5 High CPU Usage when calling winspool.Drv DocumentProperties extern method #43

Closed
Suraj1189 opened this issue Apr 5, 2019 · 0 comments

Comments

@Suraj1189
Copy link

Suraj1189 commented Apr 5, 2019

Hi have logic where i am calling winspool.Drv DocumentProperties method to open the Printer properties dialog. I have a c# .net DLL from where winspool.Drv DocumentProperties extern method gets called and printer properties dialog opened successfully.

But after successive calling of that method , machines CPU usage gets high and remains high till i close my electron-edge application. i am having Windows 10 64 bit 16GB RAM.

When i am calling this method WMI Host provider application gets launched and start consuming memory. Also on some other machine Windows 10 64 bit 8 GB RAM ,System.Management Quota Violation exception get thrown after successive calls(5-6 times) to that method.

So some how memory leak is there , which i don't understand. Please help me to find out the solution.

Please check the below code snippet from my code.

in Node.js

  1. this.edge = electron.remote.require('electron-edge');
  2. handle = this.edge.func({ assemblyFile: assemblyPath });

handle.OpenPrintDialog(printerData, function (err, res) {
if (err)
deferred.reject(err);
else
deferred.resolve(res);
handle = null;
});

In C#

  1. InterOp pattern Startup class method called via electron-edge

public async Task Invoke(dynamic input)
{
IPrintController controller = GetPrintController(monitor);
return new
{
OpenPrintDialog = (Func<object, Task>)(
async (dynamic i) => await controller.OpenPrintDialog(Convert.ToString(i.PrinterName), Convert.ToString(i.PrintDialogSetup)))
}
}
5. middle layer to to convert passed printer settings and hand it over to main class

public async Task OpenPrintDialog(string printerName, string printDialogSetup)
{
return await PrintHelper.OpenPrintDialogAsync(printerName, printDialogSetup);
}

  1. Main class where all extern methods are declared and called

public class PrintHelper
{
[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);

    [DllImport("kernel32.dll")]
    static extern bool GlobalUnlock(IntPtr hMem);

    [DllImport("kernel32.dll")]
    static extern bool GlobalFree(IntPtr hMem);

    [DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling  true, CallingConvention = CallingConvention.StdCall)]
    static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter, [MarshalAs(UnmanagedType.LPWStr)] string pDeviceName, IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr GetForegroundWindow();

    private const int DM_PROMPT = 4;
    private const int DM_OUT_BUFFER = 2;
    private const int DM_IN_BUFFER = 8;
    private IntPtr ParentHandle;
    public DialogResult ShowDialog(PrinterSettings printerSettings)
    {

        ParentHandle = GetForegroundWindow();
        DialogResult dialogResult = DialogResult.Cancel;

        IntPtr devModeData = printerSettings.GetHdevmode();
        IntPtr iDevModeNew = GlobalLock(devModeData);
        long selectedOption = DocumentProperties(ParentHandle, IntPtr.Zero, printerSettings.PrinterName, iDevModeNew, iDevModeNew, DM_IN_BUFFER | DM_PROMPT | DM_OUT_BUFFER);

        if (selectedOption == (long)DialogResult.OK)
        {
            dialogResult = DialogResult.OK;
            printerSettings.SetHdevmode(devModeData);
            printerSettings.DefaultPageSettings.SetHdevmode(devModeData);
        }
        bool unlock = GlobalUnlock(iDevModeNew);
        unlock = GlobalUnlock(devModeData);

        GlobalFree(devModeData);
        GlobalFree(ParentHandle);
       return dialogResult;
    }

}

@agracio agracio closed this as completed Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants