Tag Archives: Visual Studio

Visual Studio 2008 and SQL 2005 Careful…

Since Visual Studio 2008 came out yesterday, I installed it. One gotcha – if you already have SQL 2005 installed, check custom on the install steps, because VS2008 will install SQL Express 2005 and start up an instance of that, I had to then uninstall SQL 2005 Express. PITA :)

C# .NET Test Driven Development with Visual Studio 2005

As of late I have been stuck in C++ world. MFC/Win32, pointers and HRESULT’s. Ugh. Earlier this week I got a chance to get back to C# and .NET (aka My Roots).

I needed to create a class library for a project, basically from scratch, since there was no existing library created. I started to go at it, and about 2 minutes into it, I was like, wait, let me try to do this test first. Test Driven Development (TDD) is one of the pillars of the Agile development methodology. In the past, there has always been existing code, that was written Non-TDD, so basically what you would end up doing is adding tests to this existing code. When doing that, you kind of get into a rut where you find it easier (at least you think it is easier), to add new code, then write tests after.

So, I fired up Visual Studio 2005, created a solution, added a test project, with a test class, and one method, and started writing a test, basically on how I would think I would exercise my non existing class library. By doing this, it really makes it easier in my opinion to practice TDD.

Run the test, doesn’t even compile, why? because I haven’t even created my class library project yet. No compile = Tests Fail. What I need to do is make my tests pass. So I add just enough (but not to much) to make it pass.

I keep adding to my class library so I can get my tests to pass, and after a while, I have a nice tight class library for my project, fully tested, great code coverage, and more confidence in my code.

What is really nice, is that when I actually needed to use the class from a webpage, I basically copied my unit test code into the web page and it works, just as expected.

I’d have to say that with Visual Studio 2005, and .NET languages, doing TDD is really kind of fun. People will always say, “well you spend more time writing tests than code”, and well, yes, that might be true the first time you are writing your class library, if a bug does happen to come up, you basically have everything in place to track it down, whereas if you do not have tests, then you spend your time debugging and tracking down things for hours. A little time up front saves you tons of time down the road, and you become way more confident in your code base.

If you haven’t tried TDD with VS2005, go for it!! I bet you will really enjoy it!

VS2005: Missing Project Template – Where did my Console App Template Go?

So I fired up VS2005, and wanted to make a quick prototype C# Console Application, except I couldn’t find the Project Template!!

Found out that if you close Visual Studio,  run the VS CMD prompt, and run this: “devenv /installvstemplates”

It should add the templates back.

Good to go!

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.

.NET 3.0 WPF/WCF Templates for VS2005

Microsoft recently released the .NET Framework 3.0 (WinFX) that includes Windows Presenation Foundation, Windows Communication Foundation, Windows Workflow Foundation, and Windows CardSpace. If you are trying to develop using the WPF or WCF using VS2005, you might be wondering if there are VS2005 Templates avail for these project types. You would think that by installing the Vista SDK and the .NET Framework 3.0 you would have them, but no. After doing some digging, there are extensions for WWF for Visual Studio. For WPF and WCF, you are left with the November 2006 CTP Release of the Extensions, which is the “Cider” Release. I guess from what I am reading, there won’t be RTM versions of these extensions, well, not until the Visual Studio release of “Orcas”.

Now, I guess I don’t mind that the CTP versions are what you are supposed to use, but the problem is there is no where I can find (in one place) where all of this is laid out and specified. You have to dig around on forums and what not. Microsoft should lay out what is what so you don’t have to second guess.

After installing the RTM 2006 Extensions, you should see the .NET Framework 3.0 projects types in VS2005 as shown here:

dotnet3_projecttemplates.JPG

VS2005 C++ Unit Tests – System.AccessViolationException: Attempted to read or write protected memory.

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt..

