2009년 1월 10일 토요일

.NET CF Version Check 하기와 P/Invoke 예제

To check the version of the installed .NET framework on the device.
\Windows\cgacutil

P/Invoke 사용하여 System API접근하기
.NET library에는 Platform 정보를 읽어올 수가 없다. 2.0에서
P/Invoke를 써야 한다.
[DllImport("coredll.dll")]
private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, StringBuilder pvParam, uint fWinIni);

StringBuilder strbuild = new StringBuilder(200);
SystemParametersInfo(SPI_GETPLATFORMTYPE, 200, strbuild, 0);

* Kernel IO Control을 사용하여 device의 lowlevel을 access해야 하는 경우는 다음과 같이 P/Invoke를 사용한다.
[DllImport("coredll.dll", SetLastError = true)]
private static extern bool KernelIoControl(Int32 dwIoControlCode,
IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf,

KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, outbuff, nBuffSize, ref dwOutBytes);
int error = Marshal.GetLastWin32Error();

LPVOID의 경우 return값을 받으려면 byte[] array를 사용하고 값을
넣을때는 BitConverter.GetBytes(value).CopyTo(array, index) 를
읽어온 데이터는 BitConverter.ToInt32(array, index) 를 사용한다. (Int32대신 각각에 해당하는 type을 넣는다)

* System 시간을 읽어오려면 GetSystemTime을 P/Invoke해야 한다
[DllImport("coredll.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]
private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]
private extern static void GetLocalTime(ref SYSTEMTIME lpLocalTime);

private struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}

* Memory 정보 읽어오기
public struct MEMORYSTATUS
{
public UInt32 dwLength;
public UInt32 dwMemoryLoad;
public UInt32 dwTotalPhys;
public UInt32 dwAvailPhys;
public UInt32 dwTotalPageFile;
public UInt32 dwAvailPageFile;
public UInt32 dwTotalVirtual;
public UInt32 dwAvailVirtual;
}

[DllImport("CoreDll.dll")]
private static extern void GlobalMemoryStatus(ref MEMORYSTATUS lpBuffer);

[DllImport("CoreDll.dll")]
private static extern int GetSystemMemoryDivision
(
ref UInt32 lpdwStorePages,
ref UInt32 lpdwRamPages,
ref UInt32 lpdwPageSize
);

Getting the current directory
Windows Mobile 디바이스에는 current directory라는 개념이 없다.
따라서, path를 넘겨줘야 할 때는 반드시 fullpath를 만들어 주어야 하는데, 현재 실행되는 application의 폴더를 현재 폴더로 얻어오는 것이 일반적이다.
이를 얻어오는 방법은 Reflection을 사용한다.

String strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

여기서 Path는 string을 인자로 받아 확장자, 파일 이름 등만을 파싱하는데 유용하게 쓰이는 helper class이다.

댓글 없음:

댓글 쓰기