Categories
Geeky/Programming

Visual Studio 2005 – C++ Unit Testing – Not so good

So, as of late, I have been programming more in C++ than in C#/.NET. The first order of business was getting everything to Visual Studio 2005, which has been accomplished. In .NET, there is built in Unit Testing, Code Coverage, Refactoring, etc. In Visual Studio C++ unmanaged/native C++, you don’t get any of that. (Thanks Microsoft!) Now, if you code in managed C++, you get all the nice features (I’m pretty sure you get all of them).

What you can do, and there are some articles/blogs on the net, is set up a Unit Testing project in managed C++ and then link in your managed C++ projects. This works. Sometimes. It looks better on paper than when you actually try to implement it, depending on your environment and how you have things laid out.

We are making static MFC (probably the first problem – MFC :)) libs for a Core library, and we had to tweak a bunch of settings (ie: make a new build configuration), just so we could link into the managed C++ projects. There were numerous issue, just too many to list here. Things just don’t work nice together.

When we managed to get things to actually compile, and run, then the code coverage would show all the Microsoft API’s as not covered, since the libs were statically linked in to the test project.

Overall, my experience with Visual Studio 2005 and unit testing has been a good one. As long as you are using VB.NET or C# 🙂 

It is so nice since it is integrated into the IDE, and it can make unit tests for you from existing code. This all would be a god send for C++, yet, there isn’t anything there.

And as long as I am griping about it, intellisense in C++ really isn’t that good. I did some research and found Visual AssistX, and we purchased it. Really is worth the money. Adds refactoring and intellisense on crack compared to the built in intellisense.

Anyways, I will follow up with a few more posts with my experiences on C++ unit testing. I tried a few other frameworks, and actually got CppUnit to work well, so I will blog on the steps I took to make it work.

Just because you develop in C++ doesn’t mean you can’t develop with an Agile mindset, it is just a little bit harder to get started. Unit Testing, Refactoring, Code Coverage, and then Continuous Integration. Hopefully over time I will get some more posts up about these things and how as a C++ developer using Visual Studio 2005 you can accomplish them (or at least the way I did it) 🙂

By no means am I saying I know the best way, but it seems that there isn’t much out there talking about this stuff for a native/unmanaged C++ dev using Microsoft technologies, or maybe I just can’t find it.

Technorati tags: , , , , , , , , , , , , , , ,
Categories
Geeky/Programming

C++ OutputDebugString()

I have programmed in many different languages, and as much as I can remember, one of the best things you can do is write debug ouput to an output window

VB 6.0 – Debug.Print “Debug Output”

.NET – System.Diagnostics.Debug.WriteLine(“Debug Output”);

and recently I have been doing some VC++ programming, and I didn’t know how to output to the output window, so I decided to figure that one out. Came across OutputDebugString();

Now I can stop throwing MessageBox’s all over the place when trying to debug something. What is cool to is that there are utlities that will read the messages from a debug output, like SysInternals Microsoft’s DebugView

One thing that is kind of weird, is that even if you build in Release mode, debug messages still come out. You could and probably should I am guessing wrap then OutputDebugString() in a DEBUG define, or even better IsDebuggerPresent()

Anyways, I’m glad to have taken 2 minutes out of my coding to find this instead of going along thinking there aren’t easier ways. 🙂

Categories
Geeky/Programming

Executing MDX – Errors

So, a while back I fired up Visual Studio 2005, and was making some Reporting Services reports. I need to do some custom MDX, and was getting errors trying to exceute it. WTF right?

Query preparation failed. -> Error in the application. (msmgdsrv)

Nice error, as always. So, did some digging, and actually found a hack around it, since running the update that was supposed to fix it didnt seem to want to work.

Query preparation failed.
-> Error in the application. (msmgdsrv)

Brian Welcher has a blog post describing the work hack around it to make it work
“To manually correct this, ensure the versions of msmdlocal.dll and msmgdsrv.dll in the following directories match the versions in the %Program Files%Common FilesSystemOle DB directory.

  • %Program Files%Micorosft Visual Studio 8Common7IDEPrivateAssemblies
  • %Program Files%Microsoft SQL Server90ToolsBinnVSShellCommon7IDE
  • %Program Files%Microsoft SQL ServerMSSQL.*Reporting ServicesReportServerbin”

I did that and it worked, which is great. But now I sometimes see some odd behavior with SQL and Visual Studio and I always wonder if something is jacked because of changing those dll’s. Well, hopefully I don’t see the issue the next time I reload my machine.

