Friday, October 17, 2008

No Help inside VC IDE

I recently implemented the Help feature for an application. But, pressing F1 key or clicking on the corresponding button did not work out at all. No help window popped up. After much googling, I fell on this. This does not work when the application is run from inside the VC IDE. So, I went to the Debug directory and launched the application from there. Voila, help worked. But, still when pressing F1 key, I was getting an error message stating the the HLP file was missing. So, in order to disable that, the ON_WM_HELPINFO needed to be handled. Once that is done, the unwanted error message stops showing its ugly face.

Thursday, September 25, 2008

Adding accelerator keys in VC

A microsoft support link shows in a pretty straightforward way how to implement an accelrator key. Have a go

Getting the hyper link to work in CRichEditCtrl

Trying to get a mouse click to work in RichEdit and not succeeding? I faced a similar issue and ended up with this flow

Set the SetEventMask for your m_MyRichEdit (this is my control variable for the CRichEditCtrl class) to understand links
m_MyRichEdit.SetEventMask( \
m_MyRichEdit.GetEventMask() | ENM_MOUSEEVENTS)
Now, add a override function for OnNotify in your class. In the .h file, add
afx_msg BOOL OnNotify (WPARAM wParam, \
LPARAM lParam, LRESULT *pResult);
Add the corresponding function similar to the one below
BOOL CParentDlg:: OnNotify (WPARAM wParam, LPARAM lParam, LRESULT * pResult) 
{

if (LOWORD (wParam) == IDC_RICHEDIT)
{
MSGFILTER * mf = (MSGFILTER *) lParam;
switch (mf-> msg)
{
case WM_LBUTTONUP:
ENLINK *p_EnLink;
p_EnLink = (ENLINK *)lParam;
m_MyRichEdit.SetSel (p_EnLink->chrg);
CString szLinkString = m_RichEdit.GetSelText ();
break;
)
)

return CDialog:: OnNotify (wParam, lParam, pResult);
)
Now, your szLinkString will have the data you need to manipulate. You can use this with the ShellExecute function to popup whatever you want.

On, by the way, you need to add RTF content if you intend to make that identifiable. It goes something like
CString szT = "{\\rtf1 \\par http " + szAppendString + " \\par}";
My above code will popup a http link using which I Open it in ShellExecute

Tuesday, September 02, 2008

Auto Complete in VC++ 6

When the "Auto Complete" (Complete Word) feature of VC (6 in my case) stops working, all you need to do is
1. close the project
2. Go to the project directory
3. Remove the .ncb file in the directory
4. Re-open the project

Voila, its working !!!
(atleast it started working for me).

Ensoy

Wednesday, August 27, 2008

CTreeCtrl and Checkboxes

While working with CTreeCtrl with checkboxes, I came across these 2 issues

1. You would have set TVS_CHECKBOXES in the .rc file. Then during InitDialog, if
m_myTreeVar.SetCheck( hTreeWnd, TRUE)

is called, it will not work as expected. But, when the same line is called anywhere else later, it will work. To workaround this issue, the following two lines need to be added to simulate a removal/addition of the checkboxes flag at the beginning of InitDialog

m_myTree.ModifyStyle( TVS_CHECKBOXES, 0 );
m_myTree.ModifyStyle( 0, TVS_CHECKBOXES );

2. With the CTreeCtrl, when trying to get the handle of a "clicked" item using hittest, I fell on an issue that it was returning the handle of the NEXT item on the list and not the one which i clicked. The code is as below
 DWORD pos = GetMessagePos();
CPoint pt(LOWORD(pos), HIWORD(pos));
ScreenToClient(&pt);
UINT uFlags = 0;
HTREEITEM hItem = m_MainTree.HitTest(pt, &uFlags);


The above code returned the handle of the next list item from the one clicked. To fix the same, I just modified the "ScreenToClient" as below
m_myTree.ScreenToClient(&pt); 

and Voila!!! it worked!

Wednesday, May 28, 2008

Writing VB.Net app with c or C++ Dll

Added for my reconnaissance.

Step 1. Create a VC project (I used VC++ 6). An empty win32 DLL. The main things are below

Step 1a: For all the functions that needs to be exported, add "_stdcall" before their declaration and definition. For ex, I added
long _stdcall MyExportedFunction(int myinteger)

One VERY IMPORTANT thing to note. The return has to be something other than void as this causes an incompatibility between the DLL and the VB and the application will end up crashing.

Step 1b: Make sure to have a .def added to the VC project. The contents of my def file are


 MyVCLibrary.def : Declares the module parameters for the DLL.
LIBRARY "MyVCLibrary"
DESCRIPTION 'My VC Library DLL'

EXPORTS
; Explicit exports can go here
MyExportedFunction
My exported function prototype is
long _stdcall MyExportedFunction(int);

Now, thats it for the DLL. Compile and get it ready.

Step 2a: Create the VB.NET (I used Visual Studio 2008). In the Form Class, create a declaration as below (in a single line)


Declare Function MyExportedFunction Lib 
"MyVCLibrary.dll" (ByVal pcFilePath As Integer
) As Long

