Solved

How to "new" Winsock in a class module???

Posted on 2002-07-17
14
812 Views
Last Modified: 2013-11-13
Private Sub Class_Initialize()
   Set theSock = New Winsock   <--- Error
End Sub

The error: "Compile Error: Invaild use of New keyword"

I can't "new" Winsock in a class module. As you know, when you type "New" in VB, VB will automatically popup a listbox for all objects that can be "new". But I can't see Winsock appeared on the listbox.

Also, I have placed another Winsock control on the Main Form, don't know that will this cause the problem.
How can I fix this problem? Please Help!!!


raymng
0
Comment
Question by:raymng
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7159588
You cannot initiate a Winsock instance like this, you have to load a new winsock control to an existing form.

So, you have to create a winsock control (say name=winsock)on the form, with index=0. If you need another winsock control, you need to do like this:
dim theSock as control
load winsock(1)
set theSock = winsock(1)
...

to "unload" it, use this code (in the form)
set theSock = nothing
unload winsock(1)

As you have a class module, that class module will have to refer to the form to do the operations of loading and unloading the winsock controls...

CHeers
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7159614
You have to set a reference to the winsock dll via Project->REFERENCES and not Project->Components
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7159618
Sorry, a reference to the winsock ocx, not dll.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7159624
Here is a thread discussing this idea and some of the implications of doing it this way:
http://www.vbip.com/forum/topic.asp?id=17
0
 
LVL 4

Expert Comment

by:AlonHirsch
ID: 7159631
What exactly are you trying to do ?
If you want to create a control array of winsock controls use Load wscSock(iIndex) where iIndex is the next available number.

The Winsock class within the control is declared Public - Not Creatable. This means that you can reference the class but can not create NEW instances of it without creating new instances of the control that contains it.

HTH,
Alon
0
 

Author Comment

by:raymng
ID: 7159636
angelIII:

''''''''''''''''''''''''''''''
dim theSock as control
load winsock(1)
set theSock = winsock(1)
...

to "unload" it, use this code (in the form)
set theSock = nothing
unload winsock(1)
'''''''''''''''''''''''''''''
Whether should this code be written on the Class Module or the Main Form?

Does it mean I can only place one Winsock on the Form? so if I need more Winsock, then I need to create it by coding?

I also don't understand "As you have a class module, that class module will have to refer to the form to do the operations of loading and unloading the winsock controls", can you give me some sample coding?

Thank you very much!

raymng
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 7159660
The code MUST go to the form where the winsock control is. The problem is that the winsock control requires a form to be loaded...

no, you can place several controls on 1 form. But to load a control dynamically, you need to use the control array (using index, LOAD and UNLOAD)

The class code will have to hold a reference to the form with the winsock control(s). I do typically use a dedicated form for this, in order to avoid any troubles with multiple forms. You could also have a reference of that form in a global variable (public gWinsockForm ... in a standard module). I once have written this kind of thing, but obviously it was at a client site, and the client holds the sources...
In short, I controlled the winsocked controls by the form dedicated to it, with functions like AddSocket(), GetSocket(n) and ReleaseSocket(n), where AddSocket returned the index for the assigned socket control, which needs to be passed to GetSocket(n) and ReleaseSocket(n).
The functions SendData(n, data) and the event RecievedData(n, data) complete the handling...

CHeers

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:raymng
ID: 7159683
AzraSound:

I don't have winsock.ocx on my system, where can I donwload it?


raymng
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7159728
Its called mswinsck.ocx
0
 
LVL 4

Expert Comment

by:AlonHirsch
ID: 7159742
Hi,

The file is called Mswinsck.ocx

Alon
0
 

Author Comment

by:raymng
ID: 7159789
When I tried to add this reference, error come again: "Name conflicts with existing module, project, or object library"


raymng
0
 

Author Comment

by:raymng
ID: 7160238
Thanx everyone here!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 7160272
>>When I tried to add this reference, error come again: "Name conflicts with existing module, project, or object library"

Because you had it selected via Project->Components, not Project->References.  You needed to remove if from the Components first and then set it as a reference.
0
 

Author Comment

by:raymng
ID: 7160321
AzraSound:

Thank you! I have solved this problem.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now