After installing Office 2007 Beta, SQL 2005 and then Excel Add On’s and God knows what else, it’s just amazing anything works on my machine. Fun stuff!!

Categories
Geeky/Programming

First-chance exception at 0x7c812a5b in : Microsoft C++ exception: CError at memory location

If you are programming and you have some calls to ShellExceute in your code, and you are debugging, you might see this fly across the output window

First-chance exception at 0x7c812a5b in : Microsoft C++ exception: CError at memory location

The app will be your exe name, and location will be a memory location.

You can reproduce this by calling ShellExecute with a url as the file param like so:

ShellExecute(NULL, “open”, “http://www.stevienova.com” , NULL, NULL, SW_SHOWNORMAL);

You should have a path to a file, but what Windows does is look for the http:// or .com and then does a lookup in the registry for the default handler, which more than likely is Internet Explorer, or Firefox, or whatever is your default browser. More info here

INFO: Use ShellExecute to Launch the Default Web Browser KB 224816

I haven’t tried catching the exception, but I can’t figure out a way to get rid of it. It doesn’t seem to hurt anything and actually doesn’t throw up an error. Just an annoying little quirk in Windows and C++.

I did a little digging on Google and didn’t seem to find any more info, so I guess it isn’t a huge deal. Ahh, the mystery of Windows!!!

Categories
Geeky/Programming

ASP.NET C# – Grabbing a posted file and save to disk

Sometimes you want to grab a file posted to a page, maybe an image upload or other file upload

HttpFileCollection logFiles = Request.Files;
string path = System.Configuration.ConfigurationManager.AppSettings[“LogPath”];
if (logFiles.Count > 0)
{
HttpPostedFile logFile = logFiles.Get(0);

logFile.SaveAs(path + System.Guid.NewGuid().ToString() + “.log”);
}

As you can see, I am setting the log path from the config. The reason for this is, the path to save to is absolute, something like c:blahblahwhatever

I am saving the file with a GUID as the file name, for uniqueness, but you can grab the name form the file object as well. There are some other gotchas. 4MB limit on files, unless you tweak the HTTP runtime in the web.config, also, you need to make sure that Network Service or ASPNET user has rights to modify the log folder you are writing too. Otherwise you can impersonate a user in the web.config, and make sure that user has rights.

Categories
Geeky/Programming

C++ – Release Your Buffers

I am in the process of taking some 3rd party c++ source code, and converting it to VS2005. One thing I noticed over and over, is that when you are reading in file contents, you need to make sure to release the buffer, but with the length of the file as the parameter.

For example, this code:

CString myData;

if (file.Open(lpszFilename, CFile::modeRead))
{
DWORD dwLength = file.GetLength();

file.Read(myData.GetBuffer(dwLength), dwLength);
myData.ReleaseBuffer();

file.Close();
}

will throw an assertion when in debug mode

GetData.JPG
to fix, add the dwLength value to your ReleaseBuffer()

myData.ReleaseBuffer(dwLength);

if you run the bad code in release mode, nothing happens, which suprises me, you would think that the runtime would croak, but it doesn’t. Anyways, make sure to release your buffers.

Categories
Geeky/Programming

error C2440: 'static_cast' : cannot convert from 'UINT (__thiscall CStaticLink::* )(CPoint)' to 'LRESULT (__thiscall CWnd::* )(CPoint)'

If you are updating from VS2003 to VS2005, C++, you might run across an error like this. What it means is that they changes the return types from 2003 to 2005. You just need to change the UINT to an LRESULT and you should be good to go.

Categories
Geeky/Programming

FizzBuzz Test

This is all over the blogs today:

CodingHorror.com. Jeff Atwood writes a great post entitled Why Can’t Programmers.. Program?

Just for kix, I did it, just so I knew I wasn’t crazy 🙂

for(int i = 1; i < 101; i++)
{
if(i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
}
else if(i % 3 == 0)
{
Console.WriteLine("Fizz");
}
else if(i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i.ToString());
}

}

Categories
Geeky/Programming

ForFiles – Delete files older than X amount of Days

Windows 2003 – ForFiles

FORFILES /P C:MyPath /S /M log*.xml /D -30 /C "cmd /c del @file"

/S – look at subdirectories

log*.xml – that is the file pattern, you could have it be like *.txt for example

/D -30 = files greater than 30 days

/C – thats the command you want to run

this is pretty much a one line command that could replace a ton of vbScripts, batch files, and other apps I have seen over the years to clean up files. Something for every sys admin’s toolbelt.