Archive for category dev

Swig namespace collision

Swig is a lifesaver for using native C++ libraries in a higher level language. One problem I came across this morning, though, if you are referencing two swig-generated libraries in one application, you can get name collisions for similar types. My problem arose out of my use of the %pointer_functions macro. The generated types, SWIGTYPE_p_int, SWIGTYPE_p_long, SWIGTYPE_p_double and SWIGTYPE_p_float were all generated in the root namespace. So, when my .NET application referenced two libraries generated this way, no joy! Unfortunately, it took me a while to work out how to put the swig-generated types into an appropriate namespace, so I thought I’d document it here for you to find. The answer was to be found here, on the SWIG and C# page of the documentation. Simply add -namespace whatever.namespace.you.want to the commandline arguments. too easy!

swig.exe -c++ -csharp -namespace MyCSNamespace -outdir "swig" "script.i"

Tags: , , ,

Translate VB.NET to and from C#

This article once referred to the CarlosAg.net VB.NET to C# (both ways) code translator, which was an awesome tool.

Last time I went to use it, however, it seemed to be broken. I found this alternative.

Tags: , , ,

Determine .NET Assembly Build Date

I needed to get the Build date of an assembly to write a Really Insecureā„¢ time-limited demo for a client. Simply put, the software should only run within 45 days of being built.

Suggestions from other developers included using Visual Studio’s automated build numbering, and converting the build numbers to a date. Jeff Atwood blogged about a better solution he got from another (now 404ed) source.

He reads the Portable Executable file header, reads out the linker timestamp, and converts it to local time. Easy! Below is the code in C#. See here for a VB.NET version.

private static DateTime RetrieveLinkerTimestamp(string filePath)
{
const int PeHeaderOffset = 60;
const int LinkerTimestampOffset = 8;
byte[] b = new byte[2048];
Stream s = new FileStream(filePath, FileMode.Open, FileAccess.Read);
s.Read(b, 0, 2048);
s.Close();
int i = BitConverter.ToInt32(b, PeHeaderOffset);
int SecondsSince1970 = BitConverter.ToInt32(b, (i + LinkerTimestampOffset));
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0);
dt = dt.AddSeconds(SecondsSince1970);
dt = dt + TimeZone.CurrentTimeZone.GetUtcOffset(dt);
return dt;
}

Other authors have used the interpreted version number technique, including Kevin Gearing and this article on CodeProject. Still others have simply used the file modification date. Another possibility is embedding the build date in the description of your assembly. This strategy seemed a little more reliable than the others. After all, this is serious business.

Tags: ,