Link to home
Start Free TrialLog in
Avatar of computing
computing

asked on

NON-BLOCKING sockets without the windows message loop?

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
Avatar of jkr
jkr
Flag of Germany image

>>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
Avatar of computing
computing

ASKER

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;
}*/
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.
Ooops - the line

    wndclassex.hInstance               = hInt;

should then be

    wndclassex.hInstance               = g_hThisDll;
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
>>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");
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)
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial