[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

NON-BLOCKING sockets without the windows message loop?

Posted on 2004-10-21
10
Medium Priority
?
317 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
Comment
Question by:computing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12373270
>>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
ID: 12373430
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
ID: 12373498
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:jkr
ID: 12373515
Ooops - the line

    wndclassex.hInstance               = hInt;

should then be

    wndclassex.hInstance               = g_hThisDll;
0
 

Author Comment

by:computing
ID: 12373563
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
ID: 12373628
>>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
ID: 12449785
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:
modulo earned 0 total points
ID: 12723904
PAQed with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

656 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