简体   繁体   English

在C#中导入C ++ DLL AccessViolationException

[英]Importation C++ dll in C# AccessViolationException

I use one dll write in c++ in my program C# 我在程序C#中使用C ++编写的一个dll

I have some problem, when I execute it, I have AccessViolationException for my line 我有一些问题,当我执行它时,我的行有AccessViolationException

XIJET_GetPrinterParameter(ID_Printer, XIJET_RESOLUTION, parametre); XIJET_GetPrinterParameter(ID_Printer,XIJET_RESOLUTION,参数);

using System;
using System.Collections;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;
using System.IO;
namespace ConsoleApplication1
{
    public class Program
    {
        ushort XIJET_RESOLUTION;//300*300 fast
        ushort XIJET_TRIGGER_OFFSET;//2 inches
        ushort XIJET_AUX_OUTPUT;
        ushort XIJET_QUEUE_DEPTH=4;//4
        ushort XIJET_SUB_SAMPLE;
        ushort XIJET_JET_MASKING;
        ushort XIJET_HEAD_HEIGHT;//0.5
        uint XIJET_TRIGGER_MASK;
        public struct XIJET_CARTRIDGE_PROFILE_STRUCT
        {
            public ushort voltage; // cartridge voltage, see unit table below
            public ushort preFirePulseWidth; // Lexmark only, nSec units
            public ushort gapWidth; // Lexmark only, nSec units
            public ushort pulseWidth; // main fire pulse, see unit table below
            public ushort temperature; // degrees C
        };
        public struct XIJET_CONFIGURATION
        {
            public ushort VerticalResolutionDPI; // printhead resolution
            public ushort HorizontalResolutionDPI; // transport resolution
            public ushort NumberOfPrintheads; // up to 4 printheads may be attached
            public ushort Head1Height; // height of each printhead in pixels
            public ushort Head2Height;
            public ushort Head3Height;
            public ushort Head4Height;
         };
        #region Importation
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern bool XIJET_ProbePrinter(ushort index,
           StringBuilder PrinterName);
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern IntPtr XIJET_OpenPrinter(StringBuilder PrinterName,
            ArrayList pPrinterConfig);
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern void XIJET_ClosePrinter(IntPtr PrinterHandle);
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern bool XIJET_GetPrinterResolution(List<char> PrinterName,
            ushort ResolutionIndex,
            List<char> ResolutionDescription,
            List<ushort> HorizontalDPI,
            List<ushort> VerticalDPI,
            List<ushort> MaxSpeedIPS);
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_GetPrinterParameter(IntPtr PrinterHandle,
            uint ParameterIndex,
            IntPtr pParameter);
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_SetPrinterParameter(IntPtr PrinterHandle,
            uint ParameterIndex,
            IntPtr pParameter);
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_PrintDocumentPage( IntPtr PrinterHandle,
            uint HorizontalWidth, // in pixels
            uint VerticalHeight, // in pixels
            byte[] pBitmappedBuffer, // 1-bit per pixel image
            uint Head1Offset, // Vertical offset in pixels
            uint Head2Offset, // Vertical offset in pixels
            uint Head3Offset, // Vertical offset in pixels
            uint Head4Offset, // Vertical offset in pixels
            uint Timeout ); // timeout in msecs
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_CanvasBegin( IntPtr PrinterHandle, // as returned from open call
            uint VerticalHeight, // in pixels
            uint HorizontalWidth); // in pixels
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_CanvasWrite( IntPtr PrinterHandle, // as returned from open call
            uint VerticalOffset, // in pixels
            uint HorizontalOffset,// in pixels
            uint VerticalHeight, // in pixels
            uint HorizontalWidth, // in pixels
            byte[] pBitmappedBuffer); // 1-bit per pixel image
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]        
        public static extern IntPtr XIJET_LoadFontXFT( IntPtr PrinterHandle, // as returned from open call
            List <char> filename); // full pathname to XFT font file
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_CanvasWriteStr( IntPtr PrinterHandle, // as returned from open call
            IntPtr FontHandle, // as returned from XIJET_LoadFont
            List <char> printString, // pointer to string to print
            uint VerticalOffset, // in pixels
            uint HorizontalOffset); // in pixels
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_CanvasPrint ( IntPtr PrinterHandle, // as returned from open call
            uint Head1Offset, // Vertical offset in pixels
            uint Head2Offset, // Vertical offset in pixels
            uint Head3Offset, // Vertical offset in pixels
            uint Head4Offset, // Vertical offset in pixels
            uint Timeout ); // timeout in msecs
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_CanvasPrintExt2 ( IntPtr PrinterHandle, // as returned from open call
            uint Head1Offset, // Vertical offset in pixels
            uint Head2Offset, // Vertical offset in pixels
            uint Head3Offset, // Vertical offset in pixels
            uint Head4Offset, // Vertical offset in pixels
            uint Timeout, // timeout in msecs
            List<float> pTransportSpeedIPS); // Returned transport speed
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_WaitForPrintComplete( IntPtr PrinterHandle, // as returned from open call
            uint Timeout); // Timeout in msecs
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern void XIJET_Reset( IntPtr PrinterHandle );
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern void XIJET_ResetPrintData(IntPtr PrinterHandle );
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern void XIJET_ResetPrintQueue(IntPtr PrinterHandle );
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_GetStatus( IntPtr PrinterHandle, // as returned from open call
            IntPtr pStatusMessage); // returned text message
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_ActivatePens( IntPtr PrinterHandle );
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_DeactivatePens( IntPtr PrinterHandle );
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_SelectPens( IntPtr PrinterHandle, // as returned from open call
            uint bmPensSelect ); // bit mapped pens selector
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_GetInkRemaining( IntPtr PrinterHandle, // as returned from open call
            ushort headIndex, // zero-based index of head number
            List<short> remainingPen1, // returned SHORT
            List<short> remainingPen2,
            List<short> remainingPen3,
            List<short> remainingPen4);
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_ResetInkCartridge( IntPtr PrinterHandle, // as returned from open call
            ushort headIndex, // zero-based index of head number
            ushort penIndex); // zero-based index of pen number
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern int XIJET_QueueOutputToggle( IntPtr PrinterHandle, // as returned from open call
            ushort outputIndex, // zero-based index of head number
            ushort outputBitmask, // bit 0 = new state of output
            ushort toggleTimeMS, // time in milliseconds (0=permanent)
            ushort syncOption = 0, // 0 = immediate
            ushort syncDelay = 0 ); // synchronize delay (time or distance)
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern IntPtr XIJET_OpenLogFile(String filename); // filename including full path;
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern void XIJET_CloseLogFile( );
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern uint XIJET_TestUSB( IntPtr PrinterHandle); // as returned from open call
        [DllImport("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/XIJET_API.dll")]
        public static extern double XIJET_GetVersionAPI( );
        #endregion
        public static void Main()
        {
            String Jour = DateTime.Now.Day.ToString();
            String Mois = DateTime.Now.Month.ToString();
            String Annee = DateTime.Now.Year.ToString();
            String Log = "C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/" + Jour + "-" + Mois + "-" + Annee + ".txt";
            IntPtr Fichier = XIJET_OpenLogFile(Log);
            ushort index=0;
            ImageConverter imageConverter = new ImageConverter();
            Bitmap image=new Bitmap("C:/Users/Eren/documents/visual studio 2012/Projects/ConsoleApplication1/ConsoleApplication1/noir.bmp");
            byte[] imageByte = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
            uint Head1Offset=250;
            uint Head2Offset=0;
            uint Head3Offset=0;
            uint Head4Offset=0;
            uint Timeout=1000;
            StringBuilder PrinterName=new StringBuilder();
            XIJET_ProbePrinter(index, PrinterName);
            Console.WriteLine(PrinterName+" son index est "+index);
            ArrayList Configs = new ArrayList();
            XIJET_CONFIGURATION Config0 = new XIJET_CONFIGURATION();
            Configs.Add(Config0);
            IntPtr ID_Printer= XIJET_OpenPrinter(PrinterName, Configs);
            Console.WriteLine("Ouverture fichier " + Fichier);
            Console.WriteLine("L'ID de l'imprimante: "+ID_Printer+" est ouverte");
            uint XIJET_RESOLUTION = 2;
            IntPtr parametre = new IntPtr();
            //Console.WriteLine(ID_Printer + " " + XIJET_RESOLUTION + " " + parametre);
            int test= XIJET_GetPrinterParameter(ID_Printer, XIJET_RESOLUTION, parametre);
            Console.WriteLine("eureka  "+parametre);
           /* uint selection_cartouche = 0x01;
            int activation_cartouche = XIJET_SelectPens(ID_Printer, selection_cartouche);*/
            int retour = XIJET_ActivatePens(ID_Printer);
            Console.WriteLine("Activation des cartouches: " + retour);
            uint VerticalHeight=200,HorizontalWidth=200;
            int Page=XIJET_PrintDocumentPage( ID_Printer,VerticalHeight,HorizontalWidth, imageByte, Head1Offset, Head2Offset, Head3Offset,Head4Offset, Timeout );
            Console.WriteLine("Page chargée " + Page);
            uint VerticalOffset=50, HorizontalOffset=50;

            while (true)
            {
                XIJET_PrintDocumentPage(ID_Printer, VerticalHeight, HorizontalWidth, imageByte, Head1Offset, Head2Offset, Head3Offset, Head4Offset, Timeout);
            }
            uint usb = XIJET_TestUSB(ID_Printer);
            Console.WriteLine("usb: "+usb);
            XIJET_ClosePrinter(ID_Printer);
            Console.WriteLine(XIJET_GetVersionAPI());
            XIJET_CloseLogFile();
        }
    }
}

