Tuesday, April 29, 2008

Delete all .svn subfolders using Total Commander

You can easily delete all .svn sub folders using Total Commander in few steps:
  • Search for .svn (ALT + F7)
  • Chouse Feed to listbox (ALT + L)
  • Press * to select all results
  • Press delete to delete all .svn subfolders

Friday, April 25, 2008

Set value to unbound column

To set value in unbound column cells you have to handle CellFormating event. Then, check DataGridViewCellFormattingEventArgs and set value. For example:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == 2)
{
e.Value = "Test";
}
}

Tuesday, April 22, 2008

Compiling .NET to Machine Code

Compiling .NET to Machine Code
Normally, when a .NET application is run, the JIT (Just In Time) compiler transforms the .NET intermediate code into machine code just before it gets executed. Because of this, the compiler only has very little time to optimize the code.
However, there's a feature many .NET developers aren't even aware of: ahead-of-time compilation or AOT in short. AOT means compiling an entire .NET assembly to machine code before it is executed and usually takes place either when the an application is installed or continuously in the background while the system is idle. It allows for time-consuming optimizations to be performed on the code that normally only a traditional C or C++ compiler would have time to do.
The advantage of this method over traditional compilation however is that it takes place on the end-user's machine. Microsoft's .NET framework for example will compile .NET 2.0 assemblies to 32 bit code on a 32 bits operating system and to 64 bit code on a 64 bits operating system unless you tell it to do otherwise.
Microsoft .NET Framework
Microsoft's utility for AOT compilation is named ngen.exe, short for native image generator. It can be found in the directory the .NET runtime is installed in, eg. C:\Windows\Microsoft.NET\Framework\v2.0.50727 for the .NET Framework 2.0.
ngen.exe can be invoked from the command line in two different modes:
ImmediateUse ngen install or ngen update to immediately compile the given assembly to machine code. ngen.exe will compile the assembly and exit when it's done.
QueuedUsing this mode, assemblies are placed into a queue and will be compiled either at your measure or by a low-priority process while the system is idle. This mode can be accesses by adding the /queue:{123} argument at the command line.
ngen.exe will not modify the assemblies specified on the command line but instead put the compiled assemblies into a special folder that is checked whenever .NET needs to load an assembly.
(taken from
http://www.nuclex.org/)

Custom WWF Persistence Services

I found this great article about making custom WWF persistence service
http://www.manuelfoerster.net/wordpress/40/

Open new command prompt for cout

If you run your library from environment that don't provide you console window for your messages you can open new one using:
AllocConsole(); freopen("CONOUT$", "w", stdout);

Total commander and Winmerge

In order to use Winmerge as comparision tool add following line to wincmd.ini:

CompareTool="d:\Program Files\WinMerge\WinMerge.exe"

stl: list vs vector

They solve different problems, so it depends on the "shape" of your data, and which is more important to you, inserting, deleting or accessing quickly.Lists are double-linked lists, so insertion at either end, or in the middle (providing you know an existing element in the middle) is equally fast. Also, splicing is fast (eg. putting a list into the middle of another) - all that does is re-arrange the end pointers. All of those operations would be slower with vectors, because you would need to copy everything.eg. Inserting into the middle of a vector of 1000 items would involve moving 500 items up one. Similarly with deleting. In fact, vectors are really only good for insertion and deletion at the end.For things that require insertion and deletion, but only at *each* end (like a queue) then there is a dequeue (double-ended queue) which is really a collection of vectors. This is more efficient.However, vectors shine in random access - since they are organised sequentially in memory, whilst this is slow for insertion at anywhere other than the end, you can easily find, say, element 500, you simply index into the vector. Thus, vectors can be sorted, shuffled, and accessed randomly.When playing with STL - and these other MUD-related ideas - I am asking myself what is the need for each case - random access or fast insertion, and using the container appropriate to the case at hand.For instance, in the event management code I will post shortly, as I remove events from the queue I am keeping a copy of the auto-repeat ones, for later re-insertion. Now the copy is a temporary copy, and I don't need to access it in any special way, so I am using a simple vector. However the event queue itself is a priority queue (which I suspect is implemented as a map of queues).Vectors are designed to auto-grow, with the size - I believe they double in size each time they grow. eg. 2, 4, 8, 16 and so on. So, if you keep adding to them the number of memory allocations is pretty small. However if you know in advance how much they need (even roughly) then you can tell the vector to reserve that amount in advance.
(Thanks to
Nick Gammon. Article taken from http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=2993)

Source code organisation of templates

in the H file:
#ifndef __MY_TEMPLATE_H
#define __MY_TEMPLATE_H


... #include "mytemplate.cpp"

#endif __MY_TEMPLATE_H

and in the CPP file

#ifndef __MY_TEMPLATE_CPP
#define __MY_TEMPLATE_CPP
#include "mytemplate.h"
...
#endif __MY_TEMPLATE_CPP

Detecting memory leaks

If you put {,,msvcrtd.dll}_crtBreakAlloc (or change msvcrtd.dll with proper dll, maybe msvcr80d.dll) in watch window. Press F10 and you should see -1 as value. Change value with memory allocation number and debuger will stop at this memory allocation number!!! Great thing for memory leak hunting!!!

Note, if you have multi thread application you will not have always the same allocation number.

Sourcesafe and "An Error Occurred in the Secure Channel Support" message

I tried to enable VSS on IIS. Installation pass correctly but when I try to connect SourceSafe I was getting this error message “An Error Occurred in the Secure Channel Support”. I tried everything but with no results. And finally I found that the problem was that some process didn’t have permission on Windows\temp folder.

Useful links:
http://alinconstantin.dtdns.net/WebDocs/SCC/VSS_Internet.htm

Useful tools (if you have any problem to enable VSS on Internet:
Filddler:
http://www.fiddler2.com/

Monday, April 21, 2008

Visual studio 2005 toolbox icons screwed up

Right click on toolbox and select "Reset toolbox" from context menu to get icons back

WIA Scanning without user interaction

WiaClass wiaManager = null; // WIA manager COM object
CollectionClass wiaDevs = null; // WIA devices collection COM object
ItemClass wiaRoot = null; // WIA root device COM object
CollectionClass wiaPics = null; // WIA collection COM object
ItemClass wiaItem = null; // WIA image COM object
try {
wiaManager = new WiaClass(); // create COM instance of WIA manager
wiaDevs = wiaManager.Devices as CollectionClass; // call Wia.Devices to get all devices
if( (wiaDevs == null) (wiaDevs.Count == 0) )
{
MessageBox.Show( this, "No WIA devices found!", "WIA", MessageBoxButtons.OK, MessageBoxIcon.Stop );
Application.Exit();
return;
}
object selectUsingUI = System.Reflection.Missing.Value; // = Nothing
wiaRoot = (ItemClass) wiaManager.Create( ref selectUsingUI ); // let user select device
if( wiaRoot == null ) // nothing to do
return;
((Item)wiaRoot.Children[0]).Transfer("C:\\a.tmp", false);
....
Note: Tnx to unknown author of "WIA Scripting and .Net" at codeproject.com

Avoid flickering in C# custom control


Whent it is set BackgroundImage to custom control and Invalidate is call you can see flickering. To avoid that try like this:

publicpartial class MyControl: UserControl
{
public MyControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint ControlStyles.OptimizedDoubleBuffer, true);
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
}

How to dealocete char* str = "something";

char* str = "something";
If you try to delete str you will get exception at _CrtIsValidHeapPointer.

The thing is that you can not dealoce str because you didn't alocated it at all.
And don't wory. You will not get memory leaks. This string is already in memory. When this command is executed str just get pointer to that part of memory.
Compile program, open it (.exe) in some hex editor and search for "something". It's there :))))

Debuging tips & tricks

In visual studio 6.0 in watch window add variable "@err, hr" and you will get useful information when some function fails

Windows XP Embedded check disk

To run check disk on xp embedded you will need chkdsk.exe and autochk.exe in windows\system32 folder.
In command line type: chkdsk c: /F
System will ask to check disk after restart. Confirm it.

With a little luck error will be corrected.

Convert Byte Array 2 Variant Array and reverse

VARIANT ByteArray2VarArray(CByteArray &ba)
{
VARIANT vt;
VariantInit(&vt);
SAFEARRAY *psa;
SAFEARRAYBOUND rgsBounds[1];
rgsBounds[0].lLbound = 0;
rgsBounds[0].cElements = ba.GetSize();
psa = SafeArrayCreate(VT_UI1, 1, rgsBounds);
for (int i = 0; i <>
{
SafeArrayPutElement(psa, (long*)&i, (ba.GetData() + i));
}
vt.vt = VT_ARRAYVT_UI1;

vt.parray = psa;
return vt;

}

bool VarArray2ByteArray(VARIANT var, CByteArray &ba)

{
if ( ! (var.vt & VT_UI1))

{
return false;
}
if ( ! (var.vt & VT_ARRAY))

{
return false;
}
SAFEARRAY* psa;
if (var.vt & VT_BYREF)
{
psa = *(var.pparray);
}
else
{
psa = (var.parray);
}
long lLbound, lUbound;
SafeArrayGetLBound(psa, 1, &lLbound);

SafeArrayGetUBound(psa, 1, &lUbound);
for (long i = lLbound; i <= lUbound; i++)
{
unsigned char b;
if( SafeArrayGetElement(psa, &i, &b) == S_OK)
{
ba.Add(b);
}
}
return true;
}

ATL COM Wizard Error

I have generated empty ATL COM Wizard project in VC++ 6.0, and when I compile it I get this error:
urlmon.idl error MIDL2025 ... expecting a type specification near "IClassFactory".
And what was the problem?
Problem is because Language for non-unicode programs in regional options in control panel was set to language other then English.
When I set it back to English everything was OK.
I hope that I save someone’s time.