DLL Error with VB .NET Program

This is a discussion on DLL Error with VB .NET Program within the Windows Programming forums, part of the Platform Specific Boards category; Ok, so i was looking around on google about how to incorporate DLL's written in C with VB .NET GUI's ...

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    DLL Error with VB .NET Program

    Ok, so i was looking around on google about how to incorporate DLL's written in C with VB .NET GUI's because its stupidly simple to create one in VB and C is faster. So I want to learn how to mix the two and i found a page of an example thats supposed to do just this (here: http://www.codeproject.com/Purgatory/c_dll_in_vb.asp ). But when I execute the code, I always get this error:
    An unhandled exception of type 'System.EntryPointNotFoundException' occurred in Call_C_dll.exe

    Additional information: Unable to find an entry point named ReturnInParam in DLL vcdll.dll.
    VB Code:
    Code:
    Public Class Form1
        Inherits System.Windows.Forms.Form
     _
    | Windows Form Designer Generated Code |
     -
    
        <DllImport("vcdll.dll", CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Sub ReturnInParam(ByRef Stan As Integer, _
            ByRef Message As String)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Num As Integer = 8
            Dim Message As String = "Hey"
            ReturnInParam(Num, Message)
            MessageBox.Show(Message)
        End Sub
    End Class
    C Code:
    Code:
    #include <WINDOWS.H>
    
    
          LPCSTR DisplayStringByVal(LPCSTR pszString)
          {
              return "How's it goin? ";
          }
    
          
          void ReturnInParam(int* pnStan, char** pMsg)
          {
               long *buffer;
               char text[7] = "Hallo ";
               char name[sizeof(*pMsg)];
               
               strcpy(name, *pMsg);
               *pnStan = *pnStan + 5;
               
               buffer = (long *)calloc(sizeof(text)+sizeof(*pMsg), sizeof( char ) );
               *pMsg = (char *)buffer;
               
               // do not free the buffer, because it will be used by the caller
               //   free( buffer );
               strcpy(*pMsg, text);
               strcat(*pMsg, name);
          }
    the name and path of the dll is correct so whats wrong? thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Before you compile the C file there is a final step to do:
    define the library and function in Simple.def file as follows:
    Code:
    LIBRARY Simple
          DESCRIPTION 'Sample C DLL for use with .NET'
          EXPORTS
            DisplayStringByVal
            ReturnInParam
    This tell the VB where to locate the function Entry Point. If you don't provide this declaration then you will get message just like this
    Code:
    An unhandled exception of type 'System.EntryPointNotFoundException' occurred in Call_C_dll.exe
    
    Additional information: Unable to find an entry point named ReturnInParam in DLL E:\Temp\simple.dll.
    Can we assume you have a DEF file included in your project and have changed the library name to match your DLL project?

  3. #3
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    You need to make sure you're exporting the functions in you C DLL that you need to reference from the outside.

    Also, you may want to do some reading up on the different types of strings.. as you may find that you interchanging ANSI C strings and B strings isn't so trivial

    Good luck.

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Yes, you can assume that. I want to use Dev-C++ as the IDE for writing the DLL's but it seems that tutorials like to cover VC++. So I think I just need to know how to include the .def file while using Dev-C++. I can get things to work while using VC++ but its a student version so everytime you run a program compiled with it, a message box pops up saying "you can't distribute these files for commercial use blah blah blah we here at microsoft are gay blah blah we suck have a nice day" ..something to that effect. lol


    Can you elaborate as to what you mean by exchanging ANSI C and B strings please?


    For some reason, Dev-C++ generates the .def file and this is what it comes out as:
    Code:
    ; dlltool --base-file C:\DOCUME~1\TIMHAN~1\LOCALS~1\Temp/cca03608.base
    --output-exp circlearea_devcpp.exp --dllname circlearea_devcpp.dll
    --output-def libcirclearea_devcpp.def --no-export-all-symbols
    --add-stdcall-alias --exclude-symbol=DllMainCRTStartup@12 --def
    C:\DOCUME~1\TIMHAN~1\LOCALS~1\Temp/cca03608.def
    --output-lib libcirclearea_devcpp.a
    EXPORTS
    but this is how my def file looks from VC++:
    Code:
    EXPORTS
             DisplayStringByVal
    	ReturnInParam
    Last edited by willc0de4food; 04-09-2005 at 05:38 AM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    K so I got bored with their example and wanted to try out what I had gathered from reading things. I made a program to calculate the area of a circle, here's my VB code:
    Code:
    Private Declare Function ReturnVersion Lib "C:\Documents and Settings\Tim Hansen\My Documents\Visual Studio Projects\circlearea_devcpp.dll" () As Long
        Private Declare Function circlearea Lib "C:\Documents and Settings\Tim Hansen\My Documents\Visual Studio Projects\circlearea_devcpp.dll" (ByVal area As Double) As Double
    
    
        Private Sub About_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles About.Click
            ' Report Version Number
            Call MsgBox(ReturnVersion())
        End Sub
    
        Private Sub Bye_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bye.Click
            Application.Exit()
        End Sub
    
        Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
    
            Dim area, radius As Double
    
            radius = InputBox.Text
            area = circlearea(radius)
    
            OutputBox.Text = Str$(area)
    
        End Sub
    
        Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
            InputBox.ResetText()
            OutputBox.ResetText()
        End Sub
    and here's my C++ code:
    Code:
    #include "stdafx.h"
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
        return TRUE;
    }
    
    
    int ReturnVersion(void)
    {
    	int version = 1;
    	return version;
    }
    
    double circlearea(double radius)
    {
    	double area;
    
    	area = 3.1415926535 * (radius * radius);
    
    	return area;
    }
    and finally, here's my .def file:
    Code:
    ; dlltool --base-file C:\DOCUME~1\TIMHAN~1\LOCALS~1\Temp/cca03432.base --output-exp circlearea_devcpp.exp --dllname circlearea_devcpp.dll --output-def libcirclearea_devcpp.def --no-export-all-symbols --add-stdcall-alias --exclude-symbol=DllMainCRTStartup@12 --def C:\DOCUME~1\TIMHAN~1\LOCALS~1\Temp/cca03432.def --output-lib libcirclearea_devcpp.a
    EXPORTS
    the .def file was automatically generated by Dev-C++ and if i modify it by taking out everything before "EXPORTS" and adding ReturnVersion and circlearea after exports, it will not work with the VB interface.
    So I have two questions: How do I get it to work? lol
    And how can I call each function seperately? Because the same code in MSVC++ works but I get a message saying its a limited version of the program and when I ask for the version, it gives me a message that has "circlearea" for the title and then in the message has "7479125..." (probably whatever random junk is chilling in my ram) Do I need to create a seperate cpp file for each function? Thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  6. #6
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Hello,

    The best way for me to elaborate is to point you at a helpful resource.

    Good luck!

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    why thank you
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making a dll in .net 2.0 and than using it in .net 1.1
    By Rune Hunter in forum C# Programming
    Replies: 2
    Last Post: 04-19-2005, 02:59 PM
  2. C++ .NET - "inconsistent dll linkage" - What's that?
    By BrianK in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2004, 10:16 AM
  3. Your opinion about .NET
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-05-2002, 02:55 PM
  4. first DLL program
    By zMan in forum Windows Programming
    Replies: 1
    Last Post: 02-07-2002, 08:56 AM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 03:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21