Posts

TLS (Thread Local Storage) sample code

This will spawn multiple threads and each thread will access itTLS, including the main thread itself will access its own TLS. The code will not deallocate the memory in the heap which initially allocated in the thread. The reason is to able to show the content of the TLS and each TLS is localized to each thread. #include <stdio.h> #include <Windows.h> #include <process.h> static DWORD dwTlsIndex; // address of shared memory void func(LPVOID); int main(int argc, char* argv[]) { if ((dwTlsIndex = TlsAlloc()) == 0xFFFFFFFF) return FALSE; //main thread LPSTR lpszTemp = new char[10]; strcpy(lpszTemp, "main thread"); TlsSetValue(dwTlsIndex, (LPVOID)lpszTemp); for(int k = 0; k < 10; k++) { _beginthread(func, 0, NULL); } LPCTSTR pCheck = (LPCTSTR)TlsGetValue(dwTlsIndex); printf("Main: %s\n", pCheck); TlsFree(dwTlsIndex); return 0; } void func(LPVOID) { ...

My frequently used Visual Studio.NET 2003 IDE Commands

I do not know about others but these are commands that I would definitely want it to be customized for my preference. Edit.ToggleTaskListShortCut Edit.GotoNextLocation Edit.GotoPreviousLocation Edit.ToggleBookMark Edit.NextBookMark Edit.PreviousBookMark

Windows Script to search for a file in all the drives

Script below will search for a file in all drives(excluding network mapped drive) 'filename to search for filename="test.txt" On Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk",,48) For Each objItem in colItems Set fs = CreateObject("Scripting.FileSystemObject") Set folder = fs.GetFolder(objItem.Caption) fList(folder) Next sub fList(folderObj) for each file in folderObj.Files if file.name=filename then Wscript.Echo "File found at" & file.Path exit for end if next for each folder in folderObj.subfolders fList(folder) next end sub

Using DrWatson log to debug

- If lately application crash. DrWatson will record it and place the info to a log file.(newer version Windows should have it). Type drwtsn32 or drwatson - Then, open the log. Look for Fault-> - Start vadump and type vadump -v -p 1160 |more -p = is the process ID. (get it from task manager) in the crashed PC to get the memory address of the environment. - If confirm involved module are related to your application. - Get the pdb file (debug info) file of the related module. (even for release mode module, you can rebuild it and checked the generate debug info. It will still be able to load some additional info without the expense of address changes a lot cause the compiled release module with debug info size is identical to one without) - Debug the process using IDE - Attach to process. - The DrWatson should contain offset from the loaded module to the crash location. Makesure IDE set to read the debug info to ease in reading the crash location in response to real source. If the IDE i...

Outlook - IMAP Sent Items folder not keeping the sent items

If you are familiar with Outlook Express, you will notice that everytime if you are using the IMAP account to send out the email, the sent emails will automatically be copied one copy into "Sent Items" IMAP folder. But, if you switch to Outlook, regardless of Outlook 2000, 2002, 2003 and etc. The sent email will never be copied into the IMAP sent items folder but instead only into the local sent items folder. Anyway, there is a way to overcome this problem (courtesy of Lee Ta Nen). By using the Outlook rules to imitate the effect. Steps 1. Goto menu, Tools - Rules and Alerts 2. Click "New Rules ..." button 3. In the Rules Wizard - choose "Start from a blank rule" 4. In "Select when message should be checked", select "Check messages after sending" 5. Click "Next" button 6. Then, in the next page, choose "uses the form name from" and tick it. 7. Then, in the "Edit the rule description...

.NET GDI+ Simple Image Operations

