?
Solved

Calling Winsock from another control in same project.

Posted on 2003-02-23
9
Medium Priority
?
232 Views
Last Modified: 2013-11-13
I have made an OCX wich have to call the Winsock (connect, DataArrival, SendData etc.) that will be placed in same project. As is now I get an error, trying to call the winsock from my control. I want to know either how I can make a call from my control to the winsock. Or if the winsock can be "imported" into my project, and that way make it possible to call the winsock. Thank you!
0
Comment
Question by:Jens979
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 1

Expert Comment

by:vware
ID: 8004573
why dont you just put the winsock control in your user control, and then make all functions accessible from the control?


(code in the user control)

Public Event Connect()

Private Sub Winsock1_Connect()
'[code you want to have in the user control]
'Raise event
RaiseEvent Connect
End Sub

this way you will have your user control code and also have it "act as" winsock control
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8004595
You may kick yourself for not thinking about this.  Add a public method to your control that allows for setting the winsock object to use for all socket operations.  Something like this:

Dim Socket As Winsock
Public Sub SetWinsock(Sck As Winsock)
    Set Socket = Sck
    'Set default remote host
    Socket.RemoteHost = "127.0.0.1"
    'Set default port
    Socket.RemotePort = 123
End Sub
Public Sub Connect()
    Socket.Connect
End Sub

something to that effect.
0
 

Expert Comment

by:EViLWolf
ID: 8004907
SilentRage, what you wrote doesn't work and is totally stupid (but well thought)...

You define Socket As Winsock, so it = Nothing...
Then, in the sub, you define Sck As Winsock, so it = Nothing...
Then you say that Socket = Sck... Socket and Sck are the same thing.

The way I usually do it is putting the Winsock control in the form. Then for connect, place...

Private Sub Winsock1_Connect()
    Module1.Connect Host, Port
End Sub

Then, in Module1, you put...

Public Sub Connect(Host As Variant, Port As Variant)
    Form1.Winsock1.Connect Host, Port
End Sub

That's mainly what I do... I know it's not what you are searchign for but it might help.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:SilentRage
ID: 8005078
Love the tact there.  Really thought out.  Perhaps I shouldn't assume that you would know that you should pass a winsock object to that function something like this:

WinsockOCX.SetWinsock Winsock

Of course, that assumes that in your Project you have a reference to the OCX, and a Winsock control placed on your form and called it Winsock.
0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8005088
afterthought... modification of your code to use my technique:

Private Sub Form_Load()
   Module1.SetWinsock Winsock1
   Module1.Connect Host, Port
End Sub

Then, in Module1, you put...

Dim Socket As Winsock
Public Sub SetWinsock(Sck As Winsock)
   'this creates another reference to the above Winsock1
   Set Socket = Sck
End Sub
Public Sub Connect(Host As Variant, Port As Variant)
   Socket.Connect Host, Port
End Sub
0
 

Author Comment

by:Jens979
ID: 8008322
To vware.

Yes vware! This was what i ment about "importing" the winsock control. = "put the winsock control in your project".
But I am not quite sure about how to call it. Lets say I have following in my usercontrol;

Public Sub InviteBuddy (Buddy As String, Channel As String)
    SendThis "INVITE" & " " & Buddy & " " & Channel
End Sub

Public Sub SendThis (Streng As String)
    Winsock1.SendData (Streng)
End Sub

....so, I have several Subs like "InviteBuddy" that calls the "SendThis" with a string to be send to the server I have connected to.

So I will like to be able to use alll possibilities of the winsock.

Thank you ! - Raised another 50points.

0
 
LVL 1

Expert Comment

by:SilentRage
ID: 8009081
I told you how to call it.  What I told you is for when you do not have the winsock control ON your user control, and instead have it on your form.

Although the best way is to have the winsock control IN your usercontrol OCX.
0
 

Author Comment

by:Jens979
ID: 8009311
To SilentRage !

If I insert the "Public Sub SetWinsock(Sck As Winsock)"
in my usercontrol, or classmodule I get an error when compiling. - "User-defined types and fixed-length strings not allowed as the type of a public member of an object module; private object modules not allowed as the type of a public member of a public object module"

/Thanks
0
 
LVL 1

Accepted Solution

by:
SilentRage earned 600 total points
ID: 8010814
Even though you don't have to put the control on your form, you need to add the winsock component to your project so that it recognizes what the 'Winsock' type is.

Why don't you put the winsock control in your usercontrol OCX?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

771 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