Avatar of Rama Tito
Rama Tito
Flag for Malaysia asked on

DllImport - vb6 dll into C#

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

C#

Avatar of undefined
Last Comment
Rama Tito

8/22/2022 - Mon
kaufmed

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 Tito

ASKER
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

kaufmed

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
kaufmed

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 Tito

ASKER
kaufmed

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Rama Tito

ASKER
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.
kaufmed

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.
Russell_Venable

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.



All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
kaufmed

@Russell_Venable
The ComVisible attribute is for exposing your methods to COM, not for importing methods from another DLL.
Russell_Venable

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.
kaufmed

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"  ; )
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Rama Tito

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Rama Tito

ASKER
I did manage source the information form another forum.