Hello!

This content has been updated. You will be momentarily redirected to the current version.

Moved!



Moved! I now blog at http://www.saicharan.in



Wednesday, September 17, 2008

Creating DLLs with MinGW

This morning, I was asked how to use existing C code in C++ code. So I took the route of converting the C code into a DLL and then linking to it from the C++ code. So I had to figure out how to do this using MinGW on Windows. Here is an account of my experiments (distilled ofcourse!):
There are three things you need to do:
1. Convert the existing into a DLL.
2. Include the header of the DLL in the sources where you wish to use the library.
3. Build your code, linking against your DLL.

Here are the steps:
a) For this, you need to add the following code to the header file containing the declarations of the functions that are to be used:

#ifdef __cplusplus
#define cppfudge "C"
#else
#define cppfudge
#endif

#ifdef BUILD_DLL
// the dll exports
#define EXPORT __declspec(dllexport)
#else
// the exe imports
#define EXPORT extern cppfudge __declspec(dllimport)
#endif

b) Front of each function that you want to use from the DLL, prepend the word EXPORT. For example:
EXPORT int hello(void);

c) Create the create the object code using the following command:
>gcc -c -DBUILD_DLL TestDLL.c, where TestDLL.c is the source file.

d) Next, use the object code to create the DLL with the following command:
>gcc -shared -o TestDLL.dll TestDLL.o -Wl,--out-implib,TestDLL.a, Note that TestDLL has to be replaced with whatever is the actual DLL name.
>Creating library file: TestDLL.a (This is the output of the command)

e) Now, compile the actual file (note that this file should include the TestDLL.h header)
>gcc Main.cpp -o Main.exe TestDLL.dll

f) Done! Run the executable:
>Main.exe
Hello World!

>

Here are the files that I have used to test these things. I have tested them on Windows XP using MinGW32. I have used the following link for reference: http://www.emmestech.com/moron_guides/moron2.html

Tuesday, July 29, 2008

Global C++ Exception Handler

Just found out that many C++ compilers provide the std::set_terminate() function for catching any unanticipated exceptions. Read here for more: http://www.extinguishedscholar.com/wpglob/?p=176

Tuesday, June 10, 2008

C/C++ resources online

The following is a continually updated list of my personal favorites for C/C++ reference:

C:
C-FAQs: http://c-faq.com/index.html
Overflows in C: http://www.fefe.de/intof.html

C++:
Stanford Library: http://cslibrary.stanford.edu
C++ Coding Conventions(CERN): http://pst.web.cern.ch/PST/HandBookWorkBook/Handbook/Programming/CodingStandard/c++standard.pdf
C++ Casting: http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/cast.html & http://www.nacs.uci.edu/dcslib/sun/compilers/c-plusplus/c%2B%2B_ug/Cast.new.doc.html

ThinkPad T60 TouchPad Scroll in Office 2007

Found this link on Google. It was reported as on the day of posting as a possibly problematic site. But I went on and found the info in that site very useful. So here is the link:
http://www.lvlolvlo.net/2007/02/11/office-2007-on-a-thinkpad-ultranav-support-for-scrolling-fix/

Wrong Characters being Displayed

Had this problem with my keyboard layout getting changed just too often. The problem was as follows: Off and on, my keyboard was behaving erratically. It would mis-behave and then come back to normal. I suspected a change of keyboard layout - I had this problem earlier on my Linux Box and RI pointed out that my keyboard layout was UK English!

So, I too started off searching google for this, and things were mostly pointing to IME/Languages. When I opened the Regional Language Options in the Control Panel, the language bar popped up. I noticed that the language was set to Finnsh. Once I changed that to English, another icon appeared next to it - the Keyboard icon. It was in Finnish. So I changed it to English and all was well. But then I was wondering how these settings were changing so often. SO I went to the Regional Language Options in the Control Panel. Went to the languages tab and clicked on the Details button. From there, I selected the Finnish Language and Keyboard settings. At the bottom there is a Key Settings button. That was the major culprit. Click that button and you will see that Alt+Shift is assigned to "Switch between input Languages" option. I simply removed that key-binding to avoid un-intended switching between languages.

Also, I use the alt+shift combo often in my work - so that was the probem! Now we know! So change your keyboard settings and get back to efficient mode.

One last advice. If you ever face such a problem, make the language bar visible: Control Panel > Regional Language Options > Details > Language Bar Button > Show Language Bar. Once you see the language bar, you can know at once if your keyboard layout is the problem.

Wednesday, March 26, 2008

VirusRemoval.vbs Windows Script Host

Atlast! Got rid of that damn warning.

msconfig did not show up this thing. No amount of standard registry scans helped me find this. I looked at the standard Run, RunOnce & Shell hives under HKLM/Software/Microsoft/Windows/CurrentVersion/. Just no use. No entries that were tell-tale traces of this VirusRemoval.vbs.

