Tuesday, September 2, 2008

Visual Studio custom build events

Custom build events in VS can be quite useful, but there are a few things to watch for.

1. Calling a batch file.... be sure to use the "call" command so you can add other processing after calling the batch file.

2. Error handling only looks at the last item (VS just blindly builds a batch file from the text you provide)

So here is my updated Custom Build Event

call $(SolutionDir)CustomBatchFile.bat $(SolutionDir) $(TargetDir)$(TargetFileName)
if errorlevel 1 goto Failed

xcopy /Y /F /R $(TargetDir)$(TargetFileName) z:\*.*
if errorlevel 1 goto Failed

exit 0

:Failed

exit 1

Wednesday, July 2, 2008

My new car got it’s first “owie”.

So my six-year-old son who lives in California came to visit for today. He arrived this morning and I went to the airport to pick him up. I took the new baby so Andrew would see. After picking up my son I discovered the “owie”.

My new (well new to me) Corvette was loosing air in one of the back tires. I went straight to the tire place to get the flat fixed. Fortunately, it wasn’t exactly a flat because the Corvette comes with the cool run-flat tires. The first place I went (Discount Tire) was quite a bit less than impressive. They told me they couldn’t fix the flat but I had to buy a new tire. I could tell a high pressure tactic when I saw one, so I took my car with the ever decreasing air pressure and went to the Goodyear Tire store near my house.

This was a wonderful experience. The guy I spoke to was knowledgeable and helpful. I headed home with my family, although I must admit I didn’t like the idea of leaving my ‘Vette with anyone, no matter how nice they were. The family played games together for a while and then it was time to go get the Corvette.

We hopped in the electric car and headed back to Goodyear. On the way up the first major hill we blew a fuse. Arrrrggggghhhh! So, we pushed it to a safe place, walked back home, got into the Suburban and drove to Goodyear. Luckily we made it this time. I got the Corvette and finally some Good News! The run-flats were not damaged at all!! They suffered no damage driving all that way and will work just fine if I have another problem. Hooray only a $30.00 repair instead of $400.00 plus (apiece) for new tires!

I thought that was pretty amazing especially after I saw what had caused the “owie”. A HUGE nail! I was so impressed with Goodyear’s service that I inquired into the price of new tires from them. Still a little pricey for tires, but they are run-flats and it is a Corvette. Also, turns out the discount place wasn’t very “discount”. Goodyear’s prices were at least $100.00 less per tire!

We went home and used the Suburban to tow the electric car with its new “owie” back to the house. That’s a lot to go wrong for one day. Oh, well. At least it is all VERY fixable without much expense. More good news.

Friday, June 6, 2008

News

I got a new car, it gets 32 MPG and is still fun to drive.

Youngest son is spending two nights with the family this weekend, everyone is excited.

Friday, May 16, 2008

C++ matrix library

I just can't seem to find a matrix library that will do what I want.

Well, Matlab has one, but the also want $5000 dollars. Holy Cow! Batman! that's a lot of green for a math package.

Gotta keep looking.....

Google Docs

Google docs won’t handle docx. Ugggh, converting to a doc kills my pictures. Ok, so let’s try a pdf, nope. Time to punt.

White paper

I'm working on a white paper for work.

Image distortion correction and calibration calculations

Thursday, May 15, 2008

How to find the number of processors in a system.

Here is some useful code I wrote:

[Note: I included hyper-threaded cores as two processores (if enabled).]

int getNumberOfProcessors()
{
int rval = 1;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer[maxCPUs];
DWORD len = sizeof(buffer);

BOOL success = GetLogicalProcessorInformation(buffer,&len);

if (success)
{
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr;

int processorCount = 0;
DWORD byteOffset = 0;
ptr = buffer;
while (byteOffset < len)
{
switch (ptr->Relationship)
{
case RelationNumaNode:
case RelationProcessorCore:
processorCount++;
break;
default:
break;
}
byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
ptr++;
}
rval = processorCount;
}
if (rval > maxCPUs)
rval = maxCPUs;

return rval;
}

Learning to program Alice



My son Jacob is learning to program in Alice. Way to go Jacob!

Tuesday, May 6, 2008

Link

I forgot the link on the previous post:

Link to the About.com programming challenge.

Monday, May 5, 2008

Fastest C++ in the West