Step 2b: Call the function as below
Call MyExportedFunction(10)
. That's about pretty much to it.

Wednesday, April 09, 2008

Some Linux Tips

This is mainly from my experience in Red hat Linux 9.0 and Fedora Core (from as far back as 5 years). I have used the double quotes just to highlight the phrase to be typed on the command line. Remove the double quotes and type the commands.

How to check free space in my hard disk?

Typing "free" in your terminal command line lists out all the hard drives details.



"How do I find out the location of an executable file?

which <executable>" will show you the location from which it is called.



How can shutdown my machine with a command/shell script?

Create a shell script and put the following line in it (without the double quotes)

"shutdown -h now"

Executing this shell script shuts down your machine.



How do I mount a network shared folder in my LAN ?

For example sake, assume the network machine name is "nwsys" and the shared folder in that is "shared". Now create a directory to which you want to associate the shared folder to. Lets assume you create a folder "myshare" under "root" directory. Now the command will be

"smbmount //nwsys/shared /root/myshare -o username=<username>,workgroup=<workgroup>"

If all directories exist, you would be asked for the password, enter it. If no error message pops up, you are through.

From FC5, smbfs is removed and the command needs to modified to this:

mount -t cifs //nwsys/shared /root/myshare -o 
username=<myname>,workgroup=<myworkgroup>



I  had disconnected my LAN cable and reconnected it. Now, I am not able to connect to network machines through nautilius.

Close all nautilius windows and execute the following command at the command prompt

"killall nautilius"

This restarts the nautilius service and you should be able to connect to network systems.



 I created a shell script in windows and moved into linux. Here, i set all the requisite user rights for execution. When i tried to execute the script, it gives the error

bad interpreter: No such file or directory
The script seems to be syntatically fine.

This is a problem becaus of the difference in OSes. Open the script in vim and type the following command


:set ff=unix

This converts the script to unix based file format.

Tuesday, January 01, 2008

Difference between CR and LF

Sourced from here

The main difference is that and are characters, while EOF is more of a concept - though the end of a file can be denoted by a special marker character, too.

Generally speaking, under Windows and DOS, lines of text in a file end with the two byte character sequence ; these characters have the ASCII values 13 and 10, respectively. Under *nix, a line of text ends with just ASCII character # 10.

Under DOS and Windows, a file might also contain an end of file marker character; the ASCII value of this character is 0x1A (26 decimal). This character was used more to speed things up when working with certain kinds of files (text files, as opposed to binary files) than anything.

Exactly how you open a file, read from it, or write to it, will vary with the programming language or tool that you're using; there are a lot of commonalities between the majority of them, though. Hopefully, a few examples in C will help clear this up. A few important points first, though: a carriage return (CR) - ASCII character code 13 - is also represented by the escape sequence "\r" (not including the quote marks), and a line feed (LF) - ASCII character code 10 - is also represented by the escape sequence "\n" (again, not including the quote marks).

Under DOS or Windows - assuming that we're using the C run-time library (RTL) and not the Windows application programming interface (API), or DOS function calls - you have to tell the RTL how you would like it to treat the file. Your option are "as a text file" and "as a binary file". If you tell the RTL to treat a file as a text file, the following things will happen:

When the sequence is encountered in a file, it's replaced, "behind the scenes", by just , which is ASCII character 10. Which matches the constant '\n'.

If the end-of-file character is encountered, the file's end-of-file flag is set: fgetc() returns the value EOF (defined in stdio.h); feof() will return true (non-zero), and so on.

However, if you tell the RTL to treat the file as a binary file, the data is returned to you "raw" - no end-of-line marker translation is done, you get both the and the ; and, the end-of-file marker - if present - is treated just like any other byte in the file; it's value is returned to you.

With most compilers, under Windows or DOS, in C, a file is opened as a binary file by using fopen() and specifying the letter "b" after the mode you wish to open the file in (r, w, a, etc.). For example, to open a file named "test.dat" in read-only mode, in binary mode (without any translation of the file's contents), one could write:

FILE *f;

if (!(f = fopen(f,"rb")))
{
/* an error occurred */
}

A note regarding porting code between DOS/Windows and *nix systems: if you were to attempt to compile the above code on a *nix system, the compiler might or might not give you an indication that there was a problem: generally speaking, *nix systems always open files in "binary" mode, and "rb" (or "wb", "ab", etc.) is not a valid parameter. Exactly what would happen can be expected to vary from compiler to compiler; on some, the presence of the "b" might cause the fopen() call to fail; others might print a warning message.



One last note regarding end-of-file and translation of characters in "text" mode on DOS and Windows systems: while, in theory, the rules are simple, in practice, they're a lot more complicated. If you were to take a program that used text files in "text" mode, and compile it, on a DOS / Windows system, with different compilers from different vendors, you can expect that, to some extent, you'll get different results. One of the ways they differ in is in how the final pair in a file is treated. In "text" mode, sometimes, you'll get a final "\n", and other times you won't. In "binary" mode, you'll always get the entire contents of the file.