user2982029
user2982029

Reputation: 84

Calling C++ dll from winforms application

I undrstood the process of getting a c++ dll called in a c# console application. Can you please help me in getting the c++ dll called, in one of the button functions of a Form application that I have created in c# again.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // call the c++ dll here.
        }
     }
}

I want the dll to be called in the function call "button3_Click". I tried doing the

[DllImport("LicenseCheck.dll")];
public static extern void GetLicense();

call that stackoverflow taught me, but then that worked only when I tried it on a console application.

Would certainly be happy if someone could help me. Thanks

Upvotes: 0

Views: 2634

Answers (1)

Baldrick
Baldrick

Reputation: 11840

I think you're putting the DllImport statement inline with your code, instead of in the class body.

You need:

public partial class Form1 : Form
{
    [DllImport("LicenseCheck.dll")];
    public static extern void GetLicense();

    public Form1()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        // call the c++ dll here.
        GetLicense();
    }
 }

Please note that for this to work, the bitness of the DLL should match the bitness of your application, otherwise a BadImageFormatException exception will result.

Upvotes: 1

Related Questions