DllImport - vb6 dll into C#

Rama Tito
Rama Tito used Ask the Experts™
on
I did face error on calling VB6 dll into C#. DllNotFoundExeption was unhandled.
Please do advice.
' File : VB6
' Project Name : ProjectArea
' Class Name : ClassArea


Public Function Area(x As Integer, y As Integer) As Integer
 Area = x * y
End Function


##################################################################

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;

namespace Area_in_dll
{
    public partial class Form1 : Form
    {
        [DllImport("ProjectArea.dll",CharSet =  CharSet.Auto ,ExactSpelling = true)]
        public static extern Int16 Area(Int16 x, Int16 y) ;
        public Form1()
        {
            InitializeComponent();       
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int z;
            z = Area(5, 6);
            textBox1.Text = Convert.ToString(z);
           
        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Is "ProjectArea.dll" in the bin folder (Debug/Release depending) of your application? If not, you'll need to put the full path in your DllImport statement.
Rama TitoProgrammer

Author

Commented:
Yes it not in the the bin folder. I did add up the path, but not sure and error still pop up. Please do advice.

namespace AreaCalling
{
    public partial class Form1 : Form
    {
        [DllImport("ProjectArea.dll", SetLastError = true)]
        private static extern IntPtr LoadLibrary(string DllName);

        [DllImport("ProjectArea.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern Int16 Area(Int16 x, Int16 y);
      

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int z;

            IntPtr pDll = LoadLibrary(@"E:\C#\Pro Area\ProjectArea.dll");
            
            
            z = Area(5, 6);
            textBox1.Text = Convert.ToString(z);
        }
    }
}

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Is there a function called LoadLibrary in the "ProjectArea.dll"? Also, I was referring to adding the path in your DllImport lines.

i.e.
[DllImport("E:\C#\Pro Area\ProjectArea.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary(string DllName);

[DllImport("E:\C#\Pro Area\ProjectArea.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern Int16 Area(Int16 x, Int16 y);

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Actually, a slight mod is needed:
[DllImport(@"E:\C#\Pro Area\ProjectArea.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary(string DllName);

[DllImport(@"E:\C#\Pro Area\ProjectArea.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern Int16 Area(Int16 x, Int16 y);

Open in new window

Rama TitoProgrammer

Author

Commented:
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Is the function Area exposed for use outside of the library? I've never written a VB6 DLL that was used in .NET, but I thought all functions that can be used by external code must be decorated in some special way in order to be used externally.

Is this DLL exposed as a COM library?
Rama TitoProgrammer

Author

Commented:
Actually i am just trying out to see how its works. I just create a simple area calculation in vb and then convert in dll file. I did try this dll in vb6, its works fine. Then i want to try in C#.

I didn't get what you mean by DLL exposed as COM Library.
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Actually i am just trying out to see how its works. I just create a simple area calculation in vb and then convert in dll file.
Well how you're importing it into C# is correct. The problem you have is that the Area isn't exported properly. I haven't created any DLLs in VB6, so I am unable to tell you how to export the function properly. You may be able to find a tutorial describing such, or perhaps someone else can pop in an describe the process.

I didn't get what you mean by DLL exposed as COM Library.
COM is the Component Object Model technology developed by Microsoft. I'm not 100% certain you need to make the DLL COM-enabled, but you might. COM is a royal pain to develop, from what I understand.
Don't you need to enable COM access by adding [ComVisible(true)]
[DllImport("ProjectArea.dll",CharSet =  CharSet.Auto ,ExactSpelling = true)]
        public static extern Int16 Area(Int16 x, Int16 y);

When I reference a COM Dll from a different language into .NET I make sure I have the exact call that the dll uses. Are you sure your call isn't just int Area(int x, int y);

Try using it this way.

Don't you need to enable COM access by adding [ComVisible(true)]
[DllImport("ProjectArea.dll",CharSet =  CharSet.Auto ,ExactSpelling = true)]
        public static extern int Area(int x, int y);

Then use it as you would.



kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
@Russell_Venable
The ComVisible attribute is for exposing your methods to COM, not for importing methods from another DLL.
I know what it does. It opens the dll up for COM access it allowed me to import firewall methods from it's COM dll so I don't Know what your on about.
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I don't Know what your on about.
If you read the question, then I'm sure you'll see what I'm "on about"  ; )
Programmer
Commented:
HI, I will try out again and get back if the problem still assists. Cheers
Rama TitoProgrammer

Author

Commented:
I did manage source the information form another forum.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial