C# syntax for encrypt.dll, an attempt was made to load a program with the incorrect format

my syntax is as follows,

[DllImport("encryptpdf.dll", EntryPoint = "veryEncryptPDF")]
public static extern long EncryptPDF(string inFileName, string outFileName, long EncryptLen, long permission, string OWNER, string user);

however when I try to use it i get an error 'An attempt was made to load a program with the incorrect format'.

I assume the problem is in my declaration syntax. Please provide a correction.

I changed my declaration to,

[DllImport("encryptpdf.dll", EntryPoint = "veryEncryptPDF")]
public static extern long VeryEncryptPDF(string inFileName, string outFileName,long EnctyLen,long permission, string OwnerPassword, string UserPassword);

and still get the same error, but if I change the target platform to x86 from AnyCPU, I get this error,

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

Strangely, it feels like progress, but it would be nice if you had more support examples on how your dll works. 


We have created a C# project for you, please test the C# project in attachment.

btw, you can change from "long" to "int" or "integer" in the declaration, you will get it work properly, for example,

[DllImport("encryptpdf.dll", EntryPoint = "veryEncryptPDF")]
public static extern int VeryEncryptPDF(string inFileName, string outFileName,int EnctyLen,int permission, string OwnerPassword, string UserPassword);

below is the full C# example,

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.IO;


namespace example_CSharp


    public partial class Form1 : Form


        public Form1()





        [DllImport("encryptpdf.dll", EntryPoint = "veryEncryptPDF")]

        public static extern int EncryptPDF(string strInFileName, string strOutFileName,
int nEncryptLen, int nPermission, string
string strUserPassword);


        int GetPermission(bool Print, bool Copy, bool Modify, bool EnableAcroFormFields)


            int permission = 0;


            if (Print)

                permission |= 0x04;

            if (Copy)

                permission |= 0x10;

            if (Modify)

                permission |= 0x08;

            if (EnableAcroFormFields)

                permission |= 0x20;


            return permission;



        private void button1_Click(object sender, EventArgs e)


            string appPath = Path.GetDirectoryName(Application.ExecutablePath);

            string strInPDFFile = appPath + "\\test1.pdf";

            string strOutPDFFile = appPath + "\\_test1_encrypted.pdf";

            int nEncyptLevel = 128;

            int nPermission = GetPermission(true, true, true, true);

            EncryptPDF(strInPDFFile, strOutPDFFile, nEncyptLevel, nEncyptLevel,
            "123", "456");







VN:F [1.9.20_1166]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.20_1166]
Rating: 0 (from 0 votes)

Related Posts

This entry was posted in PDF Security and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verify Code   If you cannot see the CheckCode image,please refresh the page again!