I just won the about.com monthly competition. I wrote a program to play the game Set. (A game I don't do very well against the women folk in the family). The program plays the game 10,000 times. It ran 5x faster than the next guy, and his was pretty fast.

I wrote it to use multiple threads so if you have more than one processor it will go REALLY REALLY fast.

The code ain't pretty but it is so screamingly fast it burnt a hole in my hard disk.

My favorite part is the shuffle, to make it fast I interleave (much like a real shuffle) the cards just one time.


// shuffle
register unsigned int i = 0;
register unsigned int j = 80;

do
{
  if ((rbits & (1i64 << i)) != 0) // as we shuffle randomly swap cards
  {
    swapCards(deck[i],deck[j]); // interleave cards
    --j;
  }
} while (++i < 64);

Tuesday, April 29, 2008

Another A

Straight A's so far. Just finished another CS class. Maybe I should be a programmer for a living. (ha, ha)

It used to be really difficult to go to school. But even with all the things there are to juggle in my life, it's nice to finally have support in school.

Tuesday, April 22, 2008

The "Dumb Box"

I work for a small company. My boss does some of the programming. He has a dislike for new technology and even programs in machine code.

The company has a logic analyser that can be programmed to capture processor states. It works much like an ICE (In Circuit Emulator) in that you can watch a program execute live (or almost live). Well, my boss calls it the "Dumb Box", you see true programmers don't need an ICE they write bug free code the first time.

Going back a few years:

While going to school I worked for a Pen Windows company developing a Handwriting Word Processor. It was actually pretty cool you could hand-write a letter on a tablet PC and then reformat it to make it look really nice and pretty. Too bad it was a little before its time.

It was a small company, with just me and the owner writing software. The owner was a physicist turned programmer. He spent considerable time extolling his programming prowess and claiming that 90% of programmers weren't worth their salt.

I had to laugh, it was in the days of Windows 3.1 so we still had memory models (remember those?) and segments and all. He would copy and paste so much code that he would create functions that exceeded a segment in size. The compiler couldn't handle a 64k long function and would give the weirdest error message.

Anyway, another of his oddities was that he would brag about how he NEVER used a debugger. Of course with miles and miles of copy and paste code he would inevitably have a crash. He'd work for hours even days trying to find a crash in his code. He would break into tears and even resorted to prayer on occasion to find the bug. Finally after I could stand it no more I'd walk over to his desk press "F5" on his keyboard and the debugger would stop on the location of the crash, no fuss no tears.

He didn't learn from the experience, every time he had a crash I would say "just press F5" to which he would reply "I've never used a debugger, not even when I wrote my own operating system for the PDP-11, and I never will." Hours or days later a few rants about how Bill Gates has it out for his little company and a prayer or two. And the whole "F5" cycle would repeat itself.

My Boss' mentioning of the "Dumb Box" today reminded me of good times past.

Tuesday, April 8, 2008

Internet down

Our internet went down today. It only went down for 30 minutes, but holy cow, its was everything, no email, no VOIP phone (Broadvoice) only cell phones, it was so 90's.

I have determined to put in a failover router so our Internet will automatically use one of my backup broadband connections. Now I just have to find time to put together a linux box....One more project.

That reminds me I need to start the equalizing phase on the EV's batteries. Hopefully an extra good dose HighV and LowI will bring the batteries back to life.

Friday, March 28, 2008

Programming Challenge

About.com has a Programming challenge to hide a message inside a text file.

So decided it would be fun to try....

Here's my overview from the code:


OVERVIEW:
Spaces are used as carriers to "hide" a message in a file.
Only spaces following a period are candidates for carriers.

As the file is parsed periods followed by spaces are
adjusted to the following:
.[space] = 0 bit
.[space][space] is a non carrier
.[space][space][space] = 1 bit

The message is then written as bits into the carriers distributed
sequentially in the text file.

Thus the message can be hidden at nearly any point in the text file,
and a '.'s can be skipped randomly to reduce detection.

Open Source contributor

Finally after several revisions with my MySQL mentor, my code is going into version 6.0. Kinda cool.

All the other code I've written has disappeared behind corporate doors. I think I like this better.

Colors

It's no secret I'm not the best at colors. No I'm not completely color blind but looking at my Mac it sure FEELS that way.

How do I change the colors? Silver and pale blue who thought of that? Why not brown and green? Or better yet pink on purple! Great color scheme Apple.

Ok, rant's over. Back to your irregularly scheduled blog.

Saturday, March 22, 2008

New Mac

So I'm the proud owner of a Mac Mini. Ten minutes after getting it up and running and I find myself asking "Now what?"

Wednesday, March 19, 2008

Sunday, March 16, 2008

Working at home


Seven monitors, a big desk, and a comfy chair.

That's why I love working at home.

Wednesday, March 12, 2008

VS 2008

What's the deal with VS 2008? I'm working along compiling the latest and greatest code at work and suddenly the message pops up that the linker has crashed. I can't remember the linker ever crashing, heck I've been using Visual Studio since the first version (1.4 I think it's been a while) in Dos/Windows 3.1.

Hit build and the second time works. This worries me nothing has changed, identical source, objs, libs, etc., why would the linker work this time? Well MS what's going on?

I submitted my first patch to MySQL

Here it is:

Hi All,

This is my first attempt to contribute.

(patch details included below)

SUMMARY:

This patch adds the code for the Windows exe to lookup the network MAC for use by the UUID() function. It uses the same API as the windows command ipconfig.exe. (IPHLPAPI) It searches for network cards in the same order as ipconfig, which is handy for testing/verification on systems with multiple network cards.

It looks like there is not an automated test for the UUID function, as least not yet :).

To test the function manually on windows:

On a console window on the server under test run:

> ipconfig /all

Note the "Physical Address" of the first Ethernet adapter.

>From MySQL client run:

> SELECT UUID();

Compare the last 6 hex bytes of the uuid to the results from ipconfig above.
If they are the same the function passes.

Testing Caveat: Will fail under Win95,98 and WinNT 4 < sp4 the MAC will not be read, UUID generates random # instead.

This was fun...your comments, questions and input are appreciated.

Regards,
-Chris


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


--- /home/chris/my_gethwaddr.c 2008-03-10 18:55:00.000000000 -0600
+++ my_gethwaddr.c 2008-03-07 06:46:29.000000000 -0700
@@ -102,115 +102,11 @@
}

