在C#AccessViolationException中导入C ++ DLL

时间:2014-01-23 17:15:52

标签: c# c++ dll

我在程序C#

中使用c ++编写一个dll

我有一些问题,当我执行它时,我的行

有AccessViolationException

XIJET_GetPrinterParameter(ID_Printer,XIJET_RESOLUTION,parametre);

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();
        }
    }
}
你知道吗?文档摘录是

    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.

您可以在http://eren.atolgan.fr/kelenntechnology

查看所有文档和dll

2 个答案:

答案 0 :(得分:0)

根据C ++ doc,方法签名应为:

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

您可以在此处查看类型转换表:
http://msdn.microsoft.com/en-us/library/ac7ay120(v=vs.110).aspx

答案 1 :(得分:0)

我自己一直在努力,但我确实找到了解决方案:

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