To do it in a (semi) generic way, the only way is to send this on the (emulated) serial port of the phone;
AT+CGSN
Some/most phones allow it, some don't. Those that don't sometimes have a manufacturer specific way of getting the IMEI, some don't allow it at all.
In other words, if you need a solution that is 100% generic for every manufacturer, you're probably out of luck.