Sep 07

Whenever an installation is interrupted, you may get this message e.g. I just issued

apt-get remove gaim

and then realized that it was also uninstalling ubuntu-desktop which is a meta package, so I pressed Ctrl + C (while it was still “Reading the database”). A couple of hours later, when I tried to run Adept Installer, it snapped back with the error above. The solution is simple, open the terminal and issue:

sudo dpkg –configure -a

written by ishaq

Aug 29

I have Kubuntu + Windows XP installed on my machine, and I noticed that Kubuntu sometimes did not load the C: (in windows) or sda2 (in Kubuntu). However, since I rarely needed to access this volume from Kubuntu, I never bothered to look deeply into it. (The volume has been formatted as NTFS, but I think the file system doesn’t matter, it is the drive that had windows installed on it).

It was until recently when I accidentally started Kubuntu in recovery mode, where it showed me the complete log that had something like “Unable to load NTFS Volume sda2, Windows has some entries in the log file, make sure that windows was cleanly shutdown… blah blah blah” (It also mentioned some utility program to run if all else failed, i think its name was ntfs-repair or something).

The actual problem was that I had hibernated Windows and it had written the memory dump to boot drive (C: in my case). So, I just started Windows, and made sure to select “Turn Off” from the shutdown dialog instead of hibernate, and Kubuntu loads the drive fine…

Moral: Don’t expect Kubunbu/Ubuntu to load your windows boot drive if you have hibernated Windows OS.

written by ishaq

Aug 14

Hi, (Assalaam o Alaikum, it’s Islamic Greeting)

Today was Pakistan’s Independence Day, our nation won it’s independence in 1947 and the journey towards a (real) free Pakistan still continues…

My day wasn’t very great, I have been suffering from headache, low grade fever and flue since last evening so most of the time I was in bed. let’s hope it gets better tomorrow…

Bye (Fi Amaan-i-llah)

written by ishaq

Aug 04

NOTE:This post was posted to one of my old blogs about a year and a half ago, I am reposting it here because I think it’s useful information.

I am working on an application that needs all of the core functionality to be implemented in ANSI/ISO C/C++ for the code to be portable to multiple platforms. To implement the client front end on windows, I choose C#, so, to access the core modules, I would have to create DLLs from the source code, and call the functions from C# using Platform Invoke. I was amazed when I first came to know how easy it was.

1. Create a DLL in C/C++:
Create a new Visual C++ Project, Click File->New Project in Visual Studio, from the Visual C++ Projects, Choose “Win 32 Project” (NOT “Win32 Console Application”), Name it “MyDll” and Click OK, and in the dialog the appears next, click “Application Settings”, Select “DLL” and Check “Empty Project”.

1.1 The DLL Header
Create a Header File for your functions declarations as you normally would, Creating a DLL is nothing more than putting __declspec(dllexport) before all of your symbols that you want to be exposed from the DLL. Here, I am taking a simple example, it’s a function that adds to integers and returns the result (from my first course on C in the college ;) ). The Header file looks like this:

// MyDll.h
#ifndef _DEFINED_4B904A07_FC20_4589_825A_66604BF297F5
#define _DEFINED_4B904A07_FC20_4589_825A_66604BF297F5

#ifndef _COMPILING_4B904A07_FC20_4589_825A_66604BF297F5
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif //_COMPILING_4B904A07_FC20_4589_825A_66604BF297F5

#define CALL_CONV __stdcall

#ifdef __cplusplus
extern “C” {
#endif // __cplusplus

DLL_API int CALL_CONV sum(int x, int y); // The function being exported

#ifdef __cplusplus
}
#ifdef __cplusplus // __cplusplus

#endif //_DEFINED_4B904A07_FC20_4589_825A_66604BF297F5
 

Explanation:

The above code may seem Greek at first glance, but it’s C :). The first line #ifndef _DEFINED_4B904A07_FC20_4589_825A_66604BF297F5 and the corresponding #define and #endif are standard way to make sure that the header doesn’t get included twice. It may seem a little strange, Actually I created a GUID (Registry Format) from the GUID Creater (found in the Tools menu) and appended it’s value at the end of _DEFINED_, It makes sure that the symbol name can never conflict with any other symbol on planet. The two #ifdef __cplusplus/#endif pairs (notice the one at the before any symbol declarations and the other after them) make sure that the compiler doesn’t disturb the symbol names (C++ name mangling) so that the code is visible to standard C.

