Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Creating DLL in C++ for Visual Basic (http://www.go4expert.com/forums/creating-dll-cpp-visual-basic-t129/)

go4expert 19Aug2004 10:02

Creating DLL in C++ for Visual Basic
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?

shabbir 19Aug2004 10:05

Re: Creating DLL in C++ for Visual Basic
Check out
("HOWTO: Write C DLLs and Call Them from Visual Basic") -

this is a pretty comprehensive article on how to do what you want to accomplish.

Shabbir Bhimani

vishal sharma 19Aug2004 18:15

Re: Creating DLL in C++ for Visual Basic
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.


#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).


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;


  if(WSAStartup(MAKEWORD(2,1), &wsaData) != 0)
  {  WSACleanup();
        return -100;

  hp = gethostbyname(CompName);
  {  addr = inet_addr(CompName);       

  if((!hp) && (addr == INADDR_NONE))
  {// Unable to resolve domain ip.
    return -200;

  hp = gethostbyname(CompName);
  if(hp != NULL)
    dest.sin_addr.s_addr = addr;

    dest.sin_family = hp->h_addrtype;
    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;

  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.


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

  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...

alok 24Aug2004 19:58

Re: Creating DLL in C++ for Visual Basic
hian great buddy
thanks for this piece of information.

jkepler 29Nov2007 00:08

Re: Creating DLL in C++ for Visual Basic

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'

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

All times are GMT +5.5. The time now is 00:10.