If you have VS2005 C++ Unit Tests, written in managed C++ as they should be, and you are calling native code from them, and you see that error when running your unit tests – I have found it to mean that you are using insecure versions of methods (strcpy, etc). If you change the method calls to the secure versions, you should see your tests pass!

VS2005 LNK1104 Error – cannot open file

If you are referncing another lib in a project and you goto build and get

fatal error LNK1104: cannot open file ‘C:MyProjectDebug.obj’

Do a double check in the project you are referencing configuration. If you have a space in the name (Say “Debug Test”) you might see this error and scratch your head. If the project you are referencing outputs to $(ConfigurationName), then the output directory will have a space. Seems that when you reference that project or lib, it doesn’t like it with a space in the path name, doh! :)

VS2005 MFC Class Wizard Changes

If you ever used Visual Studio/C++ 6 (98), you would notice that the MFC Class Wizard allows you to set up messages as you create a class. Well, I’m not sure about VS2003, but VS2005, when you use the MFC Class Wizard, there are no options for setting up messages and events. Where did the options go?!?! Well, if you click on the class in class view, there are more little buttons on the properties window. If you click on the little icon next to the lightening bolt, you will see you can then set up messages for your class. Easy as 1-2-3!

VS2005 – Browser Helper Object (BHO) Tutorial

I have been dabbling with BHO’s for some time, way back with VB6, then tried in .NET, and with C++ as well. There are so many cool things you can do with them. Anyways, most of the documentation out there is sparse and old. I told myself the next time I have to make one, I am going to document it. Well, here it is in all its glory. Sorry if the code formatting is wacked, but you get the picture. I don’t claim to be an expert, but this should work :)
just in case, I uploaded it in txt format for better reading here

How to Create a Browser Helper Object in Visual Studio 2005 with C++
———————————————————————
1) Open Visual Studio 2005
2) File->New->Project
3) Visual C++
4) ATL
5) ATL Project
6) Name is whatever you want for this example I use “Company.Browser.Helper” without the quotes

7) The ATL Project Wizard Screen will appear, Hit Finish
8) Visual Studio will load up your project.
9) Right Click on The Company.Browser.Helper project, Add->Class
10) select ATL Simple Object, and click the Add button
11) fill in the ShortName – “BrowserHelper” without the quotes, the rest of the fields should fill in, hit next
12) IMPORTANT: Under Support: Check all boxes (ISupportErrorInfo, Connection points, IObjectWithSite (IE object Support)
13) Click Finish

Now on to the better stuff,

————————————————————–

In visual studio, Solution Explorer, Resource Files, you will see BrowserHelper.rgs, open it and add this to the bottom
(replace the GUID with the GUID that you see at the top of the file like CLSID = s ‘{GUID}’) in the other script code
This will register the BHO with IE when the DLL gets registered


HKLM
{
SOFTWARE
{
Microsoft
{
Windows
{
CurrentVersion
{
Explorer
{
'Browser Helper Objects'
{
{GUID}
}
}
}
}
}
}
}

above where it says ‘Browser Helper’ – you can change those names to be more descriptive, that will show in IE add on manager

————————————————————–

————————————————————–

// in the Header Files -> stdafx.h, the bottom part of the file should look like this:

#include "resource.h"
#include
#include
#include

#define CAtlString CString

using namespace ATL;

--------------------------------------------------------------

//in Source Files-> BrowserHelper.cpp you need to include

#include

--------------------------------------------------------------

Then you need to implement methods listed here. You really shouldnt have to modify these

