Using a C DLL in VB.NET

I have a simple DLL written in C that accesses a binary database.  I am writing a VB.NET front-end that is suppose to get to the database via the DLL.  (I do have source of the DLL and the database layout).  I have quite a few years programming experience in C and VB 6 but almost no experience in VB.NET.

The DLL is EnviroDll.dll

I have been unable to get VB.NET to like my function declaration:
Public Declare Function EnviroOpen Lib "EnviroDll.dll" Alias "_EnviroOpen@4" (ByVal fileName As String) As Long

I realize that I have to watch out for the size of types, which I will worry about next.

Questions:
1) What do I have to set up to make a VC DLL work within VB.NET?
In Particular:
2) Do I have to add EnviroDll.dll as a Reference or as an Import somewhere in the Projects properties or source file?

3) What is the correct format for the "Public Declare"?
(I've tried lots of combinations and looked at other code but still couldn't come up with a correct combination)



mlazearAsked:
Who is Participating?
 
drichardsCommented:
Here's exactly what I did.

File Module1.vb (in VB project):

    Module Module1
        Public Declare Function Add2 Lib "C:\Data\Projects\MyDll\Debug\mydll.dll" (ByVal addend As Long) As Double
        Public Declare Sub PrintIt Lib "C:\Data\Projects\MyDll\Debug\mydll.dll" (ByVal arg As Long)
    End Module

In button handler on VB form:

        Dim c As Long = 4
        Dim x As Double = Add2(c)
        TextBox1.Text = x.ToString()
        PrintIt(17)


Then the C++ dll  code:

    __declspec(dllexport) double __stdcall Add2(int a)
    {
        std::cout << a << std::endl;
        return (double)(a + 2);
    }

    __declspec(dllexport) void __stdcall PrintIt(int a)
    {
        std::cout << a << std::endl;
    }
0
 
drichardsCommented:
>> 1) What do I have to set up to make a VC DLL work within VB.NET?
Create a regular dll and export the functions you want to use from VB:

  int WINAPI __declspec(export) MyFunc(int, int);
  void WINAPI __declspec(export) MySub(int);

>> In Particular:
>> 2) Do I have to add EnviroDll.dll as a Reference or as an Import somewhere in the Projects properties or source file?
No, the Declare statement takes care of loading the dll and locating the functions.  Only if you make a COM dll do you use the Reference approach.

3) What is the correct format for the "Public Declare"?
Public Declare Function MyFunc Lib "<path to dll>" (a1 As Integer, a2 As Integer) As Integer
Public Declare Sub MySub "<path to dll>" (a1 As Integer)


Here's an MS link that has more (confusing) details...

http://support.microsoft.com/default.aspx?scid=kb;en-us;106553
0
 
drichardsCommented:
These:

 int WINAPI __declspec(export) MyFunc(int, int);
  void WINAPI __declspec(export) MySub(int);

should be:

 int WINAPI __declspec(dllexport) MyFunc(int, int);
  void WINAPI __declspec(dllexport) MySub(int);
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
drichardsCommented:
I really need to get my brain on straight today:

   __declspec(dllexport) int WINAPI MyFunc(int, int);
   __declspec(dllexport) void WINAPI MySub(int);
0
 
mlazearAuthor Commented:
The DLL (C portion) I already have done and working.  I can access it from C and I can access it from VB.  I just can't seem to access it from VB.NET.

What you have described for the Public Declare ... is what I have.  I obviously don't have my "brain on straight" either since it is probably something really dumb.  Originally I was just looking at the squiggles under the text to tell me that what I had was invalid.  Since then I've compiled.  The error message I'm getting is:  "Statement is not valid in a namespace".    However, the Declare is the first line of code in the file, with the exception of a few comments.

Is there some kind of wrapper or something I need at the file?  It actually sounds like I need to remove something but there is nothing at the beginning of the filel to remove.

What should the extension be for the file that contains the Declares?

0
 
drichardsCommented:
Where are you putting the Declare statements.  They need to go in the module file.

And you should add ByVal in front of any function params in the declare or else it tries to pass by ref and you'll need things like int* as params in your C++.

I'll try this with VB.NET - I usually use PInvoke now, though I would assume it is doing exactly the same thing as Declare does.
0
 
drichardsCommented:
Although I suppose to make the Integer lengths match, it should more properly look like this:

    Module Module1
        Public Declare Function Add2 Lib "C:\Data\Projects\MyDll\Debug\mydll.dll" (ByVal addend As Integer) As Integer
        Public Declare Sub PrintIt Lib "C:\Data\Projects\MyDll\Debug\mydll.dll" (ByVal arg As Integer)
    End Module

and:

        Dim c As Integer = 4
        Dim x As Integer = Add2(c)
        TextBox1.Text = x.ToString()
        PrintIt(17)

and:

    __declspec(dllexport) int __stdcall Add2(int a)
    {
        std::cout << a << std::endl;
        return a + 2;
    }

    __declspec(dllexport) void __stdcall PrintIt(int a)
    {
        std::cout << a << std::endl;
    }
0
 
mlazearAuthor Commented:
You got it!!!

>>>
   Module Module1
        Public Declare Function Add2 Lib "C:\Data\Projects\MyDll\Debug\mydll.dll" (ByVal addend As Long) As Double
        Public Declare Sub PrintIt Lib "C:\Data\Projects\MyDll\Debug\mydll.dll" (ByVal arg As Long)
    End Module
<<<

I had the declartions in a .vb file but I didn't not have them defined with in a "Module" , "End Module" bracketing.  Frankly I didn't even know about those.

Thank you very much for the solution!!!   Now I'm on to the part about getting the types correct, which you've already given me some information on.

This is my first time on ee so I'm not sure how I flag this so you get your points.

Thank you for your help,,
Mike



0
 
mlazearAuthor Commented:
Okay, I figure it out.  I have my solution, you have your points.

Thanks again,
Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.