Good afternoon, I'm developing an application that needs to make sure that the document was printed on the bematech MP-4200 TH non-fiscal thermal printer, as the printer has a buffer even if it doesn't have paper in it, it sends the command to the printer and returns as ok, that way I have no way of knowing if it was actually printed or if it was just sent to the printer, checking the class has a function that according to the documentation makes the application wait for the printing to finish.
/// <summary> /// Esta função segura a execução do Aplicativo, até que todo o texto enviado seja impresso. /// </summary> /// <param name="modo">INTEIRO modo de espera.</param> /// <returns>INTEIRO - Indica se a função conseguiu enviar o comando para impressora.</returns> [DllImport("MP2032.dll")] public static extern int EsperaImpressao(int modo);
However this is not working returning that it has unbalanced the stack.
This type of error is usually linked to something known as the Calling Convention and the
[DllImport] attribute has a property in which you define which mode should be used for the imported function.
When you don't specify any, C# sets by default StdCall and it shouldn't be like the dll you use was compiled, see the link above for the other values you can use, the most used types are StdCall and Cdecl.
It would be something like:
[DllImport("MP2032.dll", CallingConvention=CallingConvention.Cdecl)] public static extern int EsperaImpressao(int modo);