Solved

NON-BLOCKING sockets without the windows message loop?

Posted on 2004-10-21
311 Views
Last Modified: 2008-02-01
OK here is my situation...

I am writing a DLL that will be accessed by a client application. This is for a chat application btw, the DLL will handle everything, and will notify the client when chat text is recieved, but other than that, the client has no involvement with the DLL...

within the DLL, I am creating a socket connection to connect to the server. Here is my problem. Obviously I will need a non-blocking sockets system... I have tried using the Windows API method, where windows posts messages like FD_RECIEVE for specific events, but my problem is this. In order to set up this method, it requires my DLL to create a window, and set up a window procedure just to recieve a couple of FD_ events. Is there any other method in which I can use non-blocking sockets, without having to create a window?

The problem with creating a window comes when I am in debugging mode. Sometimes RegisterClass will fail for no reason, other times it will run... so if at all possible, I would like to find another method...

so a solution to this problem would be either
1) some kind of method to continue to recieve the windows FD_ events to my DLL's WindowProcedure, without having to create a window
2) if there is no way for method 1, some other method of using non-blocking sockets, WITHOUT polling

thanks for the help
0
Question by:computing
    8 Comments
     
    LVL 86

    Expert Comment

    by:jkr
    >>so a solution to this problem would be either
    >>1) some kind of method to continue to recieve the windows FD_ events to my DLL's WindowProcedure, without
    >>having to create a window

    That's easy, just use an _invisible_ window (using 'SW_HIDE') - this is a common practise.

    >>2) if there is no way for method 1, some other method of using non-blocking sockets, WITHOUT polling

    Use I/O completion ports. See the sample at http://win32.mvps.org/network/sockhim.zip
    0
     

    Author Comment

    by:computing
    That's easy, just use an _invisible_ window (using 'SW_HIDE') - this is a common practise.

    >>2) if there is no way for method 1, some other method of using non-blocking sockets, WITHOUT polling

    Use I/O completion ports. See the sample at http://win32.mvps.org/network/sockhim.zip

    for your first comment, that is what i was doing all along. I dont know why, but when im debugging my client (its written in vb), the RegisterWindow function fails most of the time, but randomly sometimes it will work. Perhaps my code is to blame? I had it set up so that the client has to call Initialize() in the DLL passing its HINSTANCE so that registerclass could create a window... take a look and tell me what you think the error could be... i never had a problem outside of debug mode, but that is a problem because it makes it impossible for me to debug my client...

    DLL CODE:


    LRESULT CALLBACK DllWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
          if(uMsg == WM_ASYNC)
          {
                       conn->socket_event(WSAGETSELECTEVENT(lParam));
             return 0;
          }
          else if (uMsg == WM_CLOSE)
          {
                DLL_Unload();
                return 0;
          }
          else {return DefWindowProc(hwnd,uMsg,wParam,lParam);}
    }

    int DLL_Initialize(HINSTANCE hInt)
    {
          if (dll_hwnd != NULL)
                return -1;

          WSADATA info;
          WNDCLASSEX      wndclassex;

          wndclassex.cbSize                        = sizeof(wndclassex);
          wndclassex.style                        = 0;
          wndclassex.lpfnWndProc                  = DllWndProc;
          wndclassex.cbClsExtra                  = 0;
          wndclassex.cbWndExtra                  = 0;
          wndclassex.hInstance                  = hInt;
          wndclassex.hIcon                        = LoadIcon(NULL,IDI_APPLICATION);
          wndclassex.hCursor                        = LoadCursor(NULL, IDC_ARROW);
          wndclassex.hbrBackground            = (HBRUSH)NULL;
          wndclassex.lpszMenuName                  = NULL;
          wndclassex.lpszClassName            = "DLL_RECIEVER";
          wndclassex.hIconSm                        = LoadIcon(NULL, IDI_APPLICATION);

          if (!RegisterClassEx(&wndclassex))
                return 1; //this is where i get my failures in debug mode

          dll_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"DLL_RECIEVER","",WS_OVERLAPPEDWINDOW,100, 100,100, 100,NULL, NULL,hInt, NULL);

          if (!dll_hwnd)
                return 2;

          ShowWindow(dll_hwnd,SW_HIDE);
          UpdateWindow(dll_hwnd);

          if (WSAStartup(MAKEWORD(1,1), &info) != 0)
          {
                DLL_Unload();
                return 3;
          }

          return 0;
    }*/
    0
     
    LVL 86

    Expert Comment

    by:jkr
    1st of all, I'd grab the DLL's instance handle in 'DllMain()', e.g.

    HINSTANCE   g_hThisDll;

    int APIENTRY    DllMain (   HINSTANCE   hInstance,
                                DWORD       dwReason,
                                LPVOID      lpReserved
                            )
    {
        if  (   dwReason    ==  DLL_PROCESS_ATTACH)
            {

                g_hThisDll  =   hInstance;
            }

        return( TRUE);
    }

    Then, you'll need to zero-out the WNDCLASSEX struct, e.g.

         WSADATA info;
        WNDCLASSEX     wndclassex;
        ZeroMemory(&wndclassex,sizeof(WNDCLASSEX));
        wndclassex.cbSize                    = sizeof(wndclassex);
        wndclassex.style                    = 0;
        wndclassex.lpfnWndProc               = DllWndProc;
        wndclassex.cbClsExtra               = 0;
        wndclassex.cbWndExtra               = 0;
        wndclassex.hInstance               = hInt;
        wndclassex.hIcon                    = LoadIcon(NULL,IDI_APPLICATION);
        wndclassex.hCursor                    = LoadCursor(NULL, IDC_ARROW);
        wndclassex.hbrBackground          = (HBRUSH)NULL;
        wndclassex.lpszMenuName               = NULL;
        wndclassex.lpszClassName          = "DLL_RECIEVER";
        wndclassex.hIconSm                    = LoadIcon(NULL, IDI_APPLICATION);

        if (!RegisterClassEx(&wndclassex))
             return 1; //this is where i get my failures in debug mode

    Then, find out what went wrong by calling 'GetLastError()' after 'RegisterClassEx()' fails.
    0
     
    LVL 86

    Expert Comment

    by:jkr
    Ooops - the line

        wndclassex.hInstance               = hInt;

    should then be

        wndclassex.hInstance               = g_hThisDll;
    0
     

    Author Comment

    by:computing
    BOOL APIENTRY DllMain( HANDLE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
                                   )
    {
        switch (ul_reason_for_call)
          {
                case DLL_PROCESS_ATTACH:
                case DLL_THREAD_ATTACH:
                case DLL_THREAD_DETACH:
                case DLL_PROCESS_DETACH:

         }
        return TRUE;
    }


    hmmm we seem to have different DLLMAIN implementations, which is why i needed to be passed an hinstance... im using vc++ which automatically set up the dll main function for me... could i typecast HANDLE hModule into an HInstance or something? Also im not sure what significance zeromemory would have, ive always used registerclass like that.. if there was a problem, id be getting a consistant failure, rather than it crashing only in debug mode wouldnt i? the get lasterror thing is a good idea... im going to try the zeromemory, and if it works ill give you the points for it.. if anyone else knows why registerclass is failing only when im in vb's debug mode, please let me know... for the record, the code of line i use in vb is App.Hinstance
    0
     
    LVL 86

    Expert Comment

    by:jkr
    >>ould i typecast HANDLE hModule into an HInstance or something?

    Well, the signature should be like in my sample above. BTW, you can of yourse always use

    wndclassex.hInstance               = GetModuleHandle("MyDll.dll");
    0
     

    Author Comment

    by:computing
    well just to update you guys, nothing so far has worked... what ive done instead is made the program write to a debug file, so basically i went back to using the windows message queue, and I dont have to debug in vb anymore... unless someone knows specifically whats going on, consider topic moot... thanks for trying (jkr)
    0
     

    Accepted Solution

    by:
    PAQed with points refunded (250)

    modulo
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now