It would be useful if you tell us where you got the bulk of the code from so we could see the original (which presumably works).

This looks wrong:
(LPCWSTR)"COM1"
Try this:
TEXT("COM1")

Also, your if/else if structure has more braces than necessary. This:
Code:
if()
{
}
else
{
  if()
  {
  }
  else
  {
    if()
    {
    }
    else
    {
      if()
      {
      }
      else 
      {
      }
    }
  }
}
Is usually written like this:
Code:
if()
{
}
else if()
{
}
else if()
{
}
else if()
{
}
else 
{
}
Or, since each if is testing for failure, it is even cleaner like this:
Code:
if (!FirstThing())
{
    ...
    exit (1);
}

if (!SecondThing())
{
    ...
    exit (2);
}

if (!ThirdThing())
{
    ...
    exit (3);
}

if (!FourthThing())
{
    ...
    exit (4);
}

// Success
...
return 0;