// this should be created by the wizard
STDMETHODIMP CBrowserHelper::InterfaceSupportsErrorInfo(REFIID riid)
{
static const IID* arr[] =
{
&IID_IBrowserHelper
};

for (int i=0; i FindConnectionPoint(DIID_DWebBrowserEvents2, &spCP);

if (FAILED(hr))
{
return hr;
}

// Subscribe the event handlers to the container
hr = spCP->Advise(reinterpret_cast(this), &m_dwCookie);

return hr;
}

STDMETHODIMP CBrowserHelper::IEUnAdvise(void)
{
HRESULT hr;
CComPtr spCP;

// Receives the connection point for WebBrowser events
hr = m_CPCptr->FindConnectionPoint(DIID_DWebBrowserEvents2, &spCP);

if (FAILED(hr))
{
return hr;
}

// Unsubscribe the event handlers to the container
hr = spCP->Unadvise(m_dwCookie);
return hr;
}

STDMETHODIMP CBrowserHelper::IEQuit(void)
{
return IEUnAdvise();
}

------------------------------------------------------------------------------------------

//then you need to implement the invoke method, as you can see i just care about the IEBeforeNaviate2() method
//so i commented the rest of the case statements out but left them in case i need them

STDMETHODIMP CBrowserHelper::Invoke(DISPID dispidMember, REFIID riid,
LCID lcid, WORD wFlags,
DISPPARAMS* pDispParams,
VARIANT* pvarResult,
EXCEPINFO* pExcepInfo, UINT* puArgErr)
{

if (!pDispParams)
{
return E_INVALIDARG;
}

switch(dispidMember)
{
case DISPID_BEFORENAVIGATE2: // Before Naigation
IEBeforeNavigate2(pDispParams);
break;

// case DISPID_COMMANDSTATECHANGE:// Command state change
// IECommandStateChange(pDispParams);
//break;

// case DISPID_DOCUMENTCOMPLETE: // Document completed
// IEDocumentComplete(pDispParams);
//break;

// case DISPID_DOWNLOADCOMPLETE: // Download completed
// IEDownloadComplete(pDispParams);
//break;

// case DISPID_NAVIGATECOMPLETE2: // Navigation completed
// IENavigateComplete2(pDispParams);
//break;

// case DISPID_NEWWINDOW2: // Open a new window
// IENewWindow2(pDispParams);
//break;

// case DISPID_PROGRESSCHANGE: // The progress status change
// IEProgressChange(pDispParams);
//break;

// case DISPID_STATUSTEXTCHANGE: // The status bar text change
// IEStatusTextChange(pDispParams);
//break;

// case DISPID_TITLECHANGE: // Title change
// IETitleChange(pDispParams);
//break;

// case DISPID_ONQUIT: // Quit
// IEQuit();
//break;
}
return S_OK;

}

------------------------------------------------------------------------------------------

//then implement BeforeNavigate2 - you can see the relevant code between the ***** that will
//check if IE is on google and redirect them to yahoo. You can add more code here like checking registry for a key,
//launching a process, etc, etc

void CBrowserHelper::IEBeforeNavigate2(DISPPARAMS* pDispParams)
{
CComQIPtr WebBrowser2Ptr;
CAtlString url;
VARIANT_BOOL* ptrBoolCancel;
VARTYPE vt;

// Check the type of IWebBrowser2
vt = pDispParams->rgvarg[6].vt;
if(vt == 0x0009)
{
WebBrowser2Ptr = pDispParams->rgvarg[6].pdispVal;
}
else
{
// Wrong type, return.
return;
}

// Check the first parameter type is VT_BYREF|VT_BOOL or not
vt = pDispParams->rgvarg[0].vt;
if(vt == 0x400B)
{
ptrBoolCancel = pDispParams->rgvarg[0].pboolVal;
}
else
{
// Wrong type, return.
return;
}

// Check the URL parameter type
vt = pDispParams->rgvarg[5].vt;
if(vt == 0x400C)
{
USES_CONVERSION;
url = OLE2T(pDispParams->rgvarg[5].pvarVal->bstrVal);
}
else
{
// Wrong type, return.
return;
}

// *********************************************************

// check for a given URL
if(url == _T("http://www.google.com/") || url == _T("http://www.google.com"))
{

BSTR newUrl = _T("http://www.yahoo.com/");

*ptrBoolCancel = TRUE;
WebBrowser2Ptr->Navigate(newUrl,NULL,NULL,NULL,NULL);

}

// ********************************************************
}

----------------------------

//you will need to wire these up in your header file, so in HEader Files->BrowserHelper.h it would look like this

----------------------------

// BrowserHelper.h : Declaration of the CBrowserHelper

#pragma once
#include "resource.h" // main symbols
#include "ExDisp.h"
#include "CompanyBrowserHelper.h"
#include "_IBrowserHelperEvents_CP.h"

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

// CBrowserHelper

class ATL_NO_VTABLE CBrowserHelper :
public CComObjectRootEx,
public CComCoClass,
public ISupportErrorInfo,
public IConnectionPointContainerImpl,
public CProxy_IBrowserHelperEvents,
public IObjectWithSiteImpl,
public IDispatchImpl
{
public:
CBrowserHelper()
{
}

DECLARE_REGISTRY_RESOURCEID(IDR_BROWSERHELPER)

BEGIN_COM_MAP(CBrowserHelper)
COM_INTERFACE_ENTRY(IBrowserHelper)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
COM_INTERFACE_ENTRY(IConnectionPointContainer)
COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()

BEGIN_CONNECTION_POINT_MAP(CBrowserHelper)
CONNECTION_POINT_ENTRY(__uuidof(_IBrowserHelperEvents))
END_CONNECTION_POINT_MAP()

public:

// ISupportsErrorInfo
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

//
// IDispatch Methods
//
STDMETHOD(Invoke)(DISPID dispidMember,REFIID riid, LCID lcid,
WORD wFlags, DISPPARAMS* pdispparams,
VARIANT* pvarResult, EXCEPINFO* pexcepinfo,
UINT* puArgErr);

//
// IOleObjectWithSite Methods
//
STDMETHOD(SetSite)(IUnknown *pUnkSite);

DECLARE_PROTECT_FINAL_CONSTRUCT()

HRESULT FinalConstruct()
{
return S_OK;
}

void FinalRelease()
{
}

public:

private:
CComQIPtr m_CPCptr;
DWORD m_dwCookie; // Connection Token - used for
// Advise and Unadvise

enum ConnectType { Advise, Unadvise }; // What to do when managing
// the connection

STDMETHOD(IEAdvise)(void);
STDMETHOD(IEUnAdvise)(void);
STDMETHOD(IEQuit)(void);

void IEBeforeNavigate2(DISPPARAMS* pDispParams);

};

OBJECT_ENTRY_AUTO(__uuidof(BrowserHelper), CBrowserHelper)

----------------------------

And Finally,

To Test:

you shouldnt have to create or modify any more code, if you build it, you should get a dll

then to register with the system you need to call

regsvr32 DllName.dll

now open a browser and goto http://www.google.com and it will redirect to yahoo!

and if you look at the loaded add-ons, it should be in the list

close all your browsers

and to unregister

regsvr32 /u DllName.dll

open a browser and try google, it should work as normal

-------------------------------------------------------------

Unit Testing Verisign Payflow Pro with VS2005

When you set up payflow pro on a system, you usually are going to use it through the web, you usually dump the certs folder into the inetsrv folder and it works fine. Thing is, when you are using VS2005 Unit Testing, you arent on the web, you can even try setting an HttpContext, but that still doesnt work, you end up with:

Get error “RESULT=-31&RESPMSG=The certificate chain did not validate, no local certificate found, Cert Path = certs, Working Directory = C:\Projects\MyProject\TestResults\user.name_COMPUTERNAME DateTime\Out”

before you make your request in your tests, do this:
System.Environment.SetEnvironmentVariable(“PFPRO_CERT_PATH”, “c:\certs\”, EnvironmentVariableTarget.Process);

Copy your certs directory to your C: drive, or you can set the path wherever – mostly likely you would want to add it to your test project, and then set the path when you setup your environment variable as releative to your project.

When you run your unit tests again, you should see a valid response and the request to Verisign should go through correctly.