passing strings to external functions

General FreeBASIC programming questions.
MrSwiss
Posts: 2119
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: passing strings to external functions

Postby MrSwiss » Nov 13, 2017 12:23

DamageX wrote:I wanted to pass a pointer because I wanted the function to read from a location other than the beginning. Specifically, if I have stuff="http://www.freebasic.net" I wanted to pass a pointer to "www.freebasic.net" and skip the "http://"
There is a massive misunderstanding from your side:
GetHostByName() is, as the name indicates, a GETTER (gets information, from .dll in this case).
Your intended use indicates, that you want to: SET something, for that you need a: SETTER.
(Maybe there is something similar like: SetHostByName() ...)
Munair
Posts: 374
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: passing strings to external functions

Postby Munair » Nov 13, 2017 12:36

MrSwiss wrote:
DamageX wrote:I wanted to pass a pointer because I wanted the function to read from a location other than the beginning. Specifically, if I have stuff="http://www.freebasic.net" I wanted to pass a pointer to "www.freebasic.net" and skip the "http://"
There is a massive misunderstanding from your side:
GetHostByName() is, as the name indicates, a GETTER (gets information, from .dll in this case).
Your intended use indicates, that you want to: SET something, for that you need a: SETTER.
(Maybe there is something similar like: SetHostByName() ...)

A library (dll) routine may require to receive the data through a parameter rather than by assignment. It's a long time ago I did programming for Windows (VB6), but I don't recall receiving data from library routines by assignment, but always through parameter. We're not talking about a class member here.
MrSwiss
Posts: 2119
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: passing strings to external functions

Postby MrSwiss » Nov 13, 2017 12:47

<ot>A library (dll) routine may require to receive the data through a parameter rather than by assignment.</ot>
What has this to do, with my post? I on purpose, did not use any parameters, to clearly show the difference:
GET...
SET...
grindstone
Posts: 360
Joined: May 05, 2015 5:35
Location: Germany

Re: passing strings to external functions

Postby grindstone » Nov 13, 2017 13:22

DamageX wrote:Specifically, if I have stuff="http://www.freebasic.net" I wanted to pass a pointer to "www.freebasic.net" and skip the "http://"
That's quite easy:

Code: Select all

Dim As String stuff="http://www.freebasic.net"
Dim As ZString Ptr p

p = StrPtr(stuff) + 7
Print *p
Sleep
It should work if you pass StrPtr(stuff) + 7 as parameter:

Code: Select all

gethostbyname(StrPtr(stuff) + 7)
MrSwiss
Posts: 2119
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: passing strings to external functions

Postby MrSwiss » Nov 13, 2017 13:31

grindstone wrote:It should work if you pass
No, it won't ever work, as long as OP, uses a GETTER,
instead of a SETTER ...
Munair
Posts: 374
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: passing strings to external functions

Postby Munair » Nov 13, 2017 13:33

MrSwiss wrote:<ot>A library (dll) routine may require to receive the data through a parameter rather than by assignment.</ot>
What has this to do, with my post? I on purpose, did not use any parameters, to clearly show the difference:
GET...
SET...

MrSwiss wrote:
grindstone wrote:It should work if you pass
No, it won't ever work, as long as OP, uses a GETTER,
instead of a SETTER ...

On closer look, actually, there is a misunderstanding on your side. First, the OP clearly stated that one of his three solutions worked. Second, he tried to feed "www.freebasic.net" to the function. Third, if you did a search on the GetHostByName function, you would have found something like this:

Code: Select all

#include <winsock.h>
#include <Windows.h>
#include <stdio.h>

#pragma comment (lib, "ws2_32.lib")

int main(void)
{
   WSADATA wsaData = { 0, };
   struct in_addr addr = { 0, };
   struct hostent * res;
   int i = 0;
   
   WSAStartup(MAKEWORD(2, 2), &wsaData);

   res = gethostbyname("www.naver.com");
   while (res->h_addr_list[i] != 0)
   {
      addr.s_addr = *(u_long *)res->h_addr_list[i++];
      printf("IP Address: %s\n", inet_ntoa(addr));
   }

   WSACleanup();
}
Note "res = gethostbyname("www.naver.com");" which is exactly what the OP was trying to do. So Pointing to the difference between a getter and setter is irrelevant.
grindstone
Posts: 360
Joined: May 05, 2015 5:35
Location: Germany

Re: passing strings to external functions

Postby grindstone » Nov 13, 2017 14:19

Anyhow the pointer can be passed this way.
dodicat
Posts: 4495
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: passing strings to external functions

Postby dodicat » Nov 13, 2017 19:32

Here is the C code translated somewhat:

Code: Select all

'#include <winsock.h>
'#include <Windows.h>
'#include <stdio.h>

'#pragma comment (lib, "ws2_32.lib")


'#include "windows.bi"
#include "win\winsock2.bi"
#include "crt.bi"

Dim  s as string
s="www.freebasic.net"

sub main(stuff as string)
   dim as WSADATA  _wsadate
   dim as  in_addr addr
   dim as hostent ptr res
   dim as integer i = 0
   WSAStartup(MAKEWORD(2, 2), @_wsadate)

   res = gethostbyname(stuff)'("www.naver.com")
   while (res->h_addr_list[i] <> 0)
     addr.s_addr = *(cast (ulong ptr,res->h_addr_list[i]))
   
      printf(!"IP Address: %s\n", inet_ntoa(addr))
      i+=1
   wend

   WSACleanup()
end sub

main(s)
sleep
dodicat
Posts: 4495
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: passing strings to external functions

Postby dodicat » Nov 13, 2017 20:19

A little tidy up:

Code: Select all


#include "win\winsock2.bi"

sub GetSiteAddress(stuff as string)
   dim as WSADATA  _wsadate
   dim as  in_addr addr
   dim as hostent ptr res
   dim as integer i = 0
   WSAStartup(MAKEWORD(2,2),@_wsadate)
   res = gethostbyname(stuff)
   if res then
   print stuff
   while (res->h_addr_list[i] <> 0)
     addr.s_addr = *(cast (ulong ptr,res->h_addr_list[i]))
   print "IP Address: ";*inet_ntoa(addr)
      i+=1
   wend
   WSACleanup()
else
   print "website error?"
   end if
end sub

GetSiteAddress "www.freebasic.net"
print
GetSiteAddress "www.naver.com"
print
GetSiteAddress "www.google.com"
sleep
 

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests