Using CAsyncSocket / CSocket in a console application
'Just Ask' Dan Maher -- dmaher@apdi.net
Wednesday, April 24, 1996
Environment: VC++4.0/OS independent
How can I use CAsyncSocket in a console application? There are other MFC
classes that one might think would be able to be used in a console
application, yet they can't be. I've tried many times to try to get this
to work, but to no avail. The closest I've come to getting it to work was
when I put a bogus CWinApp object in a module and called AfxSocketInit().
Then, everything linked and ran, except that an assertion failed in the
Create() member function. It (the CAsyncSocket) seems to want to be attached to a MFC
thread. My latest attempt was to create a Dialog based MFC application
through AppWizard and remove the dialog. I've changed the subsystem to
console, but, of course, it's looking for a main() at link time. If I
provide one, what should it call? Or can I change the entrypoint and if
so, where?
What I'm trying to do is reuse a CAsyncSocket - based class that is used
in a windowed application. I would like to have both the console app and
the windowed app to use the same class. Is there a better way to do
this?
TIA
dM
Mike Blaszczak -- mikeblas@interserv.com
Sunday, April 28, 1996
On Wed, 24 Apr 1996, "'Just Ask' Dan Maher" wrote:
>Environment: VC++4.0/OS independent
Thanks.
>How can I use CAsyncSocket in a console application?
CAsyncSocket is funny because it has its own message pump. I haven't tried
using an CAsyncSocket in a console application yet, but I think you could do
it by:
1) Declaring a CWinApp-derived object in your application
2) in your main(), call the MFC initialization function to make sure the
CWinApp is initialized and that MFC is ready to go.
3) in your main(), call AfxSocketInit()
4) You might have to create a message pump because CAsyncSocket wants to
create hidden windows and use them to receive messages. CAsyncSocket,
though, has its own message pump. It looks like you'll have to look through
the source code a bit to see when the CAsyncSocket-supplied pump is necessary
and when your pump would be required.
>There are other MFC
>classes that one might think would be able to be used in a console
>application, yet they can't be.
That could be the vaguest thing I've ever heard any (non-political) person
say. Even so, I'd say it is incorrect: you'd probably be surprised at what
you can use in a console application.
>I've tried many times to try to get this
>to work, but to no avail. The closest I've come to getting it to work was
>when I put a bogus CWinApp object in a module and called AfxSocketInit().
>Then, everything linked and ran, except that an assertion failed in the
>Create() member function. It (the CAsyncSocket) seems to want to be
>attached to a MFC
>thread.
What ASSERT failed, specifically? There are no ASSERT statements in
CAsyncSocket::Create(), so you must be getting an ASSERT from some other
place. Did you mean to say that some function which CAsyncSocket::Create()
calls gives you an ASSERT?
How could you expect an accurate answer without including the exact line and
file number of the ASSERT, by the way? People might guess, and then guess
wrong, and then give you a bogus answer. Do you think settlers in the 1840's
ran around saying "Hey, California is just full of that one element!" Maybe
physicists ran out there looking for uranium and got angry because there was
lots of gold all over the damn place. What then?
>My latest attempt was to create a Dialog based MFC application
>through AppWizard and remove the dialog. I've changed the subsystem to
>console, but, of course, it's looking for a main() at link time. If I
>provide one, what should it call? Or can I change the entrypoint and if
>so, where?
That depends. Do you want a console application that interacts with the user
through a console, or do you want a Windows application that runs hidden and
doesn't talk to the user at all? You can also write an application that
interacts with the user via a console even if you have a message pump and
/SUBSYSTEM:WINDOWS, you know. Is that what you want to do?
>TIA
HDYKMAWS?
.B ekiM
--
TCHAR szDisc[] = _T("These words are my own; I do not speak for Microsoft.");
| Вернуться в корень Архива
|