CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    CSocket and NT-Service

    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


  2. #2
    Guest

    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


  3. #3
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    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?


  4. #4
    Guest

    Re: CSocket and NT-Service

    Your program must have a window and GetMessage(); DispatchMessage() cycle in main thread.


  5. #5
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    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?


  6. #6
    Guest

    Re: CSocket and NT-Service

    You cann't use CAsyncSocket class without main window. But you can use generic WinSock functions (connect, send etc).


  7. #7
    Join Date
    Apr 1999
    Posts
    4

    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;
    }



  8. #8
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    Re: CSocket and NT-Service

    Thank u very much, following lines worked:

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

    :-)


  9. #9
    Guest

    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


  10. #10
    Join Date
    Apr 1999
    Location
    Germany
    Posts
    53

    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.


  11. #11
    Join Date
    May 1999
    Posts
    1

    Re: CSocket and NT-Service

    Thanks guys,

    This solved one of our problems too.

    Frank.


  12. #12
    Join Date
    Apr 1999
    Location
    Shanghai China
    Posts
    8

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured