none
Import de DLL C++ RRS feed

  • Discussion générale

  • Bonjour

    Voila je suis en train d'utiliser une dll C++ en C#.
    Ma première fonction marche correctement la deuxième me renvoi 0 et quand j'interroge lasterror j'ai 0x30000001 qui d’après la documentation est "Argument is not correct"

    using System;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.Text;
    
    namespace APP
    {
        [StructLayout(LayoutKind.Sequential)]
        unsafe internal struct PRINTINFO
        {
            internal uint dwCopies;// Dans la documentation du SDK en C++ j'ai comme type de variable DWORD
            internal uint dwPrintInterval;
            internal uint dwPrintSpeed;
            internal uint dwPageID;
            internal uint dwJobID;
            internal PRINT_IMGINFO pii; // type PRINT_IMGINFO
            internal PRINT_PAPERINFO ppi; // type PRINT_PAPERINFO
            internal PRINT_CCORRECTINFO lppcc;// type LP_PRINT_CCORRECTINFO
            internal PRINT_OVLINFO lppol; // type LP_PRINT_OVLINFO
            internal PRINT_OPTION lppop; //LP_PRINT_OPTION
            internal IntPtr lpReserve1; //LPVOID
            internal IntPtr lpReserve2; //LPVOID
            internal uint dwReserve1; //DWORD
            internal uint dwReserve2; 
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct PRINT_IMGINFO
        {
            internal uint dwSrcType; //DWORD
            internal uint dwResolutionType;//DWORD
            internal uint dwBandHeight; //DWORD
            internal uint dwSrcWidthPixels; //DWORD
            internal uint dwSrcHeightPixels; //DWORD
            internal uint dwRotate; //DWORD
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct PRINT_PAPERINFO
        {
            internal uint dwMediaType; //DWORD
            internal uint dwUnit;//DWORD
            internal uint dwPaperWidth;//DWORD
            internal uint dwPaperHeight; //DWORD
            internal uint dwTopMargin;//DWORD
            internal uint dwBottomMargin; // DWORD
            internal uint dwLeftMargin; // DWORD
            internal uint dwRightMargin; // DWORD
            internal uint dwGapMarkLength; //DWORD
            internal uint dwLeftGap; // DWORD
            internal uint dwRightGap; // DWORD
            internal uint dwPaperID; // DWORD
            internal uint dwEdgelessFlag; // DWORD
        }
    
        [StructLayout(LayoutKind.Sequential)]
        unsafe internal struct PRINT_CCORRECTINFO
        {
            internal uint dwMatchingType; // DWORD
            internal uint dwBrightness; // DWORD
            internal uint dwMediaType; // DWORD
            internal uint dwHalftone; // DWORD
            internal uint dwColorExt; // DWORD
            internal PRINT_CC_REG lppccreg; // LP_PRINT_CC_REG
            internal PRINT_CC_CONDITION lppcccnd; // LP_PRINT_CC_CONDITION
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct PRINT_CC_REG
        {
            internal UCMYK_VAL cmykPlainReg; // UCMYK_VA
            internal UCMYK_VAL cmykPluralReg; // UCMYK_VA
        }
    
        [StructLayout(LayoutKind.Sequential)]
        struct UCMYK_VAL
        {
            internal byte byK; // BYTE
            internal byte byC; // BYTE
            internal byte byM; // BYTE
            internal byte byY; // BYTE
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct PRINT_CC_CONDITION
        {
            internal uint dwSingleType; // DWORD
            internal uint dwLevelCorrect; // DWORD
            internal uint dwSharpness; // DWORD
            internal int lBalanceCyan; // LONG
            internal int lBalanceMagenta; // LONG
            internal int lBalanceYellow; //LONG
            internal int lBalanceBlack; // LONG
            internal int lCCDensity; // LONG
            internal int lContrast; // LONG
        }
    
        [StructLayout(LayoutKind.Sequential)]
        unsafe internal struct PRINT_OVLINFO
        {
            internal uint dwOverlayType; // DWORD
            internal fixed char szFormPath[256]; //TCHAR
            internal uint dwFormID; // DWORD
            internal bool bFormIdFixed; // BOOL
        }
    
        [StructLayout(LayoutKind.Sequential)]
        unsafe internal struct PRINT_OPTION
        {
            internal PRINT_CUTINFO lppct; // LP_PRINT_CUTINFO
            internal uint dwFormFeed; // DWORD
            internal IntPtr lpvReserve1; // LPVOID
            internal IntPtr lpvReserve2; // LPVOID
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct PRINT_CUTINFO
        {
            internal uint dwCutType; // DWORD
            internal uint dwCutByPageNum; // DWORD
            internal uint dwCutInPageNum; // DWORD
            internal uint dwCutConsNum; // DWORD
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct PRINTCMD
        {
            internal byte[] lpbyCmdData; //OUT LPBYTE
            internal uint dwDataSize; //OUT DWORD
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct STATUS_INFO
        {
            internal uint dwSize; // DWORD
            internal uint dwMainStatus; // DWORD
            internal ushort wWarningStatus; // WORD
            internal ushort wErrorStatus; // WORD
            internal ushort wFatalStatus; // WORD
            internal PAGEJOB_INFO cpi; // PAGEJOB_INFO
            internal IntPtr lpcii; // LPVOID
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct STATUS_INFO_2
        {
            internal uint dwSize; // DWORD
            internal uint dwMainStatus; // DWORD
            internal ushort wWarningStatus; // WORD
            internal ushort wErrorStatus; // WORD
            internal ushort wFatalStatus; // WORD
            internal PAGEJOB_INFO cpi; // PAGEJOB_INFO
            internal IntPtr lpcii; // LPVOID
            internal uint dwSpeed; // DWORD
            internal uint dwRemainNum; // DWORD
            internal PRNPRG_INFO cppi; // PRNPRG_INFO
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct STATUS_INFO_3
        {
           internal uint dwSize; // DWORD
            internal uint dwMainStatus; // DWORD
            internal ushort wWarningStatus; // WORD
            internal ushort wErrorStatus; // WORD
            internal ushort wFatalStatus; // WORD
            internal PAGEJOB_INFO cpi; // PAGEJOB_INFO
            internal IntPtr lpcii; // LPVOID
            internal uint dwSpeed; // DWORD
            internal uint dwRemainNum; // DWORD
            internal PRNPRG_INFO cppi; // PRNPRG_INFO
            internal uint dwMyHostID; // DWORD
            internal JOBHIST cjh; // JOBHIST
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct PAGEJOB_INFO
        {
            internal uint dwSerialCount; // DWORD
            internal ushort wTotalPage; // WORD
            internal ushort wMadeCount; // WORD
            internal ushort wSetCount; // WORD
            internal ushort wJobStatus; // WORD
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct INK_INFO_CMYK
        {
            internal byte byBlackInk; // BYTE
            internal byte byCyanInk; //BYTE
            internal byte byMagentaInk; // BYTE
            internal byte byYellowInk; // BYTE
            internal byte byWasteInk; // BYTE
            internal byte byReserved; // BYTE
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct PRNPRG_INFO
        {
            internal ushort wTotalStep; WORD
            internal ushort wCurrentStep; // WORD
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct JOBHIST
        {
            internal uint dwJhHostID; //DWORD
            internal uint dwJobID; // DWORD
            internal ushort wPrntCount; // WORD
            internal ushort wReserve;//WORD
        }
    
        [StructLayout(LayoutKind.Sequential)]
        unsafe internal struct SETTING_INFO
        {
            internal SLPTM_INFO lpcslp; // LP_SLPTM_INFO
            internal HEADADJ_INFO_1 lpchai; // LPVOID
            internal uint[] lpdwDevInfo; // LPDWORD
            internal IntPtr lpclpi; // LPVOID
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct SLPTM_INFO
        {
            internal uint dwSleepTime; // DWORD 
            internal uint dwDeepTime; // DWORD 
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct HEADADJ_INFO_1
        {
            internal CMYK_VAL cmykV; // CMYK_VAL
            internal CMYK_VAL cmykH; // CMYK_VAL
            internal uint dwHeadAdjType; // DWORD 
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct CMYK_VAL
        {
            internal byte cK; //char
            internal byte cC; //char
            internal byte cM; //char
            internal byte cY; //char
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct DEVICE_INFO
        {
            internal ushort wParticulSet; //WORD
            internal byte byOptionSet; //BYTE
            internal byte byDevAbility;//BYTE
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct LATPOS_INFO
        {
            int iCutPos; // int
            int iStopPos; // int
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct END_INFO
        {
            internal uint dwCmdType; // DWORD
            internal uint dwEndStatus; // DWORD
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct RF_INFO
        {
            internal int iSetRFIDNum; //int
            internal RFID_SET crs; // RFID_SET
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct RFID_SET
        {
            internal byte byPageId; // BYTE
            internal byte byUniqueId; // BYTE
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct DATA_INFO
        {
            internal uint dwMainteId; // DWORD
            internal int iGetDataSize; // int
        }
    
        [StructLayout(LayoutKind.Sequential)]
        unsafe internal struct ORGPOS_INFO
        {
            internal TK_INFO lpTKGap1; //LPTK_INFO
            internal TK_INFO lpTKGap2; //LPTK_INFO
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct TK_INFO
        {
            internal int iTPass_TKNum; // int
            internal int iReflec_TKNum; // int
        }
    
        [StructLayout(LayoutKind.Sequential)]
        internal struct SLANTADJ_INFO_2
        {
            internal CMYK_VAL cmykSln1; // CMYK_VAL
            internal CMYK_VAL cmykSln2; // CMYK_VAL
            internal uint dwReserved; // DWORD
        }
    
        [SuppressUnmanagedCodeSecurityAttribute] 
        internal class NativeMethods
        {
            [DllImport("mydll.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "GetDefaultPrintInfo")]
            internal static extern int GetDefaultPrintInfo(string lptPrinterName, out PRINTINFO lpPrtInfo);
            [DllImport("mydll.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "CreatePrintHandle")]
            internal static extern IntPtr CreatePrintHandle(string lptPrinterName, IntPtr lpPrtInfo);
            [DllImport("mydll.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "CreatePrintHandle")]
            internal static extern IntPtr CreatePrintHandle(string lptPrinterName, out PRINTINFO lpPrtInfo);
            [DllImport("mydll.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "StartJobCmd")]
            internal static extern int StartJobCmd(IntPtr hPrinter, PRINTCMD printcmd);
            [DllImport("mydll.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "CreateJobHandle")]
            internal static extern IntPtr CreateJobHandle(string lptPrinterName, IntPtr lpPrtInfo);
            [DllImport("mydll.dll", CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "OpenPort")]
            internal static extern IntPtr OpenPort(string lptPrinterName, string lptPortName, bool bAttribute);
        }
    }

    Mon code d'execution:

    NativeMethods.GetDefaultPrintInfo(nomImprimante, out DefaultPrintInfo);
                idPort = NativeMethods.OpenPort(nomImprimante, nomPort, false);
                Console.WriteLine("ID Port: " + idPort + " " + Marshal.GetLastWin32Error().ToString("X"));
                int length = Marshal.SizeOf(typeof(PRINTINFO));
                IntPtr printInfoPtr = Marshal.AllocHGlobal(length);
                Marshal.StructureToPtr(DefaultPrintInfo, printInfoPtr,true);
                idPrinter = NativeMethods.CreatePrintHandle(nomImprimante, printInfoPtr);
                Console.WriteLine("ID Printer: " + idPrinter + " " + Marshal.GetLastWin32Error().ToString("X"));
                idPrinter = NativeMethods.CreatePrintHandle(nomImprimante, out DefaultPrintInfo);
                Console.WriteLine("ID Printer: " + idPrinter + " " + Marshal.GetLastWin32Error().ToString("X"));


    Désolé pour la longueur mais j'ai essayé de garder ce qui me semblait nécessaire pour la compréhension.
    Si vous voyez des erreurs ou si vous avez une autre solution je suis preneur merci.


    • Modifié TheDarkBoz vendredi 25 juillet 2014 14:30
    • Type modifié Aurel Bera lundi 4 août 2014 07:38 disc
    jeudi 24 juillet 2014 10:10

Toutes les réponses