Posts

Unsafe Operations with STL !!!

It is UNSAFE to do any operation on an STL container that will modify its size while holding a reference to one of its existing element. What could happen is, when you do an operation, say push_back on a vector, it determines if there is enough space available to add a new element. If there is not sufficient space available, it allocates new space for whole of the data structure and deletes the old buffer. At this point, any reference to one of its elements created prior to push_back would have gotten corrupted. More ...

Consoles for Mr.GUI !!!

Learnt something new, a small one but very useful.

Many times I have seen GUI applications accompanied by console windows that show logs or trace information of the application. How do we do that for our application ?

Any GUI application can create its own console window just by calling AllocConsole Win32 API. Actually any process can use that API to allocate a new console. And the application must also learn to be disciplined enough to FreeConsole. Ok, fine. I used that in my small MFC application and was happy to see the console. But I did not see anything displayed on the console. As we know, each process has its own stdin, stdout and stderr. So redirect the console output of your parent application to the console. How do you do that ?

More ...

Setting Environment Variables !!!

Need to change or set the value of an environment variable programmatically and without the need to restart/log off the machine. I need the change to reflect for all processes, ie, I need to change the global environment value and not the one in the PEB [Process Environment Block] of a process. Frustated with setting the value of an environment variable !!!

For getting the set of environment variables or to get the value of an environment vaible from your C# program, there is the GetEnvironmentVariables/GetEnvironmentVariable API in the System.Environment class. But there is no API for setting the value of an environment variable.

More ...

CoMarshal ... Working in NT, Not in XP !!!

Problem:

I have created a multi-threaded application which works without any problems on a NT-4.0 Workstation/Server. When I try to run the same application in Windows XP, I get an error in a call to CoMarshalInterThreadInterfaceInStream which returns -2147418113.

I have provided a snippet of the code below where the call fails in Windows XP.

Environment: Windows-XP,SP-2,Visual Studio 6.0,SP-4,ATL-3.0

Should I be doing anything different in Windows XP?


HRESULT hr = S_OK;
IUnknown** pp = p->m_vec.begin();

while (pp m_vec.end() && hr == S_OK)
{
   if (*pp != NULL)
   {
         IEvent* pEvent = (IEvent*)*pp;
         IStream* pIStream;

         HRESULT hr = CoMarshalInterThreadInterfaceInStream(
            IID_IEvent,
            pEvent,
            &pIStream
         );

         if(SUCCEEDED(hr))
         {
            CComPtr pMarshalEvent;
            hr = CoGetInterfaceAndReleaseStream(
               pIStream,
               IID_IEvent,
               (void**)&pMarshalEvent
            );

            if(SUCCEEDED(hr))
            {
               hr = pMarshalEvent->NewCurrentCassette(m_pCurrentCassette, m_setBy);
            }
         }

      p++;
   }
}

Thread 2:

More ...

Consts in .NET !!!

I was doing some programming with C# and I had to use some consts as everybody does generally in programming. I had a class that simply had const string variables for my DB table names and stuff like that. My program was not working well and I started debugging and in the debugger, I was shocked to see that the const variables did not show the string values I had assigned. I did rebuilt and other non-sensical stuff like that until I learnt this about the consts in .NET:- const variables in .NET do not exist as variables out of the assembly they exist in. Instead, during compilation, they get embedded - hard-coded, where ever you use them, and so when you debug, you do not see the proper value that you had assigned. For debugging purposes you have to output diagnostic trace messages and verify. More ...

Joining the Game

Everything has a beginning.