Do you have some idea? 你有什么主意吗 The extract from documentation is 文档摘录为

int XIJET_GetPrinterParameter ( HANDLE PrinterHandle, // as returned from open call
    USHORT ParameterIndex,
    PVOID pParameter,
    USHORT HeadNumber = 0); // when applicable
    Returns: 1 = success
    0 = error
    XIJET_GetPrinterParameter will return a single printer configuration parameter. ParameterIndex dictates which
    parameter will be returned, while pParameter points to an appropriately sized memory field. Defined constants
    for ParameterIndex are located in XIJET_API.h. The optional parameter HeadNumber should only be included
    for head specific parameters, such as trigger offset distance.
    For example to read the printers print queue depth setting:
    int Success;
    USHORT value;
    Success = XIJET_GetPrinterParameter( printerHandle, XIJET_QUEUE_DEPTH, &value);
    See Appendix A for list of adjustable printer parameters.

You can see all documentation and dll at http://eren.atolgan.fr/kelenntechnology 您可以在http://eren.atolgan.fr/kelenntechnology上查看所有文档和dll。

Accordind to the C++ doc, the method signature should be: 根据C ++文档,方法签名应为:

 public static extern int XIJET_GetPrinterParameter(IntPtr printerHandle,
            Uint16 parameterIndex,
            IntPtr pParameter, UInt16 headNumber);

