DLLImport dynamically

F

Frank Luker

Hello,

I want to build a stopwatch class (high resolution timer) that runs with
compact framework 2.0 and also with a windows application.

I found a suitable class
(http://weblogs.asp.net/pwelter34/archive/2005/07/01/416999.aspx) but the
problem is that it imports "kernel32.dll" wich runs perfect in a windows
application but not in a windows mobile application. When I change the
DLLImport to "coredll.dll" then it runs perfectly with compact framework but
not with a windows application.

Is there a way of using the DLLImport dynamically at runtime depending on
the current platform?

Thanks in advance!
Frank
 
P

Peter Foot [MVP]

You'll need to define 2 sets of PInvoke declarations - one for each platform
e.g. MethodNameCe and MethodNameXp. Then in your stopwatch class check the
platform (e.g. if(System.Environment.OSVersion.Platform ==
PlatformID.WinCE) in your constructor and store in a boolean member) and
call the correct PInvoke in each method depending on the platform.

Peter
 
F

Frank Luker

Hello Peter,

thanks for your fast response. Unfortunately it does not work...

Currently I use this for WinCE:
[DllImport("coredll.dll")]

private static extern bool QueryPerformanceFrequency(out long lpFrequency);

[DllImport("coredll.dll")]

private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);


and that for XP:
[DllImport("kernel32.dll")]

private static extern bool QueryPerformanceFrequency(out long lpFrequency);

[DllImport("kernel32.dll")]

private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);


If I use both at the same time with different method names then I get a
runtime error in windows mobile that the PInvoke DLL "kernel32.dll" was not
found as soon as I want to initialize my class.
 
F

Frank Luker

Ok, I found a solution:

I have put my external methods into 2 different classes and call those
helper classes by a central call that decides which class to load. So I can
avoid that a class with a wrong dll import is used.




Frank Luker said:
Hello Peter,

thanks for your fast response. Unfortunately it does not work...

Currently I use this for WinCE:
[DllImport("coredll.dll")]

private static extern bool QueryPerformanceFrequency(out long
lpFrequency);

[DllImport("coredll.dll")]

private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);


and that for XP:
[DllImport("kernel32.dll")]

private static extern bool QueryPerformanceFrequency(out long
lpFrequency);

[DllImport("kernel32.dll")]

private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);


If I use both at the same time with different method names then I get a
runtime error in windows mobile that the PInvoke DLL "kernel32.dll" was
not found as soon as I want to initialize my class.




Peter Foot said:
You'll need to define 2 sets of PInvoke declarations - one for each
platform e.g. MethodNameCe and MethodNameXp. Then in your stopwatch class
check the platform (e.g. if(System.Environment.OSVersion.Platform ==
PlatformID.WinCE) in your constructor and store in a boolean member) and
call the correct PInvoke in each method depending on the platform.

Peter
 
D

Daniel Moth

FYI

This has been done:
http://www.danielmoth.com/Blog/2004/12/stopwatch.html

Also FYI, NETCF 3.5 offers this OOTB:
http://www.danielmoth.com/Blog/2007/02/netcf-35-api-additions.html

Cheers
Daniel
--
http://www.danielmoth.com/Blog

Frank Luker said:
Ok, I found a solution:

I have put my external methods into 2 different classes and call those
helper classes by a central call that decides which class to load. So I
can avoid that a class with a wrong dll import is used.




Frank Luker said:
Hello Peter,

thanks for your fast response. Unfortunately it does not work...

Currently I use this for WinCE:
[DllImport("coredll.dll")]

private static extern bool QueryPerformanceFrequency(out long
lpFrequency);

[DllImport("coredll.dll")]

private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);


and that for XP:
[DllImport("kernel32.dll")]

private static extern bool QueryPerformanceFrequency(out long
lpFrequency);

[DllImport("kernel32.dll")]

private static extern bool QueryPerformanceCounter(out long
lpPerformanceCount);


If I use both at the same time with different method names then I get a
runtime error in windows mobile that the PInvoke DLL "kernel32.dll" was
not found as soon as I want to initialize my class.




Peter Foot said:
You'll need to define 2 sets of PInvoke declarations - one for each
platform e.g. MethodNameCe and MethodNameXp. Then in your stopwatch
class check the platform (e.g. if(System.Environment.OSVersion.Platform
== PlatformID.WinCE) in your constructor and store in a boolean member)
and call the correct PInvoke in each method depending on the platform.

Peter

--
Peter Foot
Device Application Development MVP
www.peterfoot.net | www.inthehand.com

Hello,

I want to build a stopwatch class (high resolution timer) that runs
with compact framework 2.0 and also with a windows application.

I found a suitable class
(http://weblogs.asp.net/pwelter34/archive/2005/07/01/416999.aspx) but
the problem is that it imports "kernel32.dll" wich runs perfect in a
windows application but not in a windows mobile application. When I
change the DLLImport to "coredll.dll" then it runs perfectly with
compact framework but not with a windows application.

Is there a way of using the DLLImport dynamically at runtime depending
on the current platform?

Thanks in advance!
Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top