#else /* FreeBSD elif linux */
-
-#ifdef __WIN__
-
-/*
- There are header conflicts with ras.h and mprapi.h. The following
- definitions keep those from being included and defines the one constant that
- is still required.
-*/
-#define _RAS_H_
-#define __ROUTING_MPRADMIN_H__
-#define MAX_INTERFACE_NAME_LEN 256
-
-#include
-
-/*
- The following typedef is needed since we are dynamically loading
- iphlpapi.dll. Dynamic linking is used because static linking would cause
- problems with older versions of windows (WIN95, WIN98 and WINNT4 < SP4).
-*/
-
-typedef DWORD (WINAPI *pfnGetIfTable)(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder);
-
-/*
- my_gethwaddr - Windows version
-
- @brief Retrieve MAC address from network hardware
-
- @param[out] to MAC address exactly six bytes
-
- @return Operation status
- @retval 0 OK
- @retval <>0 FAILED
-
- @todo
- Resolve the header conflicts (affects more than just this file) -*/ -my_bool my_gethwaddr(uchar *to)
-{
- HINSTANCE hIphlpapi;
- pfnGetIfTable fnGetIfTable;
- PMIB_IFTABLE pIfTable;
- MIB_IFTABLE ifTable;
- PMIB_IFROW pIfRow;
- DWORD dwSize;
- DWORD dwRetVal;
- my_bool rval;
- int i;
-
- rval= 1;
-
- hIphlpapi= LoadLibrary("iphlpapi.dll");
-/* Iphlpapi is not available on WIN31, WIN95, or WINNT4 < sp4. */
- if (!hIphlpapi)
- goto error1;
-
- fnGetIfTable= (pfnGetIfTable) GetProcAddress(hIphlpapi,"GetIfTable");
- if (!fnGetIfTable)
- goto error1;
-
-/* Ask GetIfTable if one is enough. */
- dwSize= sizeof (MIB_IFTABLE);
- if (fnGetIfTable(&ifTable,&dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER)
- {
- pIfTable= (PMIB_IFTABLE) my_malloc(dwSize,0);
- if (!pIfTable)
- goto error2;
- }
- else
-/* One is enough we don't need to alloc. */
- pIfTable= &ifTable;
-
-/* Get the hardware info. */
- if ((dwRetVal= fnGetIfTable(pIfTable,&dwSize,FALSE)) == NO_ERROR)
- {
- for (i= 0; i < (int) pIfTable->dwNumEntries; i++)
- {
- pIfRow= (MIB_IFROW *) & pIfTable->table[i];
- if (pIfRow->dwType == IF_TYPE_ETHERNET_CSMACD)
- {
-/* Stop on the first network card detected. */
- memcpy(to,pIfRow->bPhysAddr,6);
- rval= 0; /* success */
- break;
- }
- }
- }
-
-/* Clean up memory allocation. */
- if (pIfTable != &ifTable)
- my_free(pIfTable,0);
-
-error2:
-/* Perform library clean up. */
- FreeLibrary(hIphlpapi);
-
-error1:
- return rval;
-}
-
-#elif
-
/* just fail */
my_bool my_gethwaddr(uchar *to __attribute__((unused))) {
return 1;
}
-
-#endif
-
#endif

#else /* MAIN */

Sunday, March 9, 2008

It works - first MySQL bug fixed.

I've always found the best way to learn a new program is to just dig in and fix a few bugs.

My first bug is fixed, the server needs some code to find the MAC address for the network card in Windows. Now I'm reviewing my code to see if I've followed their coding style, etc.

It was supposed to be an easy bug from the report. The bug wasn't quite as simple as implied. First there are three methods in the public domain and on the internet that are really just hacks and were bad choices. I finally picked apart ipconfig and found the API it uses to list all network hardware.

Next problem, if I just linked in the function used to find the MAC address it would cause MySQL to crash when running on the oldest versions of Windows.

So I dynamically loaded the iphlpapi.dll. A benefit to dynamic loading is that it won't add to the exe loading time.

Now to submit the fix.....

Technorati Post

Technorati Profile

Saturday, March 8, 2008

MySQL command line magic

When debugging MySQL (VS 2005) a few helpful command line arguments:
--console ## display server output on a console window (console is nice, debugging services can be painful)
--basdir="C:\MySQL" ## path to root of the DB

I also needed: --skip-grant-tables

Since I forgot the root password to the DB I copied to my dev. box I had to set a new one.
After starting MySQLd.exe with the --skip-grant-tables I took the following steps:

> mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 6.0.5-alpha-pro-debug Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> UPDATE mysql.user SET Password=PASSWORD('xxxxx')
-> WHERE User='root';
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> quit
Bye


Restart without the --skip-grant-tables option and we are back in business.

It's so cool when things work!

Friday, March 7, 2008

Learning the internals of MySQL

I've been playing with MySQL recently, most of my data base work has been with MS SQLServer. But I need a less expensive solution for one of my pet projects, MySQL fits perfectly.

I want to really understand the internals, so I'm actually buiding the source code. So far there have been a few hurdles to jump. But ultimately my goal is to compile and debug completely on windows.

#1 for non Linux developers (that don't work at MySQL), you have to use Bitkeeper's free Linux client to download the code.


I'm primarily a Windows developer so I wanted to avoid putting cgywin on my main dev box. No problem right? Just use linux in a virtual machine, I have VMware pro. that should work. I tried several vmware linux appliances, every one I tried wouldn't boot up or had some other catasrophic problem.

Finally I installed a fresh copy of Ubuntu in a virtual machine, it works perfectly. In fact I'm close to taking Vista (I HATE VISTA!) off my laptop and going to Ubuntu.

So I got the latest version of the code downloaded. I was able to compile and test it from inside of the VM. One step closer to my goal. Next I copied the source to my Windows dev box.

#2 Build the .sln project file so Visual Studio can build the project. I install CMake, and Bison no problem. So I run win\config

C:\Work\MySQL\mysql60\sql>win\configure WITH_INNOBASE_STORAGE_ENGINE WITH_PARTITION_STORAGE_ENGINE MYSQL_SERVER_SUFFIX=-pro
then I run: win\build-vs8.bat and an error occurs in Configure.data
a bug has already been entered for this, but it appears that the fix has yet to be added to the source tree. Edit the file and continue on. Now we have a .sln file.

#3 Even though the docs say they are building a VS 2008 .sln file VS wants to convert it from 2005. This happens with minor issues.

#4 Ok, now we're compiling..... Oh crap there's a compile "error '_vsnprintf': attributes inconsistent with previous declaration" several of them. Looks like Microsoft decided to define _vsnprintf in the newest version of stdio.h. For now I just revert to VS 2005 and everything compiles fine.

--More to come--

Thursday, March 6, 2008

Moving into the 21st century

I work for a small engineering company. This company has been using antiquated technology for some time.

My job, in part, is to modernize everything. Moving these guys to new technologies is kind of like watching snails cross a sidewalk.

The old system (still in use) runs on a 6502 microprocessor, the same one that made the Apple II and Commodore 64 work.

Choosing new technologies is also a challenge, I have to find solutions that they will be comfortable with. No one in the company except me has a clue about Linux. So I rejected embedded linux on a Spartan FPGA embedded processor early on even though it was a perfect fit. They all have Windows PCs on their desks the natural choice was embedded XP running on a small via powered motherboard.

chris' shared items

Twitter Updates

Official blog of Chris Lee Runyan

Fastest C++ in the west.