Ok. I don't understand how do you create such code, this is some magic..
SelectLayerByIndex is missing. Can you tell me how to do that?
And also, how I can get Layer's name (utf16) and how much layers is in document?
In the SDK take a look at
X:\.....\AdobeCSY_SDK\samplecode\automation\listner\
X:\.....\AdobeCSY_SDK\samplecode\automation\getter\
Compile the plugins in debug mode and place them in photoshop plugin folder.
Listner creates C++ function for every action that you do in Photoshop (select layer, save file, copy, paste, make new doc etc.)
C++ functions are in listner.log That is text file and ... Listner is magician 
His partner is getter you call it ftom meny in photoshop I think file>automate>getter
Take a look at getter.log
Modify the code to suite you.
Regards,
Momir
PS
SPErr AMAutoPlugin::SelectLayerByIndex( uint32 value)
{
SPErr error = kSPNoError;
DescriptorClassID desiredClass=classLayer;
error = PIUSelectByIndex(desiredClass, value);
if (error) goto returnError;
returnError:
return error;
}
Modified minorely from listner.log
By the way if you debug listner it self you will se underlying mechanisms
of getting subscibed to events, how thw events are handled etc.
Debuging getter is priceles since you will se that there are many ways to get info from PS.