Call Visual C++ DLL using VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danishce
    New Member
    • Apr 2007
    • 31

    Call Visual C++ DLL using VB.NET

    I getting error on calling DLL that was made in VC++ 6 from my vb.net application . i am right clicking on the project in Solution explorer, and trying to add a reference. In the reference window,
    under the browse tab, i try to add a reference to my dll. I have also copied that dll into the project directory.
    But when i select the dll and click on ok i get a message box saying"A reference to the dll could not be added.
    Make sure the file is accessible, and it is a valid assembly or COM component"

    I think the dll is native dll.Then how i use pinvoke to access the functions
    in it.The dll was made in VC++ 6.0.The name of the DLL is MyDll.dll and its function name is SayHello() which returns the string value.
    Any help.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Did you make the function COMvisible?
    In the .def file for your .dll project, do you have things like:
    Code:
    EXPORTS
        ; Explicit exports can go here
    	DllCanUnloadNow     PRIVATE
    	DllGetClassObject   PRIVATE
    	DllRegisterServer   PRIVATE
    	DllUnregisterServer PRIVATE
    	SayHello
    I could only ever get MFC c++ .dll files to work, which stinks because i only had a few tiny functions but the mfc made it over a 1meg.

    Comment

    • danishce
      New Member
      • Apr 2007
      • 31

      #3
      My code for calling Visual C DLL named "MYDll.dll" in vb.net is:

      <System.Runtime .InteropService s.DllImport("My Dll.dll", _
      SetLastError:=F alse)> _
      Public Shared Function SayHello(ByVal T_VININ As String) As String
      End Function

      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
      Dim tes As String
      tes = SayHello("Hello World")
      End Sub

      however when i compile the code i am getting error :
      "Unable to find an entry point name SayHello in DLL MyDll.dll"

      how can i resolve this?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by danishce
        however when i compile the code i am getting error :
        "Unable to find an entry point name SayHello in DLL MyDll.dll"
        how can i resolve this?
        Again, go to your DLL project and re-compile it making sure you have something like:
        Code:
        EXPORTS
            ; Explicit exports can go here
        	DllCanUnloadNow     PRIVATE
        	DllGetClassObject   PRIVATE
        	DllRegisterServer   PRIVATE
        	DllUnregisterServer PRIVATE
        	SayHello
        So that there is an entry point called SayHello in your function. It can't find an entry point because the dll was probably created without one.

        Comment

        • danishce
          New Member
          • Apr 2007
          • 31

          #5
          Yes it works. but how i pass vb.net buffer array to visual C pointer variable.I want to set the value of m_buffer variable which pass value to dll pointer variable buffer but it gives me the NullException error.(object reference not set to an instance of an object).my code is:

          Imports System.Runtime. InteropServices
          Dim m_buffer(1024) As Byte

          <DllImport("myd ll.dll", CallingConventi on:=CallingConv ention.StdCall) > _
          Private Shared Function pckInitPack(ByV al idcode As Integer, ByRef buffer() As Byte) As Byte()
          End Function

          //form_load
          pckInitPack(100 , m_buffer)


          if i use byval it set m_buffer value to zero.please guide me.
          Thanks

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I am unsure if you can correctly pass pointers like that without marking things as "UNSAFE". And I have never gotten unsafe/pointers to work for me, maybe one of the other people can weigh in on this?

            Comment

            • danishce
              New Member
              • Apr 2007
              • 31

              #7
              Thanks all.its working

              Comment

              • anishait
                New Member
                • Feb 2008
                • 1

                #8
                i have designed a frontend in .NET(using c#)(GUI application).My problem is in calling the vc++ functions from .net.
                When i click the buttons(GUI application) the correspondinf vc++ functions
                must be invoked.
                Any help plzzz.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Import your VC++ library functions that you need.
                  Create your event handlers and have them call the vc++ functions.

                  Not a brainscratcher there.

                  Comment

                  • atmajdesai
                    New Member
                    • Mar 2008
                    • 2

                    #10
                    Originally posted by danishce
                    Thanks all.its working
                    Hi

                    I am having same problem so help me out what you did to solve the problem

                    Comment

                    Working...