(In C#) System.Drawing.Graphics g; bmpOriginal = new Bitmap("C:\\myImage.gif"); int nNewWidth = 2 * bmpOriginal .Width; //suppose resize 200% int nNewHeight = 2 * bmpOriginal .Height; System.Drawing.Bitmap bmpModified = new Bitmap(nNewWidth, nNewHeight); g = Graphics.FromImage( bmpModified ); RectangleF rectDest =new RectangleF( 0, 0, nNewWidth, nNewHeight); RectangleF rectSource = new RectangleF(0, 0, bmpOriginal .Width, bmpOriginal .Height); g.DrawImage( bmpOriginal , rectDest, rectSource , GraphicsUnit.Pixel); bmpModified .Save("C:\\myNewImage.gif", System.Drawing.Imaging.ImageFormat.Gif); g.Dispose(); g = null;

Reflection API to access COM objects sample

quoted from URL Object o; System.Type t = Type.GetTypeFromProgID("MODI.Document"); o = System.Activator.CreateInstance(t); Object[] a = new Object[1]; a[0] = "C:\\Temp\\iri36300.TIF"; t.InvokeMember("Create", BindingFlags.InvokeMethod, null, o, a); Object images = t.InvokeMember( "Images", BindingFlags.GetProperty, null, o, new Object[] {} ); Type imagesType = images.GetType(); Object item = imagesType.InvokeMember( "Item", BindingFlags.GetProperty, null, images, new Object[] {"0"} ); Type itemType = item.GetType(); itemType.InvokeMember("OCR", BindingFlags.InvokeMethod, null, item, new Object[] { 9 } ); Object layout = itemType.InvokeMember("Layout", BindingFlags.GetProperty, null, item, new Object[] {} ); Type layoutType = layout.GetType(); Object ocrResult = layoutType.InvokeMember( "Text", BindingFlags.GetProperty, null, layout, ...

Locating MAPI directory using FGetComponentPath

Codes below quoted from URL By Sam Charchian typedef BOOL (STDAPICALLTYPE FGETCOMPONENTPATH) (LPSTR szComponent, LPSTR szQualifier, LPSTR szDllPath, DWORD cchBufferSize, BOOL fInstall); typedef FGETCOMPONENTPATH FAR * LPFGETCOMPONENTPATH; static TCHAR s_szMSIApplicationLCID[] = "Microsoft\\Office\\9.0\\Outlook\0LastUILanguage\0"; // STRING_OK static TCHAR s_szMSIOfficeLCID[] = "Microsoft\\Office\\9.0\\Common\\LanguageResources\0UILanguage\0InstallLangu age\0"; // STRING_OK //////////////////////////////////////////////////////////////////////////// /// // Function name : CSVCFile::InitMAPIDir // Description : For Outlook 2000 compliance. This will get the correct path to the // : MAPISVC.INF file. // Return type : void // Argument : LPSTR szMAPIDir - Buffer to hold the path to the MAPISVC file. void CSVCFile::InitMAPIDir(LPTSTR szMAPIDir) { ...

Troubleshooting Outlook Express (IMAP, POP3, HTTP, NEWS) with the log file

Just happen to know this when trying to solve my friend OE problem. You can log in POP3, IMAP, NEWS, HTTP commands of your Outlook Express into a log file. (note, the password in the log file is encrypted by Microsoft, therefore, it is safe to ask user to send the log file over if you are a IT support) To enable logging. ------------------------- Tools(menu) - Options (menu) - Maintenance(menu) - check on the protocol you need. (POP3, IMAP, NEWS or HTTP) To view the log file -------------------------- Goto path C:\Documents and Settings\YourUserName\Local Settings\Application Data\Identities\{8D32DF8B-D3B8-4783-A0C5-FE37E2FC8659}\Microsoft\Outlook Express\pop3.log (for pop3) imap.log for (imap) and other respectively. P.S. The {8D32DF8B-D3B8-4783-A0C5-FE37E2FC8659} may vary and there could be a few Identities in it. Look for the folder with the structure of *.dbx similar to your Outlook Express email account.

Steps to set up VS.NET 2003 remote debugger

(quoted from http://www.codecomments.com/archive370-2005-2-396392.html) msvcmon ( Visual C++ .NET 2003 ) on Windows 9x/NT 4.0/2k/XP. Steps to set up the remote debugger 1. copy msvcmon.exe and all the dll it needs to the target machine ( Windows NT4.0 SP6). Psapi.dll need to be copied over Winnt\System32. 2. on the target machine, open a DOS command windows c:\app\msvcmon -anyuser 3. start the app ( of course the debug version ) on the target machine 4. on the development machine ( Windows XP SP2 ) , open the project file, go to Debug -> Processes select "TCP/IP(native only)" as the transport layer, and type the IP address of the target machine. Then click "refresh" button. (use PSList or PSKill for remotely terminate process. (at sysinternals.com)) URL for explanation on its usage.

Outlook : "operation failed, object could not be found. " when trying to close a PST.

Image
When you try to close a PST. The error message "operation failed, object could not be found" pop up. After you have tried scanpst and detect and repair. Still won't work. Solution. (a hard way) Start Regedit Go to Key HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Microsoft Outlook Internet Settings Where "Microsoft Outlook Internet Settings" is the Outlook profile name that has problem. (note: Outlook support multiple profile even for same Windows login) In the HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Microsoft Outlook Internet Settings, you will see a bunch of long string sub keys in it Like in the format of GUID, like e.g. 75d1fa940a0d3a4497130516edbde90e Then, start at the very first sub keys of HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\Microsoft Outlook Internet Settings. Right click on sub key, Select Find (menu) And then, ...

error LNK2001: unresolved external symbol _IID_IMessage

Often time, I encounter unresolved external symbol _IID_IMessage problem when I am trying to use IID_IMessage in my codes. This is my solution to the problem, include and define in such sequence. #define INITGUID #define USES_IID_IMessage #define USES_IID_IMAPIPropData #define USES_IID_IMAPITable #include <initguid.h> //this is needed, #include <mapiguid.h> //then this #include <mapiform.h> #include <objbase.h> #include <mapix.h> #include <mapitags.h> #include <mapidefs.h> #include <mapiutil.h> #include <imessage.h>

Managed string to Unmanaged string

Taken from http://www.cs.virginia.edu/~cs216/labs/helpdocs/system.string.html Example Code #include "stdafx.h" #include #include using namespace std; #using using namespace System; using namespace System::Text; using namespace System::Runtime::InteropServices; //Converts a System::String to a std::string //This code assumes that you have used the following namespace: // using namespace System::Runtime::InteropServices; std::string ManagedToSTL(System::String *managed) { //get a pointer to an array of ANSI chars char *chars = (char*) Marshal::StringToHGlobalAnsi(managed).ToPointer(); //assign the array to an STL string std::string stl = chars; //free the memory used by the array //since the array is not managed, it will not be claimed by the garbage collector Marshal::FreeHGlobal(chars); return stl; } //Converts a std::string to a System::String //This code assumes that ...

IT conversation

http://www.itconversations.com/ Listener-supported audio programs, interviews and important events.

Viewing STL container in VS.NET 2003 watch windows

For a vector, you can start with these examples: "v._Myfirst" -> the first element "v._Myfirst, 10" -> 10 elements "v._Myfirst[1]" -> the element at index 1 "v.size()" -> number of elements For a list: "l._Mysize" -> number of elements "l._Myhead->_Next->_Myval" -> the first element "l._Myhead->_Next->_Next->_Myval" -> the second element "l._Myhead->_Next->_Next->_Next->_Myval" -> the third element, etc.

MFC Windows Message Processing - yield control

(Refering Programming Ms Visual C++ fifth edition - Ms Press) A workaround which works with both Win16 and Win32. Yield control once in a while by inserting the following instructions inside the time consuming function: MSG message; if (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) { ::TranslateMessage(&message); ::DispatchMessage(&message); } The PeekMessage function works like GetMessage , except that it returns immediately even if no message has arrived for your program. In that case, the function keeps on processing. If there is a message, however, the function pauses, the handler is called, and the function starts up again after the handler exits. E.g. void CMyClass::LengthyFunction() { MSG message; volatile int nTemp; for (m_nCount = 0; m_nCount for (nTemp = 0; nTemp // processing here. } if (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) { ::TranslateMessage(&message); ::D...

Recover HDD after format

SystemRescueCd is an improvement of the gentoo live cd. It aims to provide an high quality bootable CD-Rom, with all system utilities that can be required to repair your system. http://sourceforge.net/projects/systemrescuecd There seems to be some utilities for FAT16/32 ( dosfstools [ ^ ]) and NTSF ( NTSFProgs [ ^ ]) included on the bootable linux CD There is also this tool R-Studio 2.0 [ ^ ] http://www.grc.com/sr/spinrite.htm [ ^ ]

Merging menu items for MDI container and MDI child forms

Say, if MDI container has a "About" Menu that the MDI child forms need to merge with. Set the MDI container menu in such manner. (In the InitializeComponent or change it using the IDE property window) this.menuItemAbout.Index = 1; this.menuItemAbout.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItemMainHelp}); this.menuItemAbout.MergeOrder = 1; this.menuItemAbout.MergeType = System.Windows.Forms.MenuMerge.MergeItems; this.menuItemAbout.Text = "Help"; this.menuItemMainHelp.Index = 0; this.menuItemMainHelp.MergeOrder = 1000; //this need to be larger than the child so that it appear //underneath the child menu items. this.menuItemMainHelp.Text = ...

Integrate help to .NET apps

Help.ShowPopup(this, "Enter Phone number here. Valid input characters are 0-9,space,'-','(', and ')'", Cursor.Position); //this will show an unconventional popup message box on the cursor position. Nicer than standard //messagebox Can also use the HelpProvider object HelpString properties to achieve the same effect but seems to have less control over. when drag a System.Windows.Forms.HelpProvider to a form. All the controls in the form will have the option HelpNavigator on myHelpProvider avail in the properties windows. For directing the F1 key press to index of the CHM. The helpprovider need to set HelpNamespace to "Help\VB.chm" (to chm appropriate). Then, in the needed control, HelpNavigator on myHelpProvider option, choose keyword index, and then key in the helpkeyword. This will automatically map the F1 key press or the "?" help button button to be directed to the CHM.

To sort using ArrayList.Sort()

Quoted from http://www.codeproject.com/useritems/sortingarraylist.asp public struct MyStrcuture { public Int32 iID; public String sName; } In order to be able to sort elements contained in an ArrayList , we have to define the CompareTo method of the IComparable interface, and then use the ArrayList's .Sort() method. This -of course- means that our structure should be inherited from that interface .. and so we'll do! public struct MyStrcuture : IComparable { public Int32 iID; public String sName; public Int32 CompareTo(Object obj) { MyStrcuture tmpObj = (MyStrcuture) obj; return ( this .sName.CompareTo(tmpObj.sName)); } } This would allow the array list to be sorted by just calling MyArrayList.Sort() provided the arraylist containing the item with the above structure. Useful sorting algorithms in C#. URL