Solved

Using a C DLL in VB.NET

Posted on 2004-08-16
9
939 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

631 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