But done at last! Took some patient Google-searching though. Some kind soul had provided the entire code of the VirusRemoval.vbs script file at:
http://www.thinkdigit.com/forum/showthread.php?t=71097. Looking at the code was enough to tell me where the virus had made entries in the registry.


Goto: HKLM/Software/Microsoft/WindowsNT/CurrentVersion/Winlogon. Under that, you will find a key called Userinit. Double click that key. A dialog box will open up with a string parameter. Edit that to remove just the offending entry. Warning: If you are not sure what the offending entry is, DONOT modify the key. Please post back the contents of the key here, and we could work out something!

After cleaning up my registry, the value of Userinit for me is: C:\WINDOWS\system32\userinit.exe

Good Luck!

Tuesday, March 18, 2008

Install RPMs on Ubuntu

Check this link. It describes the use of a tool called 'alien' to install RPMs on Ubuntu linux!

http://theunixgeek.blogspot.com/2008/03/why-i-switched-back-to-ubuntu.html#c2746545558233032385

Friday, February 15, 2008

yum ntfs-3g

To install ntfs support for Fedora linux, use the following as root:

yum install fuse fuse-libs ntfs-3g

reference: http://www.mjmwired.net/resources/mjm-fedora-f7.html#ntfs

Tuesday, February 05, 2008

Adobe Reader Commenting Not Allowed

At last!
Wanted to allow my prof. to be able to comment the pdf files that my LaTeX produces. But, even though I was NOT setting any such restrictions in LaTeX, the Adobe Reader always showed that Commenting was not allowed. After a Google search, I got this post in the Adobe Forums: http://www.adobeforums.com/webx/.3c05f5b1

In short: You cannot comment unless explicitly permitted. For this, you need to get to Adobe Acrobat (not the reader), and set the permission from: Commenting > Enable for Commenting in Adobe Reader.

Then you will be allowed to comment or markup :)

Update: If you don't mind a few additions of Foxit, you might want to try Foxit reader(www.foxitsoftware.com) for commenting your PDF documents without any of the above hassle.

Monday, February 04, 2008

WIndows Script Host: Unable to open file

For quite some time a lot of my friends kept getting a windows script host error whenever they tried to open a drive. It says: Windows Script Error: Unable to find file: "....vbs".

Spending some time on google led me to this site:
http://www.mydigitallife.info/2007/04/19/unable-to-open-hard-or-usb-flash-drive-with-windows-script-host-cannot-find-script-file-autorunvbs-error/

All we need to do is to either delete the autorun.inf file there or modify the content in to get rid of the offending startup file info. To delete the file:

del :\autorun.inf /f /s /q /a

To modify the file, first remove its hidden-file attribute: (see http://forums.techarena.in/showthread.php?t=717194)
attrib -s -h -r :\autorun.inf

Go ahead and edit this file now.

Sunday, February 03, 2008

TeXnicCenter and Adobe Reader 8.0

I recently upgraded my Adobe Reader to version 8. The new interface is cool and all that, but I had an issue with using TeXnicCenter. Whenever I view a compiled document from TeXnicCenter, and then closed the Adobe reader or recompiled the file (causing adobe reader to close the document), the Adobe reader was crashing. A Google search yielded the following link which is very useful. It is almost a text-book type of solution which just works!

http://dikkie.net/2007/02/16/texniccenter-and-adobe-reader-80/

Sunday, January 27, 2008

Why is switch case better than if else if?

Why is switch case better than else if? Or is it?
Here is some code I got into trouble with:
.
.
.
if(dim==0){
...//do lots of things
...return;
}
doSomeCommonThingsHere();
if(dim==3)
...doSomething3();
else if(dim==4)
...doSomething4();
else if(dim==5);
...doSomething5();
doSomeMoreCommonSomethings();
.
.
.
See anything?

I did not, initially. The semi colon after else if(dim==5). How I unearthed the bug? GDB was crashing. So no debugger to help me:( I figured out that only the dim==5 case was working well - and that none of the cases above it were working correctly. So I considered moving one of the earlier case code to the end to see if I can figure out the problem. While moving the code I accidentally noticed the semi-colon. Man! Computers are dumb - or was it me dumb?

Now did say switch case might have been better? Maybe. Because we are taught to put the break; statement at the end of each case to prevent it from falling-through right? What if I forgot to put the break; ? I guess it is easier to "find" a missing break than a missing ;. Did you see the semi-colon after missing?

Now why did I use such a large number of if-else-if statements? I was using "Incremental development". I did not see the other cases coming in hordes. Shortsighted you might say, but I did not even intend to do the other cases in the first place. WOW! How easily changing requirements can screw up such wonderfully working code. Screw the changing requirements. Now you see how difficult is the job of a designer - he has to really think of extensibility - see extensibility in a new light?

Consider for a moment the above code. I was actually telling the computer to do things wrong!!! When will these computers learn to understand (:D) Or will they ever?