you can see the type conversion table here: 您可以在此处查看类型转换表:
http://msdn.microsoft.com/en-us/library/ac7ay120(v=vs.110).aspx http://msdn.microsoft.com/zh-CN/library/ac7ay120(v=vs.110).aspx

I have struggled with this myself, however I did find a solution: 我本人为此感到挣扎,但是我确实找到了解决方案:

Public Declare Function XIJET_SetPrinterParameter Lib "XIJET_API" (ByVal printerHandle 
   As IntPtr, ByVal parameterIndex As UShort, ByRef pParameter As UShort,
   ByVal HeadNumber As UShort) As Short

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#访问非托管C ++ DLL时出现AccessViolationException - AccessViolationException when accessing unmanaged C++ DLL with C# (C#)从C ++ DLL中获取char **时出现AccessViolationException - (C#) AccessViolationException when getting char ** from C++ DLL 使用非托管C ++ DLL的AccessViolationException - AccessViolationException using unmanaged C++ DLL C#使用VB6-Dll-AccessViolationException - C# Using VB6-Dll - AccessViolationException 导入:在C#下使用C ++库的语法翻译 - Importation: Syntax translation for using C++ library under C# 创建C#到C ++的桥梁:为什么在调用DLL时会出现AccessViolationException? - Creating C# to C++ bridge: Why do I get a AccessViolationException when calling DLL? 将数组移交给 C# 中动态加载的 C++ DLL 时出现 System.AccessViolationException - System.AccessViolationException when handing an array over to a dynamically loaded c++ DLL in C# 试图删除 c++ dll 中的 c# 数组。 获取 System.AccessViolationException - Trying to delete c# array in c++ dll. Getting System.AccessViolationException AccessViolationException 在使用 c++ dll 和 ZD7EFA19FBE7D32372FDZADB602EZ dll 中的 char * 参数访问 function 时出现 AccessViolationException - AccessViolationException when accessing function with char * argument in c++ dll with C# Visual C#在包装的C ++ \\ CLI dll中调用Lapack时抛出System.AccessViolationException - System.AccessViolationException thrown by Visual C# on call to Lapack inside wrapped C++\CLI dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM