O
Ole Christensen
I'm trying to make a sort of conditional compilation in my C# code because
my app is intended to run on both a Pocket PC and on a normal desktop PC. My
code uses a call to an API function that on the PPC is located in
coredll.dll and on the desktop in kernel32.dll. I could make two versions of
the DllImported funktions but then I would have to ask for the OS version in
every call to the function. This however causes the code to look messy and
it does also make the code a lot slower in case of intensive calls to the
funktion like e.g. reading single bytes from a serial port running at a
speed of 115k. A code like this would be perfect:
#if (System.Environment.OSVersion.Platform == PlatformID.WinCE)
[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError=true)]
#else
[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError=true)]
#endif
private static extern bool ReadFile(......
But it is unfortunately not possible because the
"System.Environment.OSVersion.Platform == PlatformID.WinCE" is a not legal
argument to #if in C#. Another similar solution could be:
if (System.Environment.OSVersion.Platform == PlatformID.WinCE)
{
[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError=true)]
}
else
{
[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError=true)]
}
private static extern bool ReadFile(......
But this is unfortunately not legal either, because C# doesn't accept that
the dllImport statement is located in a funktion like e.g. form_load.
Does anyone have any idea to do this in a clever way?
Thanks a lot!
Ole
my app is intended to run on both a Pocket PC and on a normal desktop PC. My
code uses a call to an API function that on the PPC is located in
coredll.dll and on the desktop in kernel32.dll. I could make two versions of
the DllImported funktions but then I would have to ask for the OS version in
every call to the function. This however causes the code to look messy and
it does also make the code a lot slower in case of intensive calls to the
funktion like e.g. reading single bytes from a serial port running at a
speed of 115k. A code like this would be perfect:
#if (System.Environment.OSVersion.Platform == PlatformID.WinCE)
[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError=true)]
#else
[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError=true)]
#endif
private static extern bool ReadFile(......
But it is unfortunately not possible because the
"System.Environment.OSVersion.Platform == PlatformID.WinCE" is a not legal
argument to #if in C#. Another similar solution could be:
if (System.Environment.OSVersion.Platform == PlatformID.WinCE)
{
[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError=true)]
}
else
{
[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError=true)]
}
private static extern bool ReadFile(......
But this is unfortunately not legal either, because C# doesn't accept that
the dllImport statement is located in a funktion like e.g. form_load.
Does anyone have any idea to do this in a clever way?
Thanks a lot!
Ole