Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Dynamic Winsock

I have downloaded the controls from Ostrosoft and do not want them compiled into ocx format, so I have added them to my code. The problem is when I do this, the whois control will fail with "Improper use of NEW keyword" and highlights this reference of code
"Set wsTCP = New Winsock". I will be more than happy to show my complete code to this. I am simply trying to combine all there controls into one useful program for my personal use.
The address for the controls is http://www.ostrosoft.com/vb/controls/whois_sample.html

Thanks in advance.
0
Laqua
Asked:
Laqua
  • 2
  • 2
1 Solution
 
wsh2Commented:
To Dynamically load a control.. (although.. why aren't you just adding the Winsock control to your form?).. try this:

1. Start a New Standard.Exe project.
2. Copy/Paste the following into the Form1 code window.
3. Press F5 to run.

<----- Code Begin ----->

Option Explicit

Private WithEvents wskGnutella As VBControlExtender

Private Sub ConnectToServer(sHostAddr As String, lPort As Long)
   wskGnutella.object.RemoteHost = sHostAddr
   wskGnutella.object.RemotePort = lPort
   wskGnutella.object.Connect
End Sub

Private Sub Form_Load()
   
   Dim strControlName As String
   Dim strControlType As String
   Dim strLicense As String
   
   strControlType = "MSWinSock.Winsock.1"
   strControlName = "wskGnutella"
   strLicense = Licenses.Add(strControlType)
   Set wskGnutella = Controls.Add(strControlType, strControlName)
   
   Call ConnectToServer("www.yahoo.com", 21)

End Sub

Private Sub wskGnutella_objectevent(Info As EventInfo)
   
   Dim p As EventParameter
   Debug.Print Info.Name
   For Each p In Info.EventParameters
      Debug.Print p.Name, p.Value
   Next

   MsgBox "It finally worked! Thank god you figured it out Chris!"

End Sub

<----- Code End ----->
0
 
zivfCommented:
You cannot do what you tried to do because the winsock control is in an OCX, and not in a DLL, this means that it must be put on a form, and not created using the NEW keyword.
I wanted to do the same a while ago in order not to need to put a form just in order to create a network connection, and you cannot do this with the winsock control.
0
 
wsh2Commented:
Pardon me zivf.. where do you see the word NEW in the comment above yours? Perhaps you should read or perhaps even try other's comments, before locking down a question with an answer.. you might just learn something about Visual Basic.. <smile>.
0
 
LaquaAuthor Commented:
It can be done, as wsh2 has shown. My problem is guys, it is going into a custom control and MUST be created dynamically on the form the custom control is on. eg: myctrl is added to form1,when form one gets username and password and sends to myctrl. myctrl creates a winsock connection and whalah. I know it is possible, but when I did it before it was by an example that someone gave me and I have lost the link to that page.
0
 
LaquaAuthor Commented:
Although this does not work for what I originally tried to do, it did help me with a different problem. Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now