Solved

Using a C DLL in VB.NET

Posted on 2004-08-16
9
921 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
I really need to get my brain on straight today:

   __declspec(dllexport) int WINAPI MyFunc(int, int);
   __declspec(dllexport) void WINAPI MySub(int);
0
 

Author Comment

by:mlazear
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 19

Expert Comment

by:drichards
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Okay, I figure it out.  I have my solution, you have your points.

Thanks again,
Mike
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now