Click to See Complete Forum and Search --> : CSocket and NT-Service


Karsten Döring
April 8th, 1999, 03:18 AM
Hi,

I programmed a little MFC-tool which watches for incoming connections on a specific port.

After I ported it to an NT-MFC-Service this tool won't work any longer.

If I test my program with socktest I can login, but nothing more happens, it seems that my program don't execute the ACCEPT task ( I use onAccept ).

Who can help me out here, or can probably send me a short code example on how to use this with an NT-Service.

Thanx in advance

April 8th, 1999, 04:03 AM
What account is your service running under? By default services run as LocalSystem, which does not have network access. Try changing it to run under an account which does have network access (use Control Panel, Services, Startup).

HTH

Karsten Döring
April 8th, 1999, 04:09 AM
My program uses the ADMINISTRATOR account. So this don't seem to be the error.

Do u have another idea?

April 8th, 1999, 04:31 PM
Your program must have a window and GetMessage(); DispatchMessage() cycle in main thread.

Karsten Döring
April 9th, 1999, 02:45 AM
Hmm,

I guess this could be really the solution, but I want to have a service without a mainwindow. How can I get rid of it?

Do u have a solution on how to have this meesage cycle without a window?

April 23rd, 1999, 08:32 AM
You cann't use CAsyncSocket class without main window. But you can use generic WinSock functions (connect, send etc).

Kubik
April 23rd, 1999, 01:07 PM
You can create hide window for service. For example:

VOID ServiceStart () {

WNDCLASS wc;
MSG msg;

wc.style = 0;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = NULL;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "AppName";

if (!RegisterClass(&wc)) {
return;
}

hWnd = CreateWindow( "AppName", NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
if (!hWnd) {
return;
}

while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {

switch (message) {

case WM_CREATE:
/*


*/
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return 0;
}

Karsten Döring
April 25th, 1999, 06:14 AM
Thank u very much, following lines worked:

while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

:-)

May 6th, 1999, 04:44 AM
Hi Karsten,

Can you brief me on How have you managed an existing application using sockets to be converted into an NT-Service, as I have made a socket appl. Now I want it to be converted into an NT-Service, How do i proceed ? Should I use ATL default service code for this and then incorporate my application or How should I proceed , enlighten me.

Thanks
SAnjeev

Karsten Döring
May 6th, 1999, 04:50 AM
I used that MFCNTSERVICE Tool wich u can find here on codeguru. It was very simple, to implement my code into that ntservice code. all nt-service function have been there already.

Frank Melendez
May 6th, 1999, 05:26 AM
Thanks guys,

This solved one of our problems too.

Frank.

youngever
May 6th, 1999, 11:38 AM
Good idea, the MFC class CSocket use the windows message to implement the accept action,
so if your main thread does not any message loop, the OnAccept() would never be called,
the best method is create a listen thread which accept client, once any client connects, you
should create another thread for receive and send data with the client.
I have finish a NT service application two month ago, it listen a port and accept valid client and
communicate with it. but there are no CSocket object in the project, I use standart Socket API.