The #define CALL_CONV is only a common practice, so that if some time later I want to change the calling convention from __stdcall to something else (say __cdecl), I have to change it in only one place.

The #ifdef _COMPILING_4B904A07_FC20_4589_825A_66604BF297F5 is a bit tricky, actually you have to put __declspec(dllexport) before all the symbol names that you have to expose from your DLL, However, that’s only true when you are compiling the DLL. when you give this header file to someone who has to use the DLL (in C++ code of course, C# doesn’t use Headers, does it? :) ), he/she has to put __declspec(dllimport) before all of the symbols. Why, you may ask, well actually it would still work if you simply remove the __declspec(dllexport) from all the declarations, but putting __declspec(dllimport) would increase the efficiency of the linker (which doesn’t do any harm) because linker knows where these symbols would be located. Here’s the trick, in the .CPP file, I define the symbol _COMPILING_4B904A07_FC20_4589_825A_66604BF297F5 (as you can see in the listing below) before including the header, so when I am actually compiling the DLL, the compiler sees this symbol as defined and defines DLL_API as __declspec(dllexport) hence putting it in front of all the symbols that are to be exposed (which is precisely what I want). When the same Header file is included by an application that uses the DLL, the symbol _COMPILING_4B904A07_FC20_4589_825A_66604BF297F5 is undefined, so compiler uses __declspec(dllimport) as the definition of DLL_API, which is neat. Hence, I don’t need to use two separated Header files for the DLL.

1.2 The Source File:
The Source File is somewhat like this:

// MyDll.cpp
#define _COMPILING_4B904A07_FC20_4589_825A_66604BF297F5

#include "MyDll.h"

extern “C” DLL_API int CALL_CONV sum(int x, int y)
{
return (x + y);
}

Build the project (Ctrl + Shift + B), and It’ll create a MyDll.lib (to be included in the C++ application that uses the DLL) and MyDll.dll (the DLL itself).

Explanation:

The .cpp code is straight forward, I have define _COMPILING_4B904A07_FC20_4589_825A_66604BF297F5 before including the header file, so that DLL_API could be properly defined. Rest of the code is not a problem.

2. Using the DLL in C#

using a DLL in C# is a breeze. Create a new C# Console Project and name it “TestMyDll”, add using System.Runtime.InteropServices; to the using Statements, and declare any DLL methods with DllImport Attribute. Here’s the code:

// Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TestMyDll
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Sum of 5 + 6 is: “ + DllSum(5, 6));
}

[DllImport(“MyDll.dll”, EntryPoint=“sum”)]
public static extern int DllSum(int x, int y);
}
}

Explanation:

The code is straight forward except for the function DllSum(), the DllImport Attribute specifies the DLL in which the following symbol resides, Normally one would only use DllImport(”DllName.dll”), EntryPoint is only required if one uses a different name for the symbol (like I am using DllSum instead of sum, EntryPoint indicates where the entry point for the symbol is.).

Copy MyDll.dll to the Debug folder of the project, Build and Execute the Program (Ctrl + F5), its output will be:

Sum of 5 + 6 is: 11

NOTES:

1. If you download the attached demo project, you’ll find that the DLL file is named MyDllD.dll, Actually the debug and release versions of a DLL are incompatible and it is a common conventions to use different names (appending a ‘D’ to the Debug version DLL) to avoid confusion. Simply Set the Output File Name property of the linker (in project properties) to $(OutDir)\$(ProjectName)D.dll and it’ll create a debug DLL that has ‘D’ appended to its name.
2. The conventions in the DLL header have been directly copied from Joseph M. Newcomer’s The Ultimate (DLL) Header, http://www.codeproject.com/dll/ultimate_dll_header.asp
3. See detailed information on Platform Invoke at: http://www.codeproject.com/dll/ultimate_dll_header.asp

written by ishaq

Aug 04

I have always been interested in things like game development and emulation (although, I keep trying web apps to change the taste, but my long term goal is to be a game developer =) , with my own company ofcourse).

Recently I thought to get practical taste of emulation, I searched on the internet, read some docs, and I got a feel that I sould go for a NES emulator (It’s easy, and a wealth of documentation is available).

Next decision was to make it in Java or C++, I choose C++ (though I still think Java would have been a better option).

So I have done some coding, starting with the 2A03 (NES’s CPU, modified form of 6502) emulation, will post about it when I am done.

written by ishaq