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
computingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>>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
computingAuthor Commented:
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
jkrCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

jkrCommented:
Ooops - the line

    wndclassex.hInstance               = hInt;

should then be

    wndclassex.hInstance               = g_hThisDll;
0
computingAuthor Commented:
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
jkrCommented:
>>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
computingAuthor Commented:
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
moduloCommented:
PAQed with points refunded (250)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.