Here's sample of how to copy an assembly from the device to the
desktop and get its version.
[C# CODE]
[DllImport("rapi.dll")]
static extern int CeRapiInit();
[DllImport("rapi.dll")]
static extern int CeRapiUninit();
[DllImport("rapi.dll")]
static extern int CeCloseHandle(IntPtr hObject);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
static extern IntPtr CeCreateFile(
string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);
[DllImport("rapi.dll", CharSet = CharSet.Unicode)]
static extern int CeReadFile(
IntPtr hFile,
byte[] lpBuffer,
int nNumberOfbytesToRead,
ref int lpNumberOfbytesRead,
int lpOverlapped);
const int ERROR_SUCCESS = 0;
const uint GENERIC_READ = 0x80000000;
const short OPEN_EXISTING = 3;
const short INVALID_HANDLE_VALUE = -1;
const short FILE_ATTRIBUTE_NORMAL = 0x80;
public static Version GetAssemblyVersionFromDevice(string
remote_file, string local_file)
{
bool rapi = CeRapiInit() == ERROR_SUCCESS;
if (!rapi) {
return null;
}
IntPtr remote_file_ptr = CeCreateFile(
remote_file,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (remote_file_ptr.ToInt32() == INVALID_HANDLE_VALUE) {
return null;
}
FileStream local_file_stream = new FileStream(
local_file,
FileMode.Create,
FileAccess.Write);
int read = 0;
int size = 1024 * 4;
byte[] data = new byte[size];
CeReadFile(remote_file_ptr, data, size, ref read, 0);
while (read > 0) {
local_file_stream.Write(data, 0, read);
if (CeReadFile(remote_file_ptr, data, size, ref read, 0) == 0)
{
CeCloseHandle(remote_file_ptr);
local_file_stream.Close();
return null;
}
}
CeCloseHandle(remote_file_ptr);
local_file_stream.Flush();
local_file_stream.Close();
if (!File.Exists(local_file)) {
return null;
}
if (rapi) {
CeRapiUninit();
}
Version version =
Assembly.LoadFrom(local_file).GetName().Version;
return version;
}
Regards,
Christian Resma Helle
http://christian-helle.blogspot.com