Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21089

C++ To VB6 Conversion

$
0
0
Hey there. It's been awhile since I posted in the VB6 area. :bigyello:

I've recently been doing some heavy duty C++ programming lately, mostly in the DirectX area, and recently got into Winsock. So after getting an awesome server to client chit chat program through the console window, I was wanting to attempt this in VB6 just for kicks. However, I ran into an api that seems to have little to no results in Google. And that is getaddrinfo(). All the other examples in VB6 I ran into have been using gethostbyname() instead which have been deprecated, and has been widely recommended that you use getaddrinfo() instead. Anyways I don't wanna go to far into talking about winsock programming or this thread will end up moved to the networking thread. My concern is the conversion. How the C++ api getaddrinfo() is, is setup like this:

Code:

typedef struct addrinfo {
  int            ai_flags;
  int            ai_family;
  int            ai_socktype;
  int            ai_protocol;
  size_t          ai_addrlen;
  char            *ai_canonname;
  struct sockaddr  *ai_addr;
  struct addrinfo  *ai_next;
} ADDRINFOA, *PADDRINFOA;

int WSAAPI getaddrinfo(
  _In_opt_  PCSTR pNodeName,
  _In_opt_  PCSTR pServiceName,
  _In_opt_  const ADDRINFOA *pHints,
  _Out_    PADDRINFOA *ppResult
);

Now _In_opt_ would make it ByVal and _Out_ would be ByRef after researching that, however we got a lot of pointers going on here. PCSTR would be a pointer to a constant string, pHints is a pointer to the structure ADDRINFO along with ppResult. However online, the only time I ever saw getaddrinfo() declared in VB was from someone posting a problem in a forum, and he had it setup differently, and in my opinion totally wrong which is why it wasnt working for him:

http://www.solveerrors.com/forums/us...vb6-239905.asp

First of all, his ADDRINFO datatype is missing 2 declarations which the API is dependent on, along with another structure. But when I declare it, I obviously can't declare a variable as the structure its within in vb6:

vb Code:
  1. Public Type sockaddr
  2.     sin_family       As Integer
  3.     sin_port         As Integer
  4.     sin_addr         As Long
  5.     sin_zero(1 To 8) As Byte
  6. End Type
  7.  
  8. Public Type addrinfo
  9. 'typedef struct addrinfo {
  10. '  int             ai_flags;
  11. '  int             ai_family;
  12. '  int             ai_socktype;
  13. '  int             ai_protocol;
  14. '  size_t          ai_addrlen;
  15. '  char            *ai_canonname;
  16. '  struct sockaddr  *ai_addr;
  17. '  struct addrinfo  *ai_next;
  18. '} ADDRINFOA, *PADDRINFOA;
  19.  
  20.     ai_flags As Long
  21.     ai_family As Long
  22.     ai_socktype As Long
  23.     ai_protocol As Long
  24.     ai_addrlen As Long
  25.     AI_CANONNAME As String
  26.     ai_addr As sockaddr
  27.     'ai_next As addrinfo '<----- not allowed in vb6
  28. End Type

Ok thats problem one that I'll need help in the conversion process to get the API to function properly.

Secondly is the API itself. He has it declared as this:

vb Code:
  1. Private Declare function GetAddrInfo Lib "ws2_32.dll" Alias "getaddrinfo" ( _
  2. ByVal NodeName As String, _
  3. ByVal ServName As String, _
  4. Hints As AddrInfo, _
  5. res As AddrInfo) As Long

I cant but feel this is also wrong, cause the original C++ version consists of all pointers, whether its to a string or structure. And you also cant ByVal Hints As AddrInfo cause you are using a structure and VB6 wont allow it. Youll end up with an error saying "User Defined Type cannot be passed through ByVal." So I have this setup for the API which is kind of the same but doubt its correct, and I cant find anything else on it through Google:

vb Code:
  1. 'The getaddrinfo function provides protocol-independent translation from an ANSI host name to an address.
  2. Public Declare Function getaddrinfo Lib "ws2_32.dll" (ByVal pNodeName As String, ByVal pServiceName As String, pHints As addrinfo, ppResult As addrinfo) As Long

When the API is used in C++ it looks something like this:

C++ Code:
  1. #define DEFAULT_PORT "3504"
  2.  
  3. int iResult = 0;
  4.  
  5. int Setup_Server()
  6. {
  7.     iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  8.     if ( iResult != 0 ) {
  9.         cout << "getaddrinfo failed with error: " << iResult << endl;
  10.         WSACleanup();
  11.         return 1;
  12.     }
  13. }

Which works great. And NULL if you put your mouse over it in Visual Studio shows #define NULL 0;. Now for the VB6 version, he did it differently which is why it failed for him, not to mention his string has a number instead of a string:

vb Code:
  1. iRet = GetAddrInfo(sHost, 0, Hints, res)

Now in my program I wrote in VB6, I did it differently:

vb6 Code:
  1. iResult = getaddrinfo("", DEFAULT_PORT, Hints, result)

And there was no 11003 and it passed, buuuuuuuut.....there was no result output because it remained 0 for everything. I saw this when I printed em out:

Code:

    Print result.ai_family
    Print result.ai_socktype
    Print result.ai_protocol

    'Output
    '0
    '0
    '0

Which leads to the conclusion that something is setup wrong either in the API itself, or the user defined type due to the missing declaration, or both! How would I go about setting it up correctly so I can get getaddrinfo() working properly in VB6 since there is a lack of information regarding the API?

Hopefully I didnt make it too confusing for yall. :bigyello:

Viewing all articles
Browse latest Browse all 21089

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>