Click here to Skip to main content
15,897,273 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: ADO datetime parameter - datestring or numbers to VARIANT Pin
Mircea Puiu14-Oct-05 21:32
Mircea Puiu14-Oct-05 21:32 
GeneralRe: ADO datetime parameter - datestring or numbers to VARIANT Pin
compoundeye14-Oct-05 21:46
compoundeye14-Oct-05 21:46 
QuestionWhy unable to receive all message from connected clients using IOCP Pin
14-Oct-05 20:08
suss14-Oct-05 20:08 
QuestionPlaying an mp3 file from memory Pin
V.G14-Oct-05 20:05
V.G14-Oct-05 20:05 
QuestionHow to find data encoding of received buffer with WSARecv() Pin
Member 168985514-Oct-05 19:49
Member 168985514-Oct-05 19:49 
AnswerRe: How to find data encoding of received buffer with WSARecv() Pin
kakan16-Oct-05 19:42
professionalkakan16-Oct-05 19:42 
GeneralRe: How to find data encoding of received buffer with WSARecv() Pin
Member 168985516-Oct-05 20:47
Member 168985516-Oct-05 20:47 
QuestionVC++6 Dll For use with VB6 Pin
tinman033014-Oct-05 18:45
tinman033014-Oct-05 18:45 
Hello all!

I'm a VB and Assembly Programmer(Strange mix.. I know..). Well I need some help making a Dll project compile for use with VB. Below this message I will include instructions that I found for creating such a project. My problem is that I have a project (Speex.. i'm sure everyone is familiar) and I need to convert it not create a new one. Thank you in advance for the response! By the way on a scale of (1 - 10) my C++ skills are about 1.

Here is the project: http://downloads.us.xiph.org/releases/speex/speex-1.0.5.tar.gz

From what I understand all I need is:
1) A .DEF file
2) "Empty" Dll project (duno if you can change that after)
3) Include "ws2_32.lib"

*************************************************************************************
The info I found
*************************************************************************************
There have been projects I have been on in the past where there was certain functionality I needed but could not get from VB, or I had to do a complete workaround to get the functionality I could get from C++ very easily. In this tutorial I will show you how to create a C++ dll that you can call from VB. I figure instead of doing the norm, "HELLO WORLD", or "ADD NUMBER", routine I will show you how to resolve a domain name to an IP address and return that IP address back to VB, this is a little advanced for the beginner but if you have some C++ experience it wont be that bad.
For this example I will be using VC++ 6 so if you have that IDE then you will be right at home and in the case you do not use that IDE then just try to follow along and enjoy the ride!
Ok, you will first have to create a new dynamic link library project. After you create this project you will be prompted on what kind of 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! Believe me when I tell you this as I had the fun of learning this the hard way and it was a nightmare.
You will 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.
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;

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;
}
Shew! Now that wasn't so bad. Ok, we have one more file to add data to but this one I swear is a breeze, this is the .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 you will run into all kinds of errors.
So rather than just talking about it let see what this file looks like.
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
You have just completed the C++ dll. All that is left for you to do is compile the dll and it will be ready for use with VB.
So what are we waiting for lets get onto the VB stuff!
This is the easiest part of the demonstration so I wont go into detail as it is pretty much self explanatory.


Private Declare Function Resolve_Name_To_Ip _
Lib "[Add the path to the dll you just compiled]" _
(ByVal pcDomainToResolve As String, _
ByVal szReturnIP As String, _
ByRef iSize As Integer) As Long

Private Sub Form_Load()

Dim sIP As String
Dim iSize As Integer
Dim lRetVal As Long
Dim sDomain As String

sDomain = "www.yahoo.com"
sIP = String(20, "*")
lRetVal = Resolve_Name_To_Ip(sDomain, sIP, iSize)
MsgBox (Mid(sIP, 1, iSize))

End Sub
Well that just about wraps it up, but again, and I cannot stress it enough that if you run into trouble compiling the DLL make sure you have included the ws2_32.lib library in the project as explained above or you will get a ton of errors.
If you ever receive errors like, "error LNK2001: unresolved external symbol", you are most likely missing a required library. Just go ahead and add the missing library to the project and you will be good to go.
I hope this has helped you out!

***********************************************************************************
End if info
***********************************************************************************
AnswerRe: VC++6 Dll For use with VB6 Pin
John R. Shaw14-Oct-05 22:01
John R. Shaw14-Oct-05 22:01 
GeneralRe: VC++6 Dll For use with VB6 Pin
tinman033015-Oct-05 12:48
tinman033015-Oct-05 12:48 
Questionwhy can't I see my class in the ClassView pane? Pin
ewighell14-Oct-05 18:05
ewighell14-Oct-05 18:05 
AnswerRe: why can't I see my class in the ClassView pane? Pin
Fired Fish14-Oct-05 20:12
Fired Fish14-Oct-05 20:12 
AnswerRe: why can't I see my class in the ClassView pane? Pin
khan++14-Oct-05 21:39
khan++14-Oct-05 21:39 
GeneralRe: why can't I see my class in the ClassView pane? Pin
ewighell15-Oct-05 6:14
ewighell15-Oct-05 6:14 
GeneralRe: why can't I see my class in the ClassView pane? Pin
fuzz_ball18-Oct-05 15:46
fuzz_ball18-Oct-05 15:46 
QuestionRichEdit Control not Drawing Properly Pin
Steve Thresher14-Oct-05 12:22
Steve Thresher14-Oct-05 12:22 
Questiontoolbar Pin
Archer28214-Oct-05 12:11
Archer28214-Oct-05 12:11 
AnswerRe: toolbar Pin
khan++14-Oct-05 21:48
khan++14-Oct-05 21:48 
QuestionListing Thread Module Names and Base Addresses Pin
Abhishek Karnik14-Oct-05 9:54
Abhishek Karnik14-Oct-05 9:54 
AnswerRe: Listing Thread Module Names and Base Addresses Pin
Arman S.14-Oct-05 10:13
Arman S.14-Oct-05 10:13 
GeneralRe: Listing Thread Module Names and Base Addresses Pin
Anonymous14-Oct-05 10:32
Anonymous14-Oct-05 10:32 
GeneralRe: Listing Thread Module Names and Base Addresses Pin
Abhishek Karnik14-Oct-05 10:48
Abhishek Karnik14-Oct-05 10:48 
GeneralRe: Listing Thread Module Names and Base Addresses Pin
Robert M Greene14-Oct-05 11:07
Robert M Greene14-Oct-05 11:07 
GeneralRe: Listing Thread Module Names and Base Addresses Pin
Abhishek Karnik14-Oct-05 11:09
Abhishek Karnik14-Oct-05 11:09 
QuestionPassing pointers Pin
Anonymous14-Oct-05 8:22
Anonymous14-Oct-05 8:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.