Solved

Using a C DLL in VB.NET

Posted on 2004-08-16
9
928 Views
Last Modified: 2012-08-13
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)



0
Comment
Question by:mlazear
  • 6
  • 3
9 Comments
 
LVL 19

Assisted Solution

by:drichards
drichards earned 500 total points
ID: 11813461
>> 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
 
LVL 19

Expert Comment

by:drichards
ID: 11813468
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
 
LVL 19

Expert Comment

by:drichards
ID: 11813483
I really need to get my brain on straight today:

   __declspec(dllexport) int WINAPI MyFunc(int, int);
   __declspec(dllexport) void WINAPI MySub(int);
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:mlazear
ID: 11813717
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
 
LVL 19

Expert Comment

by:drichards
ID: 11813808
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
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 11813984
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
 
LVL 19

Expert Comment

by:drichards
ID: 11814080
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
 

Author Comment

by:mlazear
ID: 11814741
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
 

Author Comment

by:mlazear
ID: 11814775
Okay, I figure it out.  I have my solution, you have your points.

Thanks again,
Mike
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question