Creating DLL in C++ for Visual Basic

Discussion in 'C++' started by go4expert, Aug 19, 2004.

  1. go4expert

    go4expert Moderator

    Joined:
    Aug 3, 2004
    Messages:
    306
    Likes Received:
    9
    Trophy Points:
    0
    Can someone send me a link to a good resource on how to make a C++ DLL that I can access from a Visual Basic app?
     
    Last edited: Aug 19, 2004
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Last edited: Aug 19, 2004
  3. vishal sharma

    vishal sharma New Member

    Joined:
    Jul 23, 2004
    Messages:
    106
    Likes Received:
    6
    Trophy Points:
    0
    u'll have to create a new dynamic link library project. After u create this project u'll be prompted on what kinda DLL, just click empty. After that is complete you will have to add the library ws2_32.lib to the project under project-> settings -> link -> object/modules library or the code below will not compile!. u'll now have to add three files to your project, the first being an implementation (.CPP) file, the second an interface/header (.H) file and the third a DEF (allows VB to be able to read the C++ dll function name) file.

    Now lets add to the include files the function definitions for the header file. I called the header file for this demonstration c_dll_4_vb.h.


    Code:
    #include <windows.h>
    #include <winsock.h>
    
    // Function is used to resolve a domain name to an IP address.
    // The return values are: -100 = Incorrect version of Winsock
    //                        -200 = Cant resolve domain.
    long __stdcall Resolve_Name_To_Ip
        (char *pcDomainToResolve, char szReturnIP[500], int &iSize);
    
    
    The function defined above will take three parameters:

    pcDomainToResolve - The name of the domain you are inquiring about.
    szReturnIP -The variable the IP address will be returned to you in.
    iSize - The length of IP address being returned without the padding.
    Next you will need to add the implementation of the header file to the .CPP file. Remember that when you add the code to the .CPP file make sure you include the header file name (c_dll_4_vb.h).

    Code:
    c_dll_4_vb.cpp #include "c_dll_4_vb.h"
    
    long __stdcall Resolve_Name_To_Ip
      (char *pcDomainToResolve, char szReturnIP[500], int &iSize)
    {
      WSADATA wsaData;					
      LPTSTR CompName = new char[255];
      LPDWORD CompSize = new unsigned long(255);
    
      struct sockaddr_in dest;
      struct hostent *hp;
      char *dest_ip;
    
      unsigned int addr = 0;
    
      strcpy(CompName,pcDomainToResolve);
    
      if(WSAStartup(MAKEWORD(2,1), &wsaData) != 0)
      {  WSACleanup();
    	 return -100;
      }
    
      hp = gethostbyname(CompName);
      if(!hp)
      {  addr = inet_addr(CompName);	
      }
    
      if((!hp) && (addr == INADDR_NONE))
      {// Unable to resolve domain ip.
        WSACleanup();
        return -200;
      }
    
      hp = gethostbyname(CompName);
      if(hp != NULL)
        memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length);
      else
        dest.sin_addr.s_addr = addr;
    
      if(hp)
        dest.sin_family = hp->h_addrtype;
      else
        dest.sin_family = AF_INET;
    
      dest_ip = inet_ntoa(dest.sin_addr);
    	
      iSize = strlen(dest_ip);
    	
      // Allow the string to return to the proper size.
      strncpy(szReturnIP, dest_ip, strlen(dest_ip)); 
    	
      dest_ip = NULL;
      hp = NULL;
      addr = 0;
      dest.sin_family = NULL;
      dest.sin_addr.s_addr = NULL;	
      dest.sin_addr.S_un.S_addr = NULL;
      dest.sin_port = NULL;
    
      delete [] CompName;
      delete [] CompSize;
    
      WSACleanup();
      return 0;
    }
    
    
    u'll also have to include .DEF file. This file will let VB read the function names from the C++ dll. This is the most important file when working with C++ and VB because without it u'll run into all kinda errors.

    Code:
    c_dll_4_vb.def LIBRARY c_dll_4_vb
    DESCRIPTION     'A C++ dll that can be called from VB'
    
    EXPORTS
      Resolve_Name_To_Ip @1
    
    All that is left for u 2 do is compile the dll and it will be ready for use with VB.

    happy programmin...
     
  4. alok

    alok New Member

    Joined:
    Jul 24, 2004
    Messages:
    127
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg
    Home Page:
    http://www.thatsalok.com
    hian great buddy
    thanks for this piece of information.
     
  5. jkepler

    jkepler New Member

    Joined:
    Nov 28, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    As I understood the .def file must have the code (without the name of the file at the beginning?):

    LIBRARY c_dll_4_vb
    DESCRIPTION 'A C++ dll that can be called from VB'

    EXPORTS
    Resolve_Name_To_Ip @1

    Is this correct?
    But if we have more functions, must we add @2, @3, @4...etc...?

    Kind regards,

    J. Kepler
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice