Re: CSocket and NT-Service
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
Re: CSocket and NT-Service
My program uses the ADMINISTRATOR account. So this don't seem to be the error.
Do u have another idea?
Re: CSocket and NT-Service
Your program must have a window and GetMessage(); DispatchMessage() cycle in main thread.
Re: CSocket and NT-Service
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?
Re: CSocket and NT-Service
You cann't use CAsyncSocket class without main window. But you can use generic WinSock functions (connect, send etc).
Re: CSocket and NT-Service
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;
}
Re: CSocket and NT-Service
Thank u very much, following lines worked:
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
:-)
Re: CSocket and NT-Service
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
Re: CSocket and NT-Service
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.
Re: CSocket and NT-Service
Thanks guys,
This solved one of our problems too.
Frank.
Re: CSocket and NT-Service
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.