Thursday, November 5, 2009

_tcscpy_s

Microsoft has been promoting to use the _s version for the CRT function. Soon, all the non _s version will be deprecated, perhaps two or three more operating system.

As I am trying to comply with Microsoft, I encountered a unexpected behaviour of _tcscpy_s.

If you have a buffer of lets say 24 byte and you would like to copy a 4 byte long string, based on MSDN documentation, you will do this: _tcscpy (dest, 24, src).

The _tcscpy did the correct job, copying the 4 byte of the string and 1 byte of null character to termintate the string. But hey !!! It did additional job, it messed up the data in offset 6 - 24.

Be careful !!!!

Tuesday, October 6, 2009

Microsoft Visual Studio 2008 Editor blunder

I encounter a very frustating bug in Microsoft Visual Studio 2008.

When you would like to share a code between project (and somehow the flow of the program is slightly different for these two project), I use the #define, #ifdef as the indicator for the compiler.

I have more than one project in the solution. In project A, I defined "_BLABLABLA" in the project property "Preprocessor Definitions" and in the project B, I do not define this. And in these two project, a file (test.cpp, code is included below) is included to both of the project (same file for both project). Guess what happen?

test.cpp
.
.
.
#ifdef _BLABLABLA
printf ("Testing 1");
#elseif
printf ("Testing 2");
#endif
.
.
.


The compiler take this #define and behaves correctly. Unfortunately Microsoft Visual Studio Editor do not.

You can try to navigate thru the Solution Explorer and go to project B and locate the test.cpp and double-click on the file. The file is open and if you take a look, the line contains printf ("Testing 2"); will be greyed out.... But why?????

I have no idea currently..... (I believe this is a bug in Microsoft Visual Studio 2008)

Actually it is ok, but the IntelliSense is not working on the greyed out source code, so you are stuck...

Thursday, October 1, 2009

error C2065: 'GUID_NULL' : undeclared identifier

Have been using Visual Foxpro for more than 12 years and very sad have to switch to other languages as Visual Foxpro no longer supported by Microsoft. Even though I also programs using C now and then during the 12 years, I am never done any GUI programming with that. Most of the times, I only use it to make an interface to Visual Foxpro (as Visual Foxpro natively do not support thread and pointer).

And I just change my job recently (for two months now) and I have to switch to C/C++.

The first two weeks is very miserable as I do not really used to Windows Messages. But now I like it very much even though the programming time is not as short of using Visual Foxpro. (still like Visual Foxpro more..., able to finish few forms in 1 day).

On my first project, I being task to re-enhance the existing application to make it better looking and more friendly. As I am new in C/C++, I do not have ammunition at all. So I have to build all from scratch... Luckily there are sites offer tips and free source code (i.e. codeguru.com and codeproject.com) and also thanks to Win32++ (by David Nash) for his excellent works .

I slowly start building my own library and now after two months I am quite used to C/C++ GUI programming (without MFC).

In these two months, I have made a lot of newbie mistake but I also quite frustated with Microsoft Visual Studio 2008 environment. Start from disapearing line now and then, the not so Intelligent IntelliSense and and and.

And in this post I would like mention about the imfamous compile error: error C2065: 'GUID_NULL' : undeclared identifier.

Try to google it, as from today, there are 741 results found.

By the way, this error is not happen when you turn on the precompiled headers in the Compiler settings (/Yu). But I do not like the idea of having to include stdafx.h in all my source code. I like my source code to be portable as I would like to share it amongs projects. Having to include stadfx.h create a lot of trouble (from what I know currently).

From the 741 results found, here are two of suggested solutions:
1. define your own GUID_NULL in your program
2. includes initguid.h before any others includes

These solution are not working (at least in my project).

After try and error, I found the solution (for my project, not sure if it works for yours).
I include cguid.h before any atl*.h includes and the errors gone.....

Lets hope this will help some of you that having same problem as me.

Have a nice day....