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?
Check out http://support.microsoft.com/default.aspx?scid=kb;en-us;106553 ("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. Thanks Shabbir Bhimani
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...
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