Monday, 25 May 2009

C++ Trivia :

Some trivia for you ( while I wait for a compile of 22 projects to take place… ) why does the code below not compile ?


The Problem


class AnotherClass
{};
class SomeClass
{
public:
SomeClass( AnotherClass anotherClass ){};
SomeClass( int someint ){};
void TheMethod(){};
};
int main(int argc, char* argv[] )
{
AnotherClass anotherClass;
SomeClass willCompile( anotherClass );
willCompile.TheMethod();

SomeClass wontCompile( AnotherClass() );
wontCompile.TheMethod();//error C2228: left of '.TheMethod' must have class/struct/union

return;
}


Confused.. good then I am not the only one…Lets take a quick detour to something that appears to be totally unrelated.


Quick Detour


Now given that one of the design decision of C++ was that it would be a superset of C, it has to allow for you the ability to define function pointers. So for a simple method you can define a prototype like this in your code for a method that takes 1 param ( typically you would do this as part of a typedef )


int Function(int a); 

As a short cut you can define your prototype without any param names ( since they are meaningless at this level ). 
int Function(int); 



In the same way you can also define the prototype for a method that takes a function pointer as a param


void TakesMethod( void Function( int  a ) );



Taking advantage of the “shortcut” you can equally write 


void TakesMethod2( void ( int ) );


The issue


Right so after a quick C/C++ lesson.. can you see the issue… ? Let make it a bit more obvious.. lets define a method that take a function pointer to a method that takes 0 params


void      TakesMethodNoParams(  void          () );
SomeClass wontCompile ( AnotherClass () );


Yup we just confused the compiler… it thinks we are defining a prototype for a method takes a function pointer as its single param, which is a method that takes no params, and returns a type of Anotherclass

Friday, 3 April 2009

Sharing VS Breakpoint across machines

I came across this interesting post by John Robbins where he talks about a VS add-on that he has written that allows you to save to file all the break points that you have. This is really cool if you have one environment set up exactly the way you want it – but have to now do it on a different machine.

http://www.wintellect.com/CS/blogs/jrobbins/archive/2008/07/21/debugger-settings-visual-studio-add-in-easily-copy-breakpoints-between-machines.aspx

Sunday, 22 February 2009

PowerShell : Dealing with a ‘-‘ literal in data

Lets say for example that you are parsing an xml document with PowerShell and one of the nodes is called “direct-message” as in the Twitter REST API, and using the cool PowerShell dot notation for navigating the document you might expect that you can just do this


[array]$messages = $xml.direct-messages.direct_message


but sadly you will just get this error


You must provide a value expression on the right-hand side of the '-' operator.

At C:\Utils\TwitterClient\TwitterClient.ps1:12 char:38


+ [array]$messages = $xml.direct- <<<< messages.direct_message



Basically PowerShell is seeing the ‘–’ and treating it as an operator.. but all is not lost because you can simply just escape the offending node in single quotes, as such :




[array]$messages = $directXml.'direct-messages'.direct_message

Saturday, 21 February 2009

Regex : Matching a word, how hard can it be ?

You would be tempted to think that matching a word in sentence would be an easy thing to do with a regex.. but not a simple as you might have expected.

Lets take a typical twitter tweet for example where we want to pull out the URLs :

I found this cool site http://tinurl/somevalue and this one too http://tinurl/somevalue

I first tried :

(http:.*?)\s

But alas no, you don’t get the last url… which is obvious since the last ones does not have a trailing space – so knee jerk to only look for the space if it is thre

(http:.*?)\s*

umm dope.. now I only get the http: part ( duh! ) ….Right, now I will get you,  should be just as simple as searching for a space or the end of the line so -

(http:.*?)[\s|$] – fail
(http:.*?)[\s$] – fail
(http:.*?)[\s|\b] – fail

Right so I suck at regex, and someone better at this would probably solve this in their sleep…

What should have been a simple regex is starting to look overly complex so I found a cunning way to work around this – put a space at the end of the sentence – and hey presto you get them both with the first a simple looking regex – ahhh.

What to play around with regex’s with a nice visual feedback then check out http://www.gskinner.com/RegExr/

Tuesday, 17 February 2009

Break Pointing a Windows API

Have you ever wanted to know when a method is being called – so you think, not problem I will just put a break point there and bob’s your uncle there you go. Then one day you want to do the same with one of the windows dll API’s eg CreateThread.

So what to do ? There does not appear to be any cunning way to do this with Visual Studio ( I am keen to hear if you know me to be wrong! ), but you can do it with the power that is WinDbg which you can get as part of the Debugging Tools for Windows.

Now this is not as easy tool to just pick up and use ( at least not well ) but it is nice and simple in that you don’t have to do too much config to get it going.. it just works! Hey I got it up an running – how hard can it be !

So what do you do if just want to simply break point the CreateThread :

1) Launch WinDbg
2) Ctrl+E : Point it to your exe
3) Ctrl+S : Point to your symbols
4) Ctrl+P : Point to your source

Right now you have all the information you could want ( probably more ).. now lets add that illusive break point using the edit box at the bottom ( basically a command line )

5)

image

Ahh don’t you love it when it is simple :-)

6) F5 : Then let it run

Then it will run and break, and wait for you

image
7) k : and view the call stack ( or kP if you want extra param info in your call stack )

Monday, 16 February 2009

Getting the rest of the params in a batch file

If you try this you will be sadly disappointed

set FIRST=%1
shift /1
set THEREST %*

The best way I have seen to do this is as follows :

for /f "tokens=1*" %%a in ("%*") do (   
    set FIRST=%%a   
    set THEREST=%%b)

Thursday, 12 February 2009

Fighting WIX Merge Module

This one caused me some pain…. imagine if you are trying to use Wix3 to create a nice merge module for a C++ application in VS2005. Chances are you are going to end up with this error

Error 7 Undefined preprocessor variable '$(var.MyProject.TargetFileName)'

if you have a wxs that looks something like this :

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Module Id="TestMergeModule" Language="1033" Version="1.0.0.0">
    <Package Id="ef2a568e-a8db-4213-a211-9261c26031aa" Manufacturer="Me" InstallerVersion="200" />
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="MergeRedirectFolder">
        <Component Id="MyProject" Guid="{1081C5BC-106E-4b89-B14F-FFA71B0987E1}">
          <File Id="Test" Name="$(var.MyProject.TargetFileName)" Source="$(var.MyProject.TargetPath)" DiskId="1" />
        </Component>
      </Directory>
    </Directory>
  </Module>
</Wix>

After much fighting and time wasting I found that this is known bug and there is a request in to fix it, you can see